坦克大战(一期)-增加子弹类

Network
bingor_yhj 2 years ago
parent 5606e45574
commit e119a9798c

@ -0,0 +1,44 @@
package com.msb;
import java.awt.*;
/**
* @Author bingor
* @Date 2022-09-30 14:33
* @Description:
* @Version: 1.0
*/
public class Bullet extends Frame {
private int x, y;
private DirEnum dir;
public static final int WIDTH = 8;
public static final int HEIGHT = 8;
public static final int SPEED = 5;
public Bullet(int x, int y, DirEnum dir) {
this.x = x;
this.y = y;
this.dir = dir;
}
public void paint(Graphics g) {
Color color = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(color); //还原画笔原来的颜色
moving();
}
public void moving() {
switch (dir) {
case UP: y -= SPEED; break;
case DOWN: y += SPEED; break;
case LEFT: x -= SPEED; break;
case RIGHT: x += SPEED; break;
default: break;
}
}
}

@ -15,6 +15,7 @@ import java.awt.event.WindowEvent;
public class TankFrame extends Frame {
Tank tank = new Tank(200, 200, DirEnum.RIGHT);
Bullet bullet = new Bullet(200, 200, DirEnum.DOWN);
public TankFrame() {
@ -39,6 +40,7 @@ public class TankFrame extends Frame {
public void paint(Graphics g) {
super.paint(g);
tank.paint(g);
bullet.paint(g);
}
//这里使用内部内的原因是只有TankFrame用到

Loading…
Cancel
Save