@ -1,61 +1,63 @@
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @Description: 子弹
|
||||
* @Author: zhanxinlin
|
||||
* @Date: 2022-09-27 23:10
|
||||
*/
|
||||
public class Bullet {
|
||||
private static final int SPEED = 10;
|
||||
private static final int WIDTH = 20;
|
||||
private static final int HEIGHT = 20;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private FrameTank frameTank;
|
||||
private Direction direction;
|
||||
private boolean live = true;
|
||||
|
||||
public Bullet(int x, int y, Direction direction, FrameTank frameTank) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.direction = direction;
|
||||
this.frameTank = frameTank;
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
// 子弹超出界限,从子弹集合中移除
|
||||
if (!live) {
|
||||
frameTank.bulletList.remove(this);
|
||||
}
|
||||
|
||||
Color color = g.getColor();
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(x, y, WIDTH, HEIGHT);
|
||||
g.setColor(color);
|
||||
move();
|
||||
}
|
||||
|
||||
private void move() {
|
||||
// 定义坦克的移动特征
|
||||
switch (direction) {
|
||||
case LEFT:
|
||||
x -= SPEED;
|
||||
break;
|
||||
case UP:
|
||||
y -= SPEED;
|
||||
break;
|
||||
case RIGHT:
|
||||
x += SPEED;
|
||||
break;
|
||||
case DOWN:
|
||||
y += SPEED;
|
||||
break;
|
||||
}
|
||||
|
||||
// 子弹超出临界范围
|
||||
if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
|
||||
live = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.mashibing.tank;
|
||||
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* @Description: 子弹
|
||||
* @Author: zhanxinlin
|
||||
* @Date: 2022-09-27 23:10
|
||||
*/
|
||||
public class Bullet {
|
||||
private static final int SPEED = 10;
|
||||
private static final int WIDTH = 20;
|
||||
private static final int HEIGHT = 20;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private FrameTank frameTank;
|
||||
private Direction direction;
|
||||
private boolean live = true;
|
||||
|
||||
public Bullet(int x, int y, Direction direction, FrameTank frameTank) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.direction = direction;
|
||||
this.frameTank = frameTank;
|
||||
}
|
||||
|
||||
public void paint(Graphics g) {
|
||||
// 子弹超出界限,从子弹集合中移除
|
||||
if (!live) {
|
||||
frameTank.bulletList.remove(this);
|
||||
}
|
||||
|
||||
Color color = g.getColor();
|
||||
g.setColor(Color.RED);
|
||||
g.fillOval(x, y, WIDTH, HEIGHT);
|
||||
g.setColor(color);
|
||||
move();
|
||||
}
|
||||
|
||||
private void move() {
|
||||
// 定义坦克的移动特征
|
||||
switch (direction) {
|
||||
case LEFT:
|
||||
x -= SPEED;
|
||||
break;
|
||||
case UP:
|
||||
y -= SPEED;
|
||||
break;
|
||||
case RIGHT:
|
||||
x += SPEED;
|
||||
break;
|
||||
case DOWN:
|
||||
y += SPEED;
|
||||
break;
|
||||
}
|
||||
|
||||
// 子弹超出临界范围
|
||||
if (x < 0 || y < 0 || y > FrameTank.GAME_HEIGHT || x > FrameTank.GAME_WIDTH) {
|
||||
live = false;
|
||||
}
|
||||
}
|
||||
}
|
@ -1,8 +1,10 @@
|
||||
/**
|
||||
* @Description: 方向枚举
|
||||
* @Author: zhanxinlin
|
||||
* @Date: 2022-09-27 22:21
|
||||
*/
|
||||
public enum Direction {
|
||||
LEFT, UP, RIGHT, DOWN
|
||||
}
|
||||
package com.mashibing.tank;
|
||||
|
||||
/**
|
||||
* @Description: 方向枚举
|
||||
* @Author: zhanxinlin
|
||||
* @Date: 2022-09-27 22:21
|
||||
*/
|
||||
public enum Direction {
|
||||
LEFT, UP, RIGHT, DOWN
|
||||
}
|
After Width: | Height: | Size: 824 B |
After Width: | Height: | Size: 868 B |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 936 B |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 8.0 KiB |
After Width: | Height: | Size: 8.0 KiB |
After Width: | Height: | Size: 8.2 KiB |
After Width: | Height: | Size: 8.2 KiB |
After Width: | Height: | Size: 855 B |
After Width: | Height: | Size: 839 B |
After Width: | Height: | Size: 839 B |
After Width: | Height: | Size: 852 B |
After Width: | Height: | Size: 847 B |
After Width: | Height: | Size: 312 B |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1022 B |
After Width: | Height: | Size: 814 B |
After Width: | Height: | Size: 460 B |
After Width: | Height: | Size: 280 B |
After Width: | Height: | Size: 565 B |
After Width: | Height: | Size: 846 B |
After Width: | Height: | Size: 1.0 KiB |
After Width: | Height: | Size: 1.1 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 864 B |
After Width: | Height: | Size: 863 B |
After Width: | Height: | Size: 861 B |
After Width: | Height: | Size: 863 B |
After Width: | Height: | Size: 673 B |
After Width: | Height: | Size: 670 B |
After Width: | Height: | Size: 680 B |
After Width: | Height: | Size: 677 B |
After Width: | Height: | Size: 660 B |
After Width: | Height: | Size: 691 B |
After Width: | Height: | Size: 674 B |
After Width: | Height: | Size: 271 KiB |
After Width: | Height: | Size: 1.3 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.2 KiB |
After Width: | Height: | Size: 1.4 KiB |
After Width: | Height: | Size: 1.7 KiB |
After Width: | Height: | Size: 1.3 KiB |
@ -0,0 +1,33 @@
|
||||
package test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* 读取图片测试类
|
||||
*/
|
||||
class ImageTest {
|
||||
|
||||
@Test
|
||||
void test() {
|
||||
|
||||
try {
|
||||
// 读取图片路径写死,不推荐
|
||||
// BufferedImage image = ImageIO.read(new File("C:/work/javaprojects/Tank_60/src/images/bulletD.gif"));
|
||||
// assertNotNull(image);
|
||||
|
||||
BufferedImage image2 = ImageIO.read(ImageTest.class.getClassLoader().getResourceAsStream("images/bulletD.gif"));
|
||||
assertNotNull(image2);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|