In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Java to achieve tank wars Mini Game code how to write, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Mini Game introduction:
The red tank is our control tank, the black is the enemy tank.
The next key about control tank movement direction
Press the ctrl key to fire the shell
Red tanks can go through walls, but black can't.
The specific pages are as follows:
Complete source code is provided:
Tank.java
Import java.awt.*;import java.awt.event.*;import java.util.*;public class Tank {private int x; private int y; private int oldx; private int oldy; private int life = 100th private boolean bL = false, bU = false, bR = false, bD= false;// generates random numbers private static Random r = new Random (); / / Nine kinds of tank movement directions enum Direction {Ludi Ludi / / initialize the tank direction private Direction dir = Direction.STOP;// initialize the barrel direction private Direction ptDir = Direction.U;// tank moving speed private static final int XSPEED = 5 position private static final int YSPEED = 5 role static final int HIGHT / tank size private static final int WIDTH = 30 the TankClient static final int HIGHT = 30 TankClient tc;public int getLife () {return life;} public void setLife (int life) {this.life = life;} private boolean good = true / / define the tank type, and the enemy is still our public boolean isgood () {return good;} / / define the tank state private boolean live = true;// set enemy tank random movement steps private static int step = r.nextInt (12) + 3 public void setlive / construct tank status method public boolean islive () {return live;} public void setlive (boolean live) {this.live = live } / / Constructor public Tank (int xpenint y) {this.x = x; this.y = y;} public Tank (int xreint yling Boolean good, Direction dir,TankClient tc) {this (xjiny); this.good = good; this.dir = dir; this.tc = tc } public void draw (Graphics g) {if (! live) {if (! good) {tc.tanks.remove (this);} return;} Color c = g.getColor (); / /? If (good==true) {g.setColor (Color.RED);} / define the color of our tank else g.setColor (Color.BLACK); / / define the color of enemy tank g.fillOval (xmemy, WIDTH, HIGHT); / / define the location and size of the tank g.setColor (c); / /? Move (); switch (ptDir) / / painting barrel {case L: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y + Tank.HIGHT / 2); break; case LU: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y); break Case U: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH / 2, y); break; case RU: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH, y); break Case R: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH, y + Tank.HIGHT / 2); break; case RD: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH, y + Tank.HIGHT); break Case D: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x + Tank.WIDTH / 2, y + Tank.HIGHT); break; case LD: g.drawLine (x + Tank.WIDTH / 2, y + Tank.HIGHT / 2, x, y + Tank.HIGHT); break }} private void stay () {this.x=oldx; this.y=oldy;} / / Tank Mobile void move () {this.oldx=x; this.oldy=y; switch (dir) {case L: Xmuri / XSPEED; break; case LU: Xmuri / XSPEED; break; case U / break; case U / YSPEEDs; break; case RU: Xerox / XSPEEDs; Ymurf / YSPEEDs Case R: X-ray powder XSPEED; break; case RD: X-ray powder XSPEED; y-cake YSPEED; break; case D: X-ray powder XSPEED; break; case LD: X-ray powder XSPEED; break; case STOP: break;} if (this. Dirt orientation Direction. Stop) {ptDir = dir;} if (xTankClient.WinHigh-Tank.HIGHT) y=TankClient.WinHigh-Tank.HIGHT / / Let enemy tanks move freely if (! good) {Direction [] dirs= Direction.values (); / / convert enumerations to an array if (step = = 0) {step = r.nextInt (12) + 3; int rn = r.nextInt (dirs.length); / / generate random numbers dir = dirs [rn];} step-- If (r.nextInt (40) > 38) {this.fire ();} / / Tank direction void localDirection () {if (bLTH / bUBUBD) dir= Direction.L; else if (bLTH / bUBDbD) dir= Direction.LU; else if (! BLTHG / bUBDbD) Else if (! bLTE / bUBR / bD) dir= Direction.RU; else if (! BLTIX / bUBR / bRB / bD) dir= Direction.R; else if (! BLTHUBUBUP / bRUBD / bD) dir= Direction.RD; else if (! bLTHUBUBD / bRUBD) dir= Direction.D; else if (bLTHUBD / bLTUBD / bLBD / bLBD) } public void keyPressed (KeyEvent e) {int key = e.getKeyCode (); switch (key) {case KeyEvent.VK_CONTROL: fire (); break; case KeyEvent.VK_LEFT: bL=true; break; case KeyEvent.VK_UP: bU=true Break; case KeyEvent.VK_RIGHT: bR=true; break; case KeyEvent.VK_DOWN: bD=true; break;} localDirection (); / / get the execution direction} public void keyReleased (KeyEvent e) {int key = e.getKeyCode () Switch (key) {case KeyEvent.VK_LEFT: bL=false; break; case KeyEvent.VK_UP: bU=false; break; case KeyEvent.VK_RIGHT: bR=false; break; case KeyEvent.VK_DOWN: bD=false; break;} localDirection () } / / define tank firing bullet public Missile fire () {if (! live) return null; int x = this.x+Tank.WIDTH/2-Missile.WIDTH/2; int y = this.y + Tank.HIGHT/2-Missile.HIGHT/2; Missile m = new Missile } / / "collision detection" acquires the tank rectangle attribute public Rectangle getRect () {return new Rectangle (XJ YJ WIDT Hight);} / / "collision detection" public boolean tankHitWall (Wall w) {if (this.getRect (). Intersects (w.getRect () {stay (); return true;} return false } public boolean tankHitTank (java.util.List tanks) {for (int item0)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.