//为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着

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-坦克大战小游戏,为什么会出现上方向和左方向的子弹不能发射的情况?检查了好久,有大佬帮帮忙吗,小白睡不着的更多相关文章

  1. 《HTML5经典坦克大战》游戏(代码)

    前几天粗略地学了HTML5,然后就用它写了一个<经典坦克大战>游戏. 现在想分享一下我写的代码,写得不好请大家多多指教. 给大家推荐一个网站,这个网站是为大学生而做,为方便学习编程的同学而 ...

  2. day22 01 初识面向对象----简单的人狗大战小游戏

    day22 01 初识面向对象----简单的人狗大战小游戏 假设有一个简单的小游戏:人狗大战   怎样用代码去实现呢? 首先得有任何狗这两个角色,并且每个角色都有他们自己的一些属性,比如任务名字nam ...

  3. canvas绘制“飞机大战”小游戏,真香!

    canvas是ArkUI开发框架里的画布组件,常用于自定义绘制图形.因为其轻量.灵活.高效等优点,被广泛应用于UI界面开发中. 本期,我们将为大家介绍canvas组件的使用. 一.canvas介绍 1 ...

  4. 用面向对象的编程方式实现飞机大战小游戏,java版

    概述 本文将使用java语言以面向对象的编程方式一步一步实现飞机大战这个小游戏 本篇文章仅供参考,如有写的不好的地方或者各位读者哪里没看懂可以在评论区给我留言 或者邮件8274551712@qq.co ...

  5. docker项目——搭建飞机大战小游戏

    项目2:搭建打飞机小游戏,验证数据持久化(最底下有链接) 第一步:拉取镜像 [root@localhost docker-image]# docker load < httpd_img.tar. ...

  6. 喜迎2015年新年:坦克大战(Robocode)游戏编程比赛图文总结

    2015春节前,葡萄城的软件工程师以特有的方式来迎接新年——2015新年编程邀请赛. 邀请赛的初衷,是和大家一起,寻找编程最初的单纯的快乐.       在代码的世界里,添加动力,继续远航.      ...

  7. IOS学习之路五(SpriteKit 开发飞机大战小游戏一)

    参考SpriteKit 创建游戏的教程今天自己动手做了一下,现在记录一下自己怎么做的,今天之做了第一步,一共有三个部分. 第一步,项目搭建. 项目所用图片资源:点击打开链接 1.在Xcode打开之后, ...

  8. Python小游戏之 - 飞机大战美女 !

    用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...

  9. Python小游戏之 - 飞机大战 !

    用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame # 用一个常量来存储屏 ...

随机推荐

  1. html5中的Web Storage

    html5中的Web Storage包括了两种存储方式:sessionStorage和localStorage.sessionStorage用于本地存储一个会话(session)中的数据,这些数据只有 ...

  2. Codeforces Round #615 (Div. 3)

    A. Collecting Coins 题目链接:https://codeforces.com/contest/1294/problem/A 题意: 你有三个姐妹她们分别有 a , b , c枚硬币, ...

  3. 夜晚 十点 React-Native 源码 暴力畜 系列

    百度 上 给的 关于 React-Native 的 排名 前三 继续 跟

  4. 解决 C# GetPixel 和 SetPixel 效率问题(转)

    在对Bitmap图片操作的时候,有时需要用到获取或设置像素颜色方法:GetPixel 和 SetPixel, 如果直接对这两个方法进行操作的话速度很慢,这里我们可以通过把数据提取出来操作,然后操作完在 ...

  5. idea搭建springmvc(maven版)

    一.创建maven项目 (1)选择 file > new > project (2)填写对应信息,一路点击next 配置自己本地的maven,继续next 命名项目名(随意写,但要易懂), ...

  6. BERT模型总结

    BERT模型总结 前言 ​ BERT是在Google论文<BERT: Pre-training of Deep Bidirectional Transformers for Language U ...

  7. openresty http

    openresty http openresty默认没有提供http客户端,需要第三方提供插件. 下载方式: wget https://raw.githubusercontent.com/pintsi ...

  8. 使用IDEA构建Spring-boot多模块项目配置流程

    使用IDEA构建Spring-boot多模块项目配置流程 1.创建项目 点击Create New Project 在左侧选中Spring Initializer,保持默认配置,点击下一步. 在Grou ...

  9. LeetCode 304. Range Sum Query 2D - Immutable 二维区域和检索 - 矩阵不可变(C++/Java)

    题目: Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper ...

  10. 批处理版MPlayer播放器(甲兵时代原创批处理)(下)

    注意,由于空间不支持显示退格键,需要自己手动补上,方法如上图: 接上篇: 批处理版音视频播放器上(甲兵时代原创批处理) :Bc cls COLOR 2F echo. call :colour &quo ...