修改代码格式

master
kn5886348135 3 years ago
parent 467ede82ca
commit a7dd8af564

@ -1,11 +1,12 @@
package com.example.tankbattle;
import java.awt.*;
import java.awt.Graphics;
import java.awt.Color;
public class Bullet {
public static final int SPEED = 10;
private static final int WIDTH=5, HEIGHT = 5;
private int x,y;
private static final int WIDTH = 5, HEIGHT = 5;
private int x, y;
private Dir dir;
public Bullet(int x, int y, Dir dir) {
@ -14,11 +15,11 @@ public class Bullet {
this.dir = dir;
}
public void paint(Graphics graphics) {
Color color = graphics.getColor();
graphics.setColor(Color.RED);
graphics.fillOval(x, y, 50, 50);
graphics.setColor(color);
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.RED);
g.fillOval(x, y, 50, 50);
g.setColor(c);
move();
}
@ -27,12 +28,13 @@ public class Bullet {
case LEFT:
x -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;

@ -1,5 +1,5 @@
package com.example.tankbattle;
public enum Dir {
LEFT, UP, RIGHT, DOWN,
LEFT, UP, RIGHT, DOWN
}

@ -1,7 +1,7 @@
package com.example.tankbattle;
public class Main {
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws InterruptedException {
TankFrame tankFrame = new TankFrame();
while (true) {
Thread.sleep(50);

@ -8,15 +8,15 @@ public class Tank {
private Dir dir = Dir.DOWN;
public static final int SPEED = 5;
private TankFrame tankFrame = null;
private TankFrame tf = null;
private boolean moving = false;
public Tank(int x, int y, Dir dir, TankFrame tankFrame) {
public Tank(int x, int y, Dir dir, TankFrame tf) {
this.x = x;
this.y = y;
this.dir = dir;
this.tankFrame = tankFrame;
this.tf = tf;
}
public boolean isMoving() {
@ -31,16 +31,16 @@ public class Tank {
this.dir = dir;
}
public void paint(Graphics graphics) {
Color color = graphics.getColor();
graphics.setColor(Color.YELLOW);
graphics.fillRect(x, y, 50, 50);
graphics.setColor(color);
public void paint(Graphics g) {
Color c = g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, 50, 50);
g.setColor(c);
move();
}
public void fire(){
tankFrame.bullets.add(new Bullet(this.x, this.y, this.dir));
tf.bullets.add(new Bullet(this.x, this.y, this.dir));
}
private void move() {
@ -51,12 +51,12 @@ public class Tank {
case LEFT:
x -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case UP:
y -= SPEED;
break;
case RIGHT:
x += SPEED;
break;
case DOWN:
y += SPEED;
break;

@ -10,14 +10,9 @@ import java.util.List;
public class TankFrame extends Frame {
private static int x = 200, y = 200;
Dir dir = Dir.DOWN;
private static final int SPEED = 10;
Tank tankA = new Tank(200, 200, Dir.DOWN,this);
Tank tankB = new Tank(200, 200, Dir.DOWN,this);
Tank myTank = new Tank(200, 200, Dir.DOWN, this);
private static final int GAME_WIDTH = 800,GAME_HEIGHT=600;
@ -46,20 +41,20 @@ public class TankFrame extends Frame {
offScreenImage = createImage(GAME_WIDTH, GAME_HEIGHT);
}
Graphics gOffScreen = offScreenImage.getGraphics();
Color color = gOffScreen.getColor();
Color c = gOffScreen.getColor();
gOffScreen.setColor(Color.BLACK);
gOffScreen.fillRect(0, 0, GAME_WIDTH, GAME_HEIGHT);
gOffScreen.setColor(color);
gOffScreen.setColor(c);
paint(gOffScreen);
g.drawImage(offScreenImage, 0, 0, null);
}
@Override
public void paint(Graphics graphics) {
tankA.paint(graphics);
bullet.paint(graphics);
public void paint(Graphics g) {
myTank.paint(g);
bullet.paint(g);
for (Bullet bullet1 : bullets) {
bullet.paint(graphics);
bullet.paint(g);
}
}
@ -111,7 +106,7 @@ public class TankFrame extends Frame {
bD = false;
break;
case KeyEvent.VK_CONTROL:
tankA.fire();
myTank.fire();
break;
default:
break;
@ -122,21 +117,13 @@ public class TankFrame extends Frame {
private void setMainTankDir() {
if (!bL && !bR && !bU && !bD) {
tankA.setMoving(false);
myTank.setMoving(false);
} else {
tankA.setMoving(true);
if (bL) {
tankA.setDir(Dir.LEFT);
}
if (bR) {
tankA.setDir(Dir.RIGHT);
}
if (bU) {
tankA.setDir(Dir.UP);
}
if (bD) {
tankA.setDir(Dir.DOWN);
}
myTank.setMoving(true);
if (bL) myTank.setDir(Dir.LEFT);
if (bR) myTank.setDir(Dir.RIGHT);
if (bU) myTank.setDir(Dir.UP);
if (bD) myTank.setDir(Dir.DOWN);
}
}
}

Loading…
Cancel
Save