代码格式

master
kn5886348135 3 years ago
parent a7dd8af564
commit 11457b64b8

@ -4,8 +4,8 @@ import java.awt.Graphics;
import java.awt.Color; import java.awt.Color;
public class Bullet { public class Bullet {
public static final int SPEED = 10; private static final int SPEED = 1;
private static final int WIDTH = 5, HEIGHT = 5; private static final int WIDTH = 30, HEIGHT = 30;
private int x, y; private int x, y;
private Dir dir; private Dir dir;
@ -18,7 +18,7 @@ public class Bullet {
public void paint(Graphics g) { public void paint(Graphics g) {
Color c = g.getColor(); Color c = g.getColor();
g.setColor(Color.RED); g.setColor(Color.RED);
g.fillOval(x, y, 50, 50); g.fillOval(x, y, WIDTH, HEIGHT);
g.setColor(c); g.setColor(c);
move(); move();
} }

@ -1,7 +1,7 @@
package com.example.tankbattle; package com.example.tankbattle;
import java.awt.Graphics;
import java.awt.*; import java.awt.Color;
public class Tank { public class Tank {
private int x,y; private int x,y;
@ -39,10 +39,6 @@ public class Tank {
move(); move();
} }
public void fire(){
tf.bullets.add(new Bullet(this.x, this.y, this.dir));
}
private void move() { private void move() {
if (!moving) { if (!moving) {
return; return;
@ -64,4 +60,8 @@ public class Tank {
break; break;
} }
} }
public void fire(){
tf.bullets.add(new Bullet(this.x, this.y, this.dir));
}
} }

@ -10,16 +10,13 @@ import java.util.List;
public class TankFrame extends Frame { public class TankFrame extends Frame {
private static final int SPEED = 10;
Tank myTank = new Tank(200, 200, Dir.DOWN, this); Tank myTank = new Tank(200, 200, Dir.DOWN, this);
Bullet bullet = new Bullet(300, 300, Dir.DOWN);
private static final int GAME_WIDTH = 800,GAME_HEIGHT=600; private static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
List<Bullet> bullets = new ArrayList<>(); List<Bullet> bullets = new ArrayList<>();
Bullet bullet = new Bullet(300, 300, Dir.DOWN);
public TankFrame() throws HeadlessException { public TankFrame() throws HeadlessException {
setSize(GAME_WIDTH, GAME_HEIGHT); setSize(GAME_WIDTH, GAME_HEIGHT);
setResizable(false); setResizable(false);
@ -28,7 +25,7 @@ public class TankFrame extends Frame {
addKeyListener(new MyKeyListener()); addKeyListener(new MyKeyListener());
addWindowListener(new WindowAdapter() { addWindowListener(new WindowAdapter() {
@Override @Override
public void windowClosed(WindowEvent e) { public void windowClosing(WindowEvent e) {
System.exit(0); System.exit(0);
} }
}); });
@ -54,7 +51,7 @@ public class TankFrame extends Frame {
myTank.paint(g); myTank.paint(g);
bullet.paint(g); bullet.paint(g);
for (Bullet bullet1 : bullets) { for (Bullet bullet1 : bullets) {
bullet.paint(g); bullet1.paint(g);
} }
} }

Loading…
Cancel
Save