Javase-坦克大战小游戏,为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着
//为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着
package TanKe.lbl;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;
import java.util.concurrent.atomic.DoubleAdder;
//JFrame为事件源
public class MytankGame extends JFrame{
Mypanel mp = null;
public static void main(String[] args) {
MytankGame mg = new MytankGame();
}
//构造方法设置画布属性,定义自己的画框,并将划款添加进JFrame
public MytankGame() {
this.setTitle("坦克大战");
mp = new Mypanel();
this.add(mp);
//启动线程
Thread t = new Thread(mp);
t.start();
//创建监听
this.addKeyListener(mp);
this.setSize(400,300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//定义自己的画框,并且作为监听者类
class Mypanel extends JPanel implements KeyListener,Runnable{
Ltank LL = null;
Vector<Dtank> vv = new Vector<Dtank>();
int enSize = 6;
//构造函数实例化我的坦克
public Mypanel() {
LL = new Ltank(170,130);//坦克的初始坐标
//创建敌人坦克引用
for(int i = 0; i < enSize; i ++) {
Dtank dd = new Dtank((i+1) * 60, 0);
//设置颜色
dd.setColor(1);
//设置方向
dd.setDirect(1);
//将引用添加到集合里
vv.add(dd);
}
}
//重写父类的画笔方法
public void paint(Graphics g) {
super.paint(g);
//设置地图为黑色
g.fillRect(0, 0, 400, 300);
//画出自己的坦克
this.drawTank(LL.getX(), LL.getY(), g, LL.direct, 0);
//判断子弹不为空
if(LL.ss != null&&LL.ss.isLive == true) {
g.draw3DRect(LL.ss.x, LL.ss.y, 1, 1, false);
}
//画出敌方坦克
for(int i = 0; i < vv.size(); i ++) {
//取出坦克并且赋予坐标等属性画出来
this.drawTank(vv.get(i).getX(), vv.get(i).getY(), g, vv.get(i).direct, 1);
}
}
//画的动作,这么画,如何画
public void drawTank(int x, int y, Graphics g, int direct, int type) {
switch (type) {
case 0:
g.setColor(Color.cyan);
break;
case 1:
g.setColor(Color.blue);
break;
}
switch (direct) {
//向上
case 0:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y);
break;
//向下
case 1:
g.fill3DRect(x, y, 5, 30, false);
g.fill3DRect(x + 15, y, 5, 30, false);
g.fill3DRect(x + 5, y + 5, 10, 20, false);
g.fillOval(x + 5, y + 10, 10, 10);
g.drawLine(x + 10, y + 15, x + 10, y + 30);
break;
//向左
case 2:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x, y + 10);
break;
//向右
case 3:
g.fill3DRect(x, y, 30, 5, false);
g.fill3DRect(x, y + 15, 30, 5, false);
g.fill3DRect(x + 5, y + 5, 20, 10, false);
g.fillOval(x + 10, y + 5, 10, 10);
g.drawLine(x + 15, y + 10, x + 30, y + 10);
break;
}
}
//重写监听者的方法
public void keyPressed(KeyEvent arg0) {
//识别按键
if(arg0.getKeyCode() == KeyEvent.VK_W) {
this.LL.setDirect(0);
this.LL.moveup();
}
else if(arg0.getKeyCode() == KeyEvent.VK_S) {
this.LL.setDirect(1);
this.LL.movedown();
}
else if(arg0.getKeyCode() == KeyEvent.VK_A) {
this.LL.setDirect(2);
this.LL.moveleft();
}
else if(arg0.getKeyCode() == KeyEvent.VK_D) {
this.LL.setDirect(3);
this.LL.moveright();
}
//坦克开火
if (arg0.getKeyCode() == KeyEvent.VK_J) {
this.LL.shotDtank();
}
this.repaint();
}
@Override
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.repaint();
}
}
}
//定义子弹类,实现线程接口
class Shot implements Runnable{
int x;
int y;
int direct;
int speed = 2;
boolean isLive = true;
public Shot(int x, int y, int direct) {
this.x = x;
this.y = y;
this.direct = direct;
}
//线程方法
public void run() {
while(true) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
switch(direct) {
case 0:
y = y - speed;
case 1:
y = y + speed;
case 2:
x = x - speed;
case 3:
x = x + speed;
}
//何时死亡
//子弹碰到边缘
if(x<0||x>400||y<0||y>300) {
this.isLive = false;
break;
}
}
}
}
//定义一个坦克类
class Tank {
int x = 0;
int y = 0;
//0,1,2,3分别是上下左右
int direct = 0;
int speed = 1;
int color;
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
public Tank( int x, int y) {
this.x = x;
this.y = y;
}
//获取坦克的x,y坐标
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
//方向
public int getDirect() {
return direct;
}
public void setDirect(int direct) {
this.direct = direct;
}
//速度
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
}
//我方坦克类
class Ltank extends Tank {
Shot ss = null;
public Ltank(int x, int y) {
super(x,y);
}
//开火方法
public void shotDtank() {
//判断子弹方向
switch(this.direct){
case 0:
ss = new Shot(x + 10, y, 0);
break;
case 1:
ss = new Shot(x + 10, y + 30, 1);
break;
case 2:
ss = new Shot(x, y + 10,2);
break;
case 3:
ss = new Shot(x + 30, y + 10,3);
break;
}
//启动子弹线程
Thread t = new Thread(ss);
t.start();
}
//坦克向上移动
public void moveup() {
y= y - speed;
}
//向下移动
public void movedown() {
y= y + speed;
}
//向左移动
public void moveleft() {
x= x - speed;
}
//向右移动
public void moveright() {
x= x + speed;
}
}
//敌方坦克类
class Dtank extends Tank {
public Dtank(int x, int y) {
super(x,y);
}
}
Javase-坦克大战小游戏,为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着的更多相关文章
- 《HTML5经典坦克大战》游戏(代码)
前几天粗略地学了HTML5,然后就用它写了一个<经典坦克大战>游戏. 现在想分享一下我写的代码,写得不好请大家多多指教. 给大家推荐一个网站,这个网站是为大学生而做,为方便学习编程的同学而 ...
- day22 01 初识面向对象----简单的人狗大战小游戏
day22 01 初识面向对象----简单的人狗大战小游戏 假设有一个简单的小游戏:人狗大战 怎样用代码去实现呢? 首先得有任何狗这两个角色,并且每个角色都有他们自己的一些属性,比如任务名字nam ...
- canvas绘制“飞机大战”小游戏,真香!
canvas是ArkUI开发框架里的画布组件,常用于自定义绘制图形.因为其轻量.灵活.高效等优点,被广泛应用于UI界面开发中. 本期,我们将为大家介绍canvas组件的使用. 一.canvas介绍 1 ...
- 用面向对象的编程方式实现飞机大战小游戏,java版
概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...
- docker项目——搭建飞机大战小游戏
项目2:搭建打飞机小游戏,验证数据持久化(最底下有链接) 第一步:拉取镜像 [root@localhost docker-image]# docker load < httpd_img.tar. ...
- 喜迎2015年新年:坦克大战(Robocode)游戏编程比赛图文总结
2015春节前,葡萄城的软件工程师以特有的方式来迎接新年——2015新年编程邀请赛. 邀请赛的初衷,是和大家一起,寻找编程最初的单纯的快乐. 在代码的世界里,添加动力,继续远航. ...
- IOS学习之路五(SpriteKit 开发飞机大战小游戏一)
参考SpriteKit 创建游戏的教程今天自己动手做了一下,现在记录一下自己怎么做的,今天之做了第一步,一共有三个部分. 第一步,项目搭建. 项目所用图片资源:点击打开链接 1.在Xcode打开之后, ...
- Python小游戏之 - 飞机大战美女 !
用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...
- Python小游戏之 - 飞机大战 !
用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame # 用一个常量来存储屏 ...
随机推荐
- go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer)
目录 go微服务框架kratos学习笔记七(kratos warden 负载均衡 balancer) demo demo server demo client 池 dao service p2c ro ...
- 安装numpy、matplotlib
一.安装numpy 1.下载 https://pypi.org/project/numpy/#files 2.安装 pip3 install numpy-1.17.3-cp37-cp37m-win_a ...
- 分布式唯一ID自增(雪花算法)
public class IdWorker { // ==============================Fields===================================== ...
- svn和 android adt的 eclipse插件更新地址
下边这两个插件的更新地址是每次安装android开发环境时都能用到的,为了方便在这里记录一下. android adt: http://dl-ssl.google.com/android/eclips ...
- Processing 3!
Welcome to Processing 3! Dan explains the new features and changes; the links Dan mentions are on th ...
- JS-05-元素获取
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 简单的在jsp页面操作mysql
---恢复内容开始--- 上一篇讲了在DOS界面下操作mysql 现在我们来说说怎么在jsp页面中操作mysql 要用jsp页面操作mysql需要jdbc(不是非要jdbc,还有其他的) 下载地址:w ...
- Selenium(一):元素定位
一.Selenium 8种定位方式 baidu.html <form id="form" name="f" action="/s" c ...
- nmap详解之选项说明
功能选项 功能选项可以组合使用.一些功能选项只能够在某种扫描模式下使用.nmap会自动识别无效或者不支持的功能选项组合,并向用户发出警告信息. 如果你是有经验的用户,可以略过结尾的示例一节.可以使用n ...
- IDEA | Dot Executable: null..No dot executable found
背景 今天想在IDEA上打开.puml后缀的类图,发现IDEA并不能识别,但是正常的时序图却能正常打开,打开类图就报如下错误: 解决方案 经排查,idea的plantuml插件默认只支持时序图,类图还 ...