java项目 ---坦克大战,坦克向8个方向运动
程序员文章站
2022-04-07 12:28:02
...
窗体类;
--------------------------------------
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//消除闪烁,使用双缓冲
//线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;
public class TankClient extends Frame {
private static final int GAME_WIDTH = 800;
private static final int GAME_HIGHT = 600;
private int ScreenHight = 700;
Tank myTank = new Tank(50, 50);
private Image offScreenImage = null;
// 重写
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HIGHT);
}
// 拿到图片上的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.green);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);// 画在背后图片上
g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上
}
public void paint(Graphics g) {
myTank.draw(g);
}
public void lauchFrame() {
setTitle("TankWar");
setBounds(230, 100, 800, 600);
// 匿名内部类,短,不涉及将来的扩展
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setBackground(Color.GREEN);
setVisible(true);
addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
// 内部类,只为这个TankWar服务,方便的访问包装类的方法,不方便公开,
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();// 内部调用父类的paint方法;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 键盘事件,内部类
private class KeyMoniter extends KeyAdapter {
public void keyReleased(KeyEvent e){
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
// 增加100辆坦克到游戏
private void addtank(int n) {
for (int i = 0; i < n; i++) {
}
}
}
-----------------------------------------------------------------------
坦克类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tank {
public static final int xspeed = 1;
public static final int yspeed = 1;
int x, y;
private boolean bL = false, bU = false, bR = false, bD = false;
public enum Direction {
L, LU, U, RU, R, RD, LD, D, STOP
};// 枚举类型
private Direction dir = Direction.STOP;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
g.setColor(c);
move();
}
// 移动坦克
void move() {
switch (dir) {
case L:
x -= xspeed;
break;
case LU:
x -= xspeed;
y -= yspeed;
break;
case U:
y -= yspeed;
break;
case RU:
x += xspeed;
y -= yspeed;
break;
case R:
x += xspeed;
break;
case RD:
x += xspeed;
y += yspeed;
break;
case LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
case STOP:
break;
}
}
public void keyPressed(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}
void locateDirection() {
if(bL&&!bU&&!bR&&!bD) dir=Direction.L;
else if(bL&&bU&&!bR&&!bD) dir=Direction.LU;
else if(!bL&&bU&&!bR&&!bD) dir=Direction.U;
else if(!bL&&bU&&bR&&!bD) dir=Direction.RU;
else if(!bL&&!bU&&bR&&!bD) dir=Direction.R;
else if(!bL&&!bU&&!bR&&bD) dir=Direction.D;
else if(!bL&&!bU&&bR&&bD) dir=Direction.RD;
else if(bL&&!bU&&!bR&&bD) dir=Direction.LD;
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
public void keyReleased(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();
}
}
------------------------------------------------------------
我会慢慢完善坦克大战这个小项目,一起互相学习!!
--------------------------------------
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
//消除闪烁,使用双缓冲
//线程重画更加均匀,更能控制重化的速度。按键重画不能解决子弹自动飞行的问题;
public class TankClient extends Frame {
private static final int GAME_WIDTH = 800;
private static final int GAME_HIGHT = 600;
private int ScreenHight = 700;
Tank myTank = new Tank(50, 50);
private Image offScreenImage = null;
// 重写
public void update(Graphics g) {
if (offScreenImage == null) {
offScreenImage = this.createImage(GAME_WIDTH, GAME_HIGHT);
}
// 拿到图片上的画笔
Graphics gOffScreen = offScreenImage.getGraphics();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.green);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HIGHT);
gOffScreen.setColor(c);
paint(gOffScreen);// 画在背后图片上
g.drawImage(offScreenImage, 0, 0, null);// 画在屏幕上
}
public void paint(Graphics g) {
myTank.draw(g);
}
public void lauchFrame() {
setTitle("TankWar");
setBounds(230, 100, 800, 600);
// 匿名内部类,短,不涉及将来的扩展
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
setResizable(false);
setBackground(Color.GREEN);
setVisible(true);
addKeyListener(new KeyMoniter());
new Thread(new PaintThread()).start();
}
public static void main(String[] args) {
TankClient tc = new TankClient();
tc.lauchFrame();
}
// 内部类,只为这个TankWar服务,方便的访问包装类的方法,不方便公开,
private class PaintThread implements Runnable {
public void run() {
while (true) {
repaint();// 内部调用父类的paint方法;
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
// 键盘事件,内部类
private class KeyMoniter extends KeyAdapter {
public void keyReleased(KeyEvent e){
myTank.keyReleased(e);
}
public void keyPressed(KeyEvent e) {
myTank.keyPressed(e);
}
}
// 增加100辆坦克到游戏
private void addtank(int n) {
for (int i = 0; i < n; i++) {
}
}
}
-----------------------------------------------------------------------
坦克类
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
public class Tank {
public static final int xspeed = 1;
public static final int yspeed = 1;
int x, y;
private boolean bL = false, bU = false, bR = false, bD = false;
public enum Direction {
L, LU, U, RU, R, RD, LD, D, STOP
};// 枚举类型
private Direction dir = Direction.STOP;
public Tank(int x, int y) {
this.x = x;
this.y = y;
}
public void draw(Graphics g) {
Color c = g.getColor();
g.setColor(Color.red);
g.fillOval(x, y, 30, 30);
g.setColor(c);
move();
}
// 移动坦克
void move() {
switch (dir) {
case L:
x -= xspeed;
break;
case LU:
x -= xspeed;
y -= yspeed;
break;
case U:
y -= yspeed;
break;
case RU:
x += xspeed;
y -= yspeed;
break;
case R:
x += xspeed;
break;
case RD:
x += xspeed;
y += yspeed;
break;
case LD:
x -= xspeed;
y += yspeed;
break;
case D:
y += yspeed;
break;
case STOP:
break;
}
}
public void keyPressed(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
case KeyEvent.VK_LEFT:
bL = true;
break;
case KeyEvent.VK_UP:
bU = true;
break;
case KeyEvent.VK_RIGHT:
bR = true;
break;
case KeyEvent.VK_DOWN:
bD = true;
break;
}
locateDirection();
}
void locateDirection() {
if(bL&&!bU&&!bR&&!bD) dir=Direction.L;
else if(bL&&bU&&!bR&&!bD) dir=Direction.LU;
else if(!bL&&bU&&!bR&&!bD) dir=Direction.U;
else if(!bL&&bU&&bR&&!bD) dir=Direction.RU;
else if(!bL&&!bU&&bR&&!bD) dir=Direction.R;
else if(!bL&&!bU&&!bR&&bD) dir=Direction.D;
else if(!bL&&!bU&&bR&&bD) dir=Direction.RD;
else if(bL&&!bU&&!bR&&bD) dir=Direction.LD;
else if(!bL&&!bU&&!bR&&!bD) dir=Direction.STOP;
}
public void keyReleased(KeyEvent e) {
// 获得键的虚拟码
int key = e.getKeyCode();
// if(key==KeyEvent.VK_RIGHT){
// x+=5;
// }if(key==KeyEvent.VK_LEFT){
// x=x-5;
// }if(key==KeyEvent.VK_UP){
// y=y-5;
// }if(key==KeyEvent.VK_DOWN){
// y=y+5;
// }
switch (key) {
case KeyEvent.VK_LEFT:
bL = false;
break;
case KeyEvent.VK_UP:
bU = false;
break;
case KeyEvent.VK_RIGHT:
bR = false;
break;
case KeyEvent.VK_DOWN:
bD = false;
break;
}
locateDirection();
}
}
------------------------------------------------------------
我会慢慢完善坦克大战这个小项目,一起互相学习!!
上一篇: 编程练习——计算器的改良
下一篇: perl单元测试