기본 기능이 구현되었습니다.... 로컬 탱크의 경로 탐색이 상대적으로 약합니다. 각 탱크는 동시에 최대 5발을 발사할 수 있습니다.... 적은 세 가지 유형의 병력을 가지고 있으며 메뉴 표시줄이 응답하지 않습니다. . 이것은 매우 간단합니다. 필요한 경우 직접 추가하세요. 학습 참조용으로만 모든 코드를 업로드하세요.
- package.tank.common;
-
- 추상 공용 클래스 Bullet은 Runnable, Common을 구현합니다. {
- private int x, y;
- private int speed;
- 개인 int 방향;
- 개인 부울 살아있음;
- 보호된 int 전력;
-
- public Bullet(int x, int y, int 속도, int 방향) {
- this.x = x;
- this.y = y;
- this.speed = 속도;
- this.direction = 방향;
- Alive = true;
- }
-
- public void die() {
- 살아있는 = false;
- }
-
- public int getPower() {
- return power;
- }
-
- public int getX() {
- return x ;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return Alive;
- }
-
- public void run() {
- while (true) {
- try {
- Thread.sleep(15);
- } catch (InterruptedException e) {
- // TODO 자동 생성된 캐치 블록
- e.printStackTrace();
- }
- 스위치(방향) {
- 케이스 UP:
- y -= 속도;
- 중단;
- 케이스 DOWN :
- y = 속도;
- 중단;
- 사례 RIGHT:
- x = 속도;
- 중단;
- 사례 LEFT:
- x -= 속도;
- 중단;
- }
- if (x WIDTH || y > HEIGHT || y Alive = false;
- return;
- }
- }
- }
-
- }
-
코드 복사
- 패키지 Tank.common;
-
- 공용 인터페이스 공통 {
- public static final int UP = 0;
- public static final int DOWN = 1;
- public static final int RIGHT = 2;
- public static final int LEFT = 3;
- public static final int WIDTH = 600;
- public static final int HEIGHT = 400;
- public static final int WATER = 0;
- public static final int WALLS = 1;
- public static final int STEELS = 2;
- public static final int GRASS = 3;
-
- }
-
코드 복사
- package.common;
-
- import java.awt.Color;
- import java.util.ArrayList;
-
- import Tank.entity .NormalBullet;
-
- public 추상 클래스 탱크 구현 Common {
- protected int x;
-
- protected int y;
-
- private Color color;
-
- private int 속도;
-
- private int 방향;
-
- private ArrayList bullets;
-
- private ArrayList 탱크;
-
- 개인 총알 총알;
-
- 개인 int maxBulletNum;
-
- 개인 부울 살아 있음;
-
- 보호된 int 생명;
-
- ArrayList< 벽> wall;
-
- private int TankImageID;
-
- {
- 속도 = 2;
- 방향 = UP;
- 살아있는 = true;
- }
-
- public Tank() {
- this.x = 0;
- this.y = 0;
- color = Color.black;
- }
-
- public Tank(int x, int y , Color color) {
- this.x = x;
- this.y = y;
- this.color = color;
- maxBulletNum = 5;
- bullets = new ArrayList( );
- wall = new ArrayList();
- }
-
- protected boolean canMove(int x, int y) {
- if (x < 0 || x > WIDTH - 20 || y < 0 || y > HEIGHT - 20) {
- return false;
- }
-
- if (tanks == null) {
- true 반환 🎜> }
-
- if (tanks.size() == 1) {
- return true;
- }
-
- for (int i = 0; i < wall.size (); i) {
- Wall tempWall = walls.get(i);
- if (tempWall.isAlive()) {
- if (x >= tempWall.getX() && y >= tempWall.getY()) {
- if (x <= tempWall.getX() 20 && y <= tempWall.getY() 20) {
- return tempWall.canBeWalk();
- }
- } else if (x >= tempWall.getX() && y <= tempWall.getY()) {
- if (x <= tempWall.getX() 20
- && (y 20) >= tempWall.getY()) {
- return tempWall.canBeWalk();
- }
- } else if (x <= tempWall.getX() && y >= tempWall.getY() ) {
- if ((x 20) >= tempWall.getX()
- && y <= tempWall.getY() 20) {
- return tempWall.canBeWalk();
- }
- } else if (x <= tempWall.getX() && y <= tempWall.getY()) {
- if ((x 20) >= tempWall.getX()
- && (y 20) >= tempWall.getY()) {
- return tempWall.canBeWalk();
- }
- }
- }
- }
-
- for (int i = 0 ; 나는 < 탱크.크기(); i) {
- 탱크 tempTank = Tanks.get(i);
- if (tempTank == this)
- break;
- if (tempTank.isAlive()) {
- if (x > ;= tempTank.getX() && y >= tempTank.getY()) {
- if (x <= tempTank.getX() 20 && y <= tempTank.getY() 20) {
- return false;
- }
- } else if (x >= tempTank.getX() && y <= tempTank.getY()) {
- if (x <= tempTank.getX() 20
- && (y 20) >= tempTank.getY()) {
- return false;
- }
- } else if (x <= tempTank.getX() && y >= tempTank .getY()) {
- if ((x 20) >= tempTank.getX()
- && y <= tempTank.getY() 20) {
- return false;
- }
- } else if (x <= tempTank.getX() && y <= tempTank.getY()) {
- if ((x 20) >= tempTank.getX()
- && (y 20) >= tempTank.getY()) {
- false 반환;
- }
- }
- }
- }
- true 반환;
- }
-
- public void Damage(int power) {
- lifes -= power;
- if (lifes <= 0) {
- Alive = false;
- }
- }
- public 배열목록 getBullet() {
- 글머리 기호 반환;
- }
- public Color getColor() {
- 반환 색상;
- }
- public int getDirection() {
- 복귀 방향;
- }
- public int getImageID() {
- return TankImageID;
- }
- public int getSpeed() {
- 복귀 속도;
- }
- 공개 ArrayList getWalls() {
- return this.walls;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return Alive;
- }
-
- public void move(int 방향) {
- setDirection (방향);
- int x = this.x;
- int y = this.y;
- 스위치(방향) {
- case UP:
- y -= 속도;
- break ;
- 케이스 DOWN:
- y = 속도;
- 중단;
- 케이스 RIGHT:
- x = 속도;
- 중단;
- 케이스 LEFT:
- x -= 속도 ;
- break;
- }
- if (canMove(x, y)) {
- this.x = x;
- this.y = y;
- }
- }
-
- public void setAllTanks(ArrayList 탱크) {
- this.tanks = 탱크;
- }
-
- public void setDirection(int 방향) {
- this.direction = 방향;
- }
-
- final protected void setImageID(int ID) {
- this.tankImageID = ID;
- }
-
- public void setSpeed(int speed) {
- this.speed = speed ;
- }
-
- public void setWalls(ArrayList wallList) {
- this.walls = wallList;
- }
-
- public void Shot() {
- 스위치(방향) {
- 케이스 UP:
- bullet = new NormalBullet(x 10, y, UP);
- break;
- 케이스 DOWN:
- bullet = new NormalBullet(x 10, y 20, DOWN);
- break;
- case RIGHT:
- bullet = new NormalBullet(x 20, y 10, RIGHT);
- break;
- case LEFT:
- bullet = new NormalBullet(x, y 10, LEFT);
- break;
- }
- for (int i = 0; i < bullets.size(); i) {
- Bullet temp = bullets.get (i);
- if (!temp.isAlive()) {
- bullets.remove(temp);
- }
- }
- if (bullets.size() >= maxBulletNum) {
-
- } else {
- new Thread(bullet).start();
- bullets.add(bullet);
- }
-
- }
- }
复代码
- package Tank.common;
-
- public abstract class Wall {
- private int x, y;
-
- private int wallImageID;
-
- private boolean canWalk;
-
- private boolean canFly;
-
- private boolean living;
-
- private boolean canHit;
-
- public Wall(int x, int y, int ID, 부울 워크, 부울 플라이, 부울 히트) {
- this.x = x;
- this.y = y;
- this.wallImageID = ID;
- this.canWalk = 걷기 ;
- this.canFly = fly;
- this.alive = true;
- this.canHit = Hit;
- }
-
- public boolean canBeFly() {
- return canFly;
- }
-
- public boolean canBeHit() {
- return this.canHit;
- }
-
- public boolean canBeWalk() {
- return canWalk;
- }
-
- public void die() {
- Alive = false;
- }
-
- public int getImageID() {
- return wallImageID;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- 살아서 돌아옴;
- }
-
- }
-
复代码
- package.entity;
-
- public class Bomb {
- private int x;
- private int y;
-
- private int life = 9;
-
- private boolean live;
-
- public Bomb(int x, int y) {
- this.x = x;
- this.y = y;
- Alive = true;
- }
-
- public void decrese() {
- if (life > 0) {
- life--;
- } else {
- Alive = false ;
- }
- }
-
- public int getLife() {
- return life;
- }
-
- public int getX() {
- return x;
- }
-
- public int getY() {
- return y;
- }
-
- public boolean isAlive() {
- return Alive;
- }
-
- }
-
复主代码
- package.entity;
-
- import Tank.common.EnemyTank;
- import Tank.common.Tank;
-
- 공개 클래스 EnemyTank1 확장 EnemyTank {
-
- public EnemyTank1(int x, int y, Tank Hero) {
- super(x, y, Hero, 3, 1);
- setSpeed(2);
- setShotSpeed( 0.8);
- }
- }
-
复代码
- package.entity;
-
- import Tank.common.EnemyTank;
- import Tank.common.Tank;
-
- 공개 클래스 EnemyTank2 확장 EnemyTank {
- public EnemyTank2(int x, int y, Tank Hero) {
- super(x, y, Hero, 5, 2);
- setSpeed(3);
- setShotSpeed(0.5);
- }
- }
-
复제대码
- package.entity;
-
- import Tank.common.EnemyTank;
- import Tank.common.Tank;
-
- 공개 클래스 EnemyTank3 확장 EnemyTank {
- public EnemyTank3(int x, int y, Tank Hero) {
- super(x, y, Hero, 10, 3);
- setSpeed(5);
- setShotSpeed(0.2);
- }
- }
-
复제대码
- package Tank.entity;
-
- import Tank.common.Wall;
-
- public 클래스 Grass 확장 Wall {
- public Grass(int x , int y) {
- super(x, y, 3, true, true, false);
- }
- }
-
复system代码
- package.entity;
-
- import java.awt.Color;
-
- import Tank.common.Tank;
-
- 공개 class MyTank 확장 탱크 {
- public MyTank(int x, int y) {
- super(x, y, Color.yellow);
- lifes = 5;
- super.setImageID(0);
- }
-
- public int getLife() {
- return lifes;
- }
-
- }
-
复主代码
- package Tank.entity;
-
- import Tank.common.Bullet;
-
- public class NormalBullet은 Bullet을 확장합니다. {
-
- public NormalBullet (int x, int y, int 방향) {
- super(x, y, 2, 방향);
- power = 1;
- }
-
- }
-
제제대码
- package.entity;
-
- import java.util.ArrayList;
-
- import Tank.common.EnemyTank;
-
- 공개 class Stage {
- private int totalEnemyNum;
- private int LeaveEnemyNum;
- private int totalHeroLife;
- private int LeaveHeroLife;
- private int level;
- private WallContainer wallContainer;
- private ArrayList< ;적탱크> enemeyTanks;
- private int activeEnemyTankNum;
- private EnemyTank activeEnemyTanks[];
- private MyTank Hero;
-
- public Stage(int totalEnemyNum, int totalHeroLife, int level,
- int activeEnemyTankNum) {
- this.totalEnemyNum = totalEnemyNum;
- this.totalHeroLife = totalHeroLife;
- this.level = level;
- this.activeEnemyTankNum = activeEnemyTankNum;
- this.leaveEnemyNum = this.totalEnemyNum;
- this.leaveHeroLife = this.totalHeroLife;
- this.enemeyTanks = new ArrayList();
- this.activeEnemyTanks = new EnemyTank[this.activeEnemyTankNum];
- this.hero = new MyTank(290, 370);
- this.wallContainer = new WallContainer();
- }
-
- public void addEnemyTank(EnemyTank 탱크) {
- enemeyTanks.add(tank);
- }
-
- public void autoCreateEnemyTank() {
- for (int i = 0; i < totalEnemyNum; i) {
- double key = Math.random();
- if (key <= 0.33) {
- this.enemeyTanks.add(new EnemyTank1(0, 0, 영웅));
- } else if (key >= 0.66) {
- this.enemeyTanks.add(new EnemyTank2(0, 0 , 영웅));
- } else {
- this.enemeyTanks.add(new EnemyTank3(0, 0, 영웅));
- }
- }
- }
-
- 공개 int getActiveEnemyTankNum() {
- return this.activeEnemyTankNum;
- }
-
- public EnemyTank[] getEnemyTank() {
- return this.activeEnemyTanks;
- }
-
- 공개 MyTank getHeroTank() {
- return this.hero;
- }
-
- public int getLeaveEnemyNum() {
- return this.leaveEnemyNum;
- }
-
- public int getLeaveHeroLife () {
- return this.leaveHeroLife;
- }
-
- public int getLevel() {
- return this.level;
- }
-
- public WallContainer getWallContainer() {
- return this.wallContainer;
- }
-
- public boolean isHeroDead() {
- if (this.leaveHeroLife <= 0) {
- return true;
- } else
- false 반환;
- }
- public EnemyTank popEnemyTank() {
- if (leaveEnemyNum > 0) {
- this.leaveEnemyNum--;
- EnemyTank temp = enemeyTanks.get(enemeyTanks.size() - 1);
- enemeyTanks.remove(temp);
- return temp;
- } else
- return null;
- }
-
- public MyTank popHero() {
- LeaveHeroLife--;
- MyTank temp = new MyTank(290, 370);
- temp. setWalls(wallContainer.getWallList());
- return temp;
- }
-
- public void setHeroTank(MyTank 탱크) {
- this.hero = 탱크;
- }
- }
-
复代码
- 패키지 탱크.entity;
-
- import Tank.common.Wall;
-
- public 클래스 Steels 확장 Wall {
- public Steels(int x , int y) {
- super(x, y, 2, false, false, false);
- }
- }
-
复system代码
- package.entity;
-
- import java.util.ArrayList;
-
- import Tank.common.Common;
- import Tank.common .Wall;
-
- 공용 클래스 WallContainer는 Common {
- private ArrayList을 구현합니다. data;
-
- public WallContainer() {
- data = new ArrayList();
- for (int i = 0; i < 28; i) {
- this.addWall (10 i * 20, 100, WATER);
- if (i == 11) {
- i = 4;
- }
- }
-
- for (int i = 0; i < 28; i) {
- this.addWall(10 i * 20, 160, WALLS);
- if (i == 12) {
- i = 4;
- }
- }
-
- for (int i = 0; i < 28; i) {
- this.addWall(10 i * 20, 220, STEELS);
- if (i == 11) {
- i = 4;
- }
- }
-
- for (int i = 0; i this.addWall(10 i * 20, 280, GRASS);
- if (i == 12) {
- i = 4;
- }
- }
- }
-
- public void addWall(int x, int y, int 종류) {
- 스위치(종류) {
- 케이스 WATER:
- data.add(new Water(x, y));
- break;
- 케이스 WALLS:
- data.add (new Walls(x, y));
- break;
- case STEELS:
- data.add(new Steels(x, y));
- break;
- case GRASS:
- data.add(new Grass(x, y));
- break;
- }
- }
-
- public ArrayList getWallList() {
- ArrayList temp = data;
- return temp;
- }
-
- public boolean isEmpty() {
- return data.isEmpty();
- }
-
- public void RemoveDead( ) {
- for (int i = 0; i < data.size(); i) {
- Wall temp = data.get(i);
- if (!temp.isAlive())
- data.remove(temp);
- }
- }
- }
复代码
- package.tank.entity;
-
- import Tank.common.Wall;
-
- public 클래스 Walls는 Wall을 확장합니다. {
- public Walls(int x , int y) {
- super(x, y, 1, false, false, true);
- }
- }
-
复system代码
- 패키지 Tank.entity;
-
- import Tank.common.Wall;
-
- public class Water 확장 Wall {
- public Water(int x , int y) {
- super(x, y, 0, false, true, false);
- }
- }
-
复system代码
- 탱크.gui 패키지;
-
- import javax.swing.JFrame;
- import javax.swing.JMenu;
- import javax.swing.JMenuBar;
- import javax.swing.JMenuItem;
-
- 공용 클래스 TankFrame은 JFrame을 확장합니다. {
-
- /**
- *
- */
- private static final long serialVersionUID = 1L;
- private TankPanel gamePanel;
-
- public TankFrame() {
- super("坦克大战————玄雨제작");
- this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- //添加游戏主面板
- gamePanel = new TankPanel();
- this.add(gamePanel);
- gamePanel.addMouseListener(gamePanel);
- this.addKeyListener(gamePanel);
-
- // 添加菜单栏
- JMenuBar menuBar = new JMenuBar();
- // ///////////////////////////// //
- JMenu menu1 = new JMenu("菜单");
- menuBar.add(menu1);
- JMenuItem itemNewGame = new JMenuItem("新游戏");
- menu1.add(itemNewGame) ;
- menu1.addSeparator();
- JMenuItem itemList = new JMenuItem("排行榜");
- menu1.add(itemList);
- menu1.addSeparator();
- JMenuItem itemExit = new JMenuItem("退출");
- menu1.add(itemExit);
- //////////////////////////// ///////
- JMenu menu2 = new JMenu("设置");
- JMenuItem itemSet = new JMenuItem("设置");
- menu2.add(itemSet);
- menuBar. add(menu2);
- // /////////////////////////////////
- JMenu 메뉴3 = 신규 JMenu("帮助");
- menuBar.add(menu3);
- JMenuItem itemInfo = new JMenuItem("关于");
- menu3.add(itemInfo);
- JMenuItem itemHelp = new JMenuItem( "帮助");
- menu3.add(itemHelp);
- this.setJMenuBar(menuBar);
-
- this.setRessible(false);
- this.pack();
- this.setVisible(true);
- }
-
- }
-
复代码
- package.tank.start;
-
- import Tank.gui.TankFrame;
-
- public class TankStart {
-
- /**
- * @param args
- */
- public static void main(String[] args) {
- // TODO 자동 생성 메서드 스텁
- new TankFrame().setLocation(250, 150);
- }
-
- }
-
复代码
|