关于坦克大战java代码的信息
JAVA坦克大战,这段代码为什么子弹的坐标在变,却不能repaint,但是按下任意键盘的建却重绘了呢?
Mypanel的 run方法里要调用repaint方法 否则你的repaint方法只会在keyPressed发生的时候才调用
创新互联自2013年创立以来,先为庐江等服务建站,庐江等地企业,进行企业商务咨询服务。为庐江企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
修改一下两个地方
(1)
// 键盘获取事件的函数
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_J) {
if (hero.shot.size() 5) {
hero.shott();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_W) {
hero.setSDC(hero.getSpeed(), 0, hero.getColor());
hero.moveUp();
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
hero.setSDC(hero.getSpeed(), 1, hero.getColor());
hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
hero.setSDC(hero.getSpeed(), 2, hero.getColor());
hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
hero.setSDC(hero.getSpeed(), 3, hero.getColor());
hero.moveRight();
}
/**
* 这个repaint注释掉
*/
//this.repaint();
}
(2)
// 线程
/**
* 一秒钟60帧
*/
public void run() {
// TODO Auto-generated method stub
while(true){
this.repaint();
try {
Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
完整代码如下:
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class aaa extends JFrame {
public static void main(String[] args) {
aaa a1 = new aaa();
Thread t1 = new Thread(a1.mp);
t1.start();
}
MyPanel mp = null;
public aaa() {
mp = new MyPanel();
this.add(mp);
this.addKeyListener(mp);
this.setSize(500, 500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class MyPanel extends JPanel implements KeyListener, Runnable {
MyTank hero = null;
VectorEmenyTank emeny = new VectorEmenyTank();
int emsize = 5;
// 键盘获取事件的函数
public void keyPressed(KeyEvent arg0) {
// TODO Auto-generated method stub
if (arg0.getKeyCode() == KeyEvent.VK_J) {
if (hero.shot.size() 5) {
hero.shott();
}
}
if (arg0.getKeyCode() == KeyEvent.VK_W) {
hero.setSDC(hero.getSpeed(), 0, hero.getColor());
hero.moveUp();
} else if (arg0.getKeyCode() == KeyEvent.VK_S) {
hero.setSDC(hero.getSpeed(), 1, hero.getColor());
hero.moveDown();
} else if (arg0.getKeyCode() == KeyEvent.VK_A) {
hero.setSDC(hero.getSpeed(), 2, hero.getColor());
hero.moveLeft();
} else if (arg0.getKeyCode() == KeyEvent.VK_D) {
hero.setSDC(hero.getSpeed(), 3, hero.getColor());
hero.moveRight();
}
/**
* 这个repaint注释掉
*/
//this.repaint();
}
public void keyReleased(KeyEvent arg0) {
// TODO Auto-generated method stub
}
public void keyTyped(KeyEvent arg0) {
// TODO Auto-generated method stub
}
// 完毕
public MyPanel() {
hero = new MyTank(250, 250);
hero.setSDC(5, 2, 2);
for (int i = 0; i emsize; ++i) {
EmenyTank em = new EmenyTank((i + 1) * 60, 20);
em.setSDC(5, 1, 1);
emeny.add(em);
}
}
// 线程
/**
* 一秒钟60帧
*/
public void run() {
// TODO Auto-generated method stub
while(true){
this.repaint();
try {
Thread.sleep(1000 / 60);
} catch (InterruptedException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
public void paint(Graphics g) {
super.paint(g);
// 画板,坦克得放在画板后头
g.fillRect(0, 0, 400, 400);
// paint敌人坦克
for (int i = 0; i emeny.size(); ++i) {
EmenyTank em = null;
em = emeny.get(i);
this.drawTank(em.getX(), em.getY(), g, em.getDirect(),
em.getColor());
}
// 画我自己的坦克
this.drawTank(hero.getX(), hero.getY(), g, hero.getDirect(),
hero.getColor());
// 画出我的子弹
for (int i = 0; i hero.shot.size(); i++) {
Shot myShot = hero.shot.get(i);
if (myShot != null myShot.live == true) {
g.draw3DRect(myShot.x, myShot.y, 2, 2, false);
}
if (myShot.live == false) {
hero.shot.remove(myShot);
}
}
}
public void drawTank(int x, int y, Graphics g, int direct, int color) {
// 判断坦克的颜色(敌我)然后画出坦克
switch (color) {
case 0:
g.setColor(Color.BLUE);
break;
case 1:
g.setColor(Color.YELLOW);
break;
case 2:
g.setColor(Color.GREEN);
break;
}
// 判断坦克的方向然后再画出坦克
switch (direct) {
case 0:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x + 26, y, 10, 30, false);
g.fill3DRect(x + 10, y + 5, 16, 20, false);
g.drawLine(x + 18, y + 15, x + 18, y);
break;
case 1:
g.fill3DRect(x, y, 10, 30, false);
g.fill3DRect(x + 26, y, 10, 30, false);
g.fill3DRect(x + 10, y + 5, 16, 20, false);
g.drawLine(x + 18, y + 15, x + 18, y + 30);
break;
case 2:
g.fill3DRect(x + 3, y - 3, 30, 10, false);
g.fill3DRect(x + 3, y + 23, 30, 10, false);
g.fill3DRect(x + 8, y + 7, 20, 16, false);
g.drawLine(x + 18, y + 15, x + 3, y + 15);
break;
case 3:
g.fill3DRect(x + 3, y - 3, 30, 10, false);
g.fill3DRect(x + 3, y + 23, 30, 10, false);
g.fill3DRect(x + 8, y + 7, 20, 16, false);
g.drawLine(x + 18, y + 15, x + 33, y + 15);
break;
}
}
}
class EmenyTank extends Tank implements Runnable {
public EmenyTank(int x, int y) {
// TODO Auto-generated method stub
super(x, y);
}
public void run() {
}
}
class Shot implements Runnable {
protected int x;
protected int y;
protected int direct;
protected int speed = 4;
protected boolean live = true;
public void setX(int x) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setDirect(int direct) {
this.direct = direct;
}
public int getDirect() {
return direct;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getSpeed() {
return speed;
}
// 子弹的上下左右以及走的速度
public void run() {
// TODO Auto-generated method stub
while (true) {
try {
Thread.sleep(100);
} catch (Exception e) {
}
switch (direct) {
case 0:
y -= speed;
break;
case 1:
y += speed;
break;
case 2:
x -= speed;
break;
case 3:
x += speed;
break;
}
if (x 400 || x 0 || y 400 || y 0) {
this.live = false;
break;
}
}
}
}
class Tank {
protected int x;
protected int y;
protected int speed = 5;
protected int direct;
protected int color;
boolean live;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setSDC(int speed, int direct, int color) {
this.speed = speed;
this.direct = direct;
this.color = color;
}
public int getSpeed() {
return speed;
}
public int getDirect() {
return direct;
}
public int getColor() {
return color;
}
}
class MyTank extends Tank {
public MyTank(int x, int y) {
// TODO Auto-generated method stub
super(x, y);
}
VectorShot shot = new VectorShot();
Shot shota = null;
public void shott() {
switch (this.direct) {
case 0:
shota = new Shot();
shota.x = x + 18;
shota.y = y;
shota.direct = 0;
shot.add(shota);
break;
case 1:
shota = new Shot();
shota.x = x + 18;
shota.y = y + 30;
shota.direct = 1;
shot.add(shota);
break;
case 2:
shota = new Shot();
shota.x = x + 3;
shota.y = y + 15;
shota.direct = 2;
shot.add(shota);
break;
case 3:
shota = new Shot();
shota.x = x + 33;
shota.y = y + 15;
shota.direct = 3;
shot.add(shota);
break;
}
Thread t = new Thread(shota);
t.start();
}
public void moveUp() {
if (y 0) {
y -= speed;
}
}// 我的坦克得在自己的类里定义怎么移动
public void moveDown() {
if (y 367) {
y += speed;
}
}
public void moveLeft() {
if (x 0) {
x -= speed;
}
}
public void moveRight() {
if (x 365) {
x += speed;
}
}
}
java坦克大战源代码 怎么导入
坦克大战源代码应该是个完整的项目吧。
对于完整的带项目配置文件的java源码,按步骤操作即可:
File - Import - General
选择Existing Projects into Workspace,选择要导入的文件,点击“finish",OK。
java之坦克大战
最近学习不喜欢在算法上磕太长时间了,所以我就开始写小游戏了,也算是给自己一个目标,写个游戏里面需要的东西很多,就第一项窗口的建造而言,JFreme类的使用中有很多的方法需要记忆,此外对于类与对象的使用也占了很大的部分。
我是观看教程然后学习制作坦克大战,最开始是制作窗口,用到了一个类JFrame下面是一些使用方法。
jf.setTitle("我是标题鸭!!!");(制作标题)
jf.setSize(20,10)//设置了一个长为20,高为10的框图。
jf.setBounds(1,2,20,10)//设置一个左上角顶点在(1,2),长为20,宽为10的窗体。
jf.setLocation(1,2)//设置一个左上角顶点在(1,2)的窗体。
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);(这样设置会导致你在关闭窗体的同时,终止程序的运行,会带来一定的便利性。)
jf.setVisible(true);(窗体在默认情况下是不可见的,只在后台运行,只有像这样设置可见后,在程序运行的时候,窗体才会出现。)
然后是背景窗口的上色与选项字体的添加, setColor方法:设置颜色, 该方法用于设置画笔的颜色,例如Color.BLACK:黑色,还需要填充画布使用 fillRect (int x,int y,int width,int height)绘制并填充矩形 4个参数,x、y起点坐标,宽,长。
不得不说有一个明确的目标确实学习的比较快。
分享文章:关于坦克大战java代码的信息
网站链接:http://scyanting.com/article/ddsicje.html