坦克大战(一期)-敌人坦克随机动

Network
bingor_yhj 2 years ago
parent 99c37fe372
commit 4cd5736900

@ -7,5 +7,12 @@ package com.msb;
* @Version: 1.0 * @Version: 1.0
*/ */
public enum DirEnum { public enum DirEnum {
LEFT, RIGHT, UP, DOWN LEFT, RIGHT, UP, DOWN;
public static DirEnum valueOf(int ordinal) {
if (ordinal < 0 || ordinal >= values().length) {
throw new IndexOutOfBoundsException("Invalid ordinal");
}
return values()[ordinal];
}
} }

@ -38,10 +38,6 @@ public class Tank {
return; return;
} }
/* Color color = g.getColor();
g.setColor(Color.YELLOW);
g.fillRect(x, y, 50, 50);
g.setColor(color);*/
switch (dir) { switch (dir) {
case UP: g.drawImage(ResourcesMgr.tankU, x, y, null); break; case UP: g.drawImage(ResourcesMgr.tankU, x, y, null); break;
case DOWN: g.drawImage(ResourcesMgr.tankD, x, y, null); break; case DOWN: g.drawImage(ResourcesMgr.tankD, x, y, null); break;

@ -1,5 +1,7 @@
package com.msb; package com.msb;
import java.util.Random;
/** /**
* @Author bingor * @Author bingor
* @Date 2022-09-29 11:02 * @Date 2022-09-29 11:02
@ -10,10 +12,11 @@ public class TankDemo {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
TankFrame tankFrame = new TankFrame(); TankFrame tankFrame = new TankFrame();
Random random = new Random();
//创建5个敌方坦克 //创建5个敌方坦克
for (int i=0; i<5; i++) { for (int i=0; i<5; i++) {
tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.DOWN, GroupEnum.BAD, tankFrame)); tankFrame.tanks.add(new Tank(100 + i*80, 100, DirEnum.valueOf(random.nextInt(3)), GroupEnum.BAD, tankFrame));
} }
//现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑 //现实当中,虽然可以过按键来改变方块的坐标并且重新刷新画板来实现移动,但是敌方的坦克应该是自动在跑

@ -1,5 +1,6 @@
package com.msb.test; package com.msb.test;
import com.msb.DirEnum;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -7,6 +8,7 @@ import javax.imageio.ImageIO;
import java.awt.image.BufferedImage; import java.awt.image.BufferedImage;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.Random;
/** /**
* @Author bingor * @Author bingor
@ -24,4 +26,13 @@ public class CommonTest {
Assert.assertNotNull(image); Assert.assertNotNull(image);
} }
@Test
public void testEnum() {
Random random = new Random();
System.out.println(DirEnum.valueOf(random.nextInt(3)));
System.out.println(DirEnum.valueOf(random.nextInt(3)));
System.out.println(DirEnum.valueOf(random.nextInt(3)));
System.out.println(DirEnum.valueOf(random.nextInt(3)));
}
} }

Loading…
Cancel
Save