I am participating in the nuggets Community game creative submission Contest. For details, please see: Game Creative Submission Contest.

preface

“Game” idea for a back-end CURD programmer is relatively strange, not very good at, but I want a reward, thinking for a long time, thought of the beginning of Java, training teacher led to do a classic small game – [aircraft war], so this period to record the development process of aircraft war.

Design idea:

1. Create a JFrame form implementation of the game interface

2. Create the fighter class and the Bullet class, and place the bullet starting point in front of the fighter

3. Create an enemy class.

4. Determine the collision method between enemy aircraft and bullets, that is, the enemy aircraft disappears after being hit, and determine the collision method between enemy aircraft and fighter aircraft

Overall development idea

The first step is to create the JFrame form and set the size

public class Game { public static void main(String[] args) { JFrame j = new JFrame(); // Create window j.setSize(400, 650); j.setTitle("PlaneWar"); // Set the header j.setlocationRelativeto (null); / / set the center j.s etDefaultCloseOperation (3); // default to disable j.setVisible(true); // Window visibility}}Copy the code

Create the canvas and set the background image

Public GamePanel() {state=START; bx = 0; by = -5350; bg = new ImageIcon("src/images/background_1.png").getImage(); }Copy the code

Create the fighter class, define x and y as the fighter coordinates, and create the bullet, place it in front of the aircraft.

List fires = new ArrayList();

public class Bullet { int x, y; Public Bullet(int a, int b) {this.x=a; this.y=b; } public int getX() {return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; Public void drawZd(Graphics g) {Image zd=new ImageIcon(" SRC /images/bullet_7.png").getimage (); g.drawImage(zd, x-19, y-80, null); }}Copy the code

Create enemy class, same as fighter class

public class Enemy { int x,y; String icon; Public Enemy(String icon) {this.icon=icon; public Enemy(String icon) {this.icon=icon; x=(int)(Math.random()*360)+10; // generate x coordinate y=-30; Public void drawPlane(Graphics g) {Image plane=new ImageIcon(icon).getimage (); g.drawImage(plane, x-15, y-15, null); } public int getX() {return x; } public void setX(int x) { this.x = x; } public int getY() { return y; } public void setY(int y) { this.y = y; }}Copy the code

Determination bullet and enemy aircraft collision method

public boolean Boom(Bullet z, Enemy d) { int zx, zy, dx, dy; boolean crash = false; // Bullet coordinates zx = z.getx (); zy = z.getY(); // dx = d.gett (); dy = d.getY(); if (dx < zx && zy - dy < 30 && dx + 30 > zx) { crash = true; } return crash; }Copy the code

Determine the method of collision between aircraft and enemy aircraft

public boolean Boom2(Enemy d, int x, int y) { int dx, dy; boolean crash = false; // dx = d.gett (); dy = d.getY(); // Set x = x-50; y = y - 80; If (dx > x+ 20&&y-dy < 20&&dx < x+ 60) {crash = true; } return crash; }Copy the code

Increase in the canvas class to monitor mouse movement method, and set to generate the bullet speed and the enemy, with the object to store the bullet, bring up the launch of a bullet from the collection objects, the disappearance of the bullet to delete the corresponding bullet objects from the collection, when the bullet collision with the enemy aircraft remove bullets and enemy aircraft, when the enemy planes collide with the aircraft, game over

Bullet movement: Bullet image in the window shows the position of the horizontal and vertical changes

List<Bullet> zd = new ArrayList<Bullet>(); @override public void mouseMoved(MouseEvent e) {x = LLDB (); // retrieve mouse coordinates y = llety (); } // Crash Crash = new Crash(); for (int j = 0; j < zd.size(); j++) { Bullet z = zd.get(j); for (int i = 0; i < dj.size(); i++) { Enemy d = dj.get(i); If (crash.Boom(z, d)) {score += 2; // dx = dj.get(I).getx (); Dj.get (I).gety (); db = true; Dj.remove (I); Zd. remove(j); // Remove bullet break from collection; }}} for (int I = 0; i < dj.size(); i++) { Enemy d = dj.get(i); if (crash.Boom2(d, x, y)) { score += 2; // dx = dj.get(I).getx (); dy = dj.get(i).getY(); db = true; dj.remove(i); // Remove enemy aircraft from collection}}}Copy the code

\