<> Above

<> Write on the front

Technology comes from sharing , So take time to use it today java Do a little game finishing paste out for your reference .java It's really not suitable for writing desktop applications , Here is just to let you understand through this game oop The process of object oriented programming , It's just entertainment . The code is very simple , And it's easy to understand , And the notes are very clear , There are still problems , Make up lessons in private .

<> Complete code

<> Enemy aircraft
import java.util.Random; Enemy aircraft : It's a flying object , It's also the enemy public class Airplane extends
FlyingObject implements Enemy { private int speed = 3; // Move steps /** Initialization data */
public Airplane(){ this.image = ShootGame.airplane; width = image.getWidth();
height= image.getHeight(); y = -height; Random rand = new Random(); x = rand.
nextInt(ShootGame.WIDTH - width); } /** Score */ @Override public int getScore()
{ return 5; } /** // Cross border treatment */ @Override public boolean outOfBounds() { return y>
ShootGame.HEIGHT; } /** move */ @Override public void step() { y += speed; } }
<> Bonus points
/** * reward */ public interface Award { int DOUBLE_FIRE = 0; // Double fire int LIFE = 1;
//1 Life /** Reward type ( above 0 or 1) */ int getType(); }
<> honeybee
import java.util.Random; /** honeybee */ public class Bee extends FlyingObject
implements Award{ private int xSpeed = 1; //x Coordinate moving speed private int ySpeed = 2;
//y Coordinate moving speed private int awardType; // Reward type /** Initialization data */ public Bee(){ this.image =
ShootGame.bee; width = image.getWidth(); height = image.getHeight(); y = -height
; Random rand = new Random(); x = rand.nextInt(ShootGame.WIDTH - width);
awardType= rand.nextInt(2); // Reward at initialization } /** Reward type */ public int getType(){
return awardType; } /** Cross border treatment */ @Override public boolean outOfBounds() { return
y>ShootGame.HEIGHT; } /** move , It can fly sideways */ @Override public void step() { x += xSpeed;
y+= ySpeed; if(x > ShootGame.WIDTH-width){ xSpeed = -1; } if(x < 0){ xSpeed = 1
; } } }
<> ZiDan : It's a flying object
/** * ZiDan : It's a flying object */ public class Bullet extends FlyingObject { private int speed
= 3; // Speed of movement /** Initialization data */ public Bullet(int x,int y){ this.x = x; this.y = y;
this.image = ShootGame.bullet; } /** move */ @Override public void step(){ y-=
speed; } /** Cross border treatment */ @Override public boolean outOfBounds() { return y<-height;
} }
<> The enemy's score
/** * enemy , There can be scores */ public interface Enemy { /** The enemy's score */ int getScore(); }
<> Flying object ( enemy airplane , honeybee , bullet , Hero machine )
import java.awt.image.BufferedImage; /** * Flying object ( enemy airplane , honeybee , bullet , Hero machine ) */ public abstract
class FlyingObject { protected int x; //x coordinate protected int y; //y coordinate protected int
width; // wide protected int height; // high protected BufferedImage image; // picture 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 int getWidth() {
return width; } public void setWidth(int width) { this.width = width; } public
int getHeight() { return height; } public void setHeight(int height) { this.
height= height; } public BufferedImage getImage() { return image; } public void
setImage(BufferedImage image) { this.image = image; } /** * Check for out of bounds * @return
true Out of bounds or not */ public abstract boolean outOfBounds(); /** * The flying object moves one step */ public
abstract void step(); /** * Check whether the current flying object is damaged by bullets (x,y) hit (shoot) in * @param Bullet Bullet target *
@return true It means you've been hit */ public boolean shootBy(Bullet bullet){ int x = bullet.x;
// Bullet abscissa int y = bullet.y; // Bullet ordinate return this.x<x && x<this.x+width && this.y<y
&& y<this.y+height; } }
<> Hero machine
import java.awt.image.BufferedImage; /** * Hero machine : It's a flying object */ public class Hero
extends FlyingObject{ private BufferedImage[] images = {}; // Hero image private int
index= 0; // Hero image switch index private int doubleFire; // Double fire private int life; // life /**
Initialization data */ public Hero(){ life = 3; // initial 3 Life doubleFire = 0; // The initial firepower is 0 images =
new BufferedImage[]{ShootGame.hero0, ShootGame.hero1}; // Hero image array image =
ShootGame.hero0; // Initially hero0 picture width = image.getWidth(); height = image.
getHeight(); x = 150; y = 400; } /** Gain double firepower */ public int isDoubleFire() {
return doubleFire; } /** Set double fire */ public void setDoubleFire(int doubleFire) {
this.doubleFire = doubleFire; } /** Increase firepower */ public void addDoubleFire(){
doubleFire= 40; } /** Increase life */ public void addLife(){ // Increase life life++; } /** Life reduction */
public void subtractLife(){ // Life reduction life--; } /** Gain life */ public int getLife(){
return life; } /** The current object moved a little , Relative distance ,x,y Mouse position */ public void moveTo(int x,int y){
this.x = x - width/2; this.y = y - height/2; } /** Cross border treatment */ @Override public
boolean outOfBounds() { return false; } /** Fire a bullet */ public Bullet[] shoot(){ int
xStep= width/4; //4 half int yStep = 20; // step if(doubleFire>0){ // Double fire Bullet[]
bullets= new Bullet[2]; bullets[0] = new Bullet(x+xStep,y-yStep);
//y-yStep( The location of the bullet from the plane ) bullets[1] = new Bullet(x+3*xStep,y-yStep); return bullets;
}else{ // Single firepower Bullet[] bullets = new Bullet[1]; bullets[0] = new Bullet(x+2*
xStep,y-yStep); return bullets; } } /** move */ @Override public void step() { if(
images.length>0){ image = images[index++/10%images.length]; // Switch pictures hero0,hero1 }
} /** Collision algorithm */ public boolean hit(FlyingObject other){ int x1 = other.x - this.
width/2; //x Minimum coordinate distance int x2 = other.x + this.width/2 + other.width; //x Maximum coordinate distance int
y1= other.y - this.height/2; //y Minimum coordinate distance int y2 = other.y + this.height/2 + other.
height; //y Maximum coordinate distance int herox = this.x + this.width/2; // Hero machine x Distance of coordinate center point int heroy =
this.y + this.height/2; // Hero machine y Distance of coordinate center point return herox>x1 && herox<x2 && heroy>y1 &&
heroy<y2; // It's a collision within the range } }
<> Game start main class
import java.awt.Font; import java.awt.Color; import java.awt.Graphics; import
java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.
Arrays; import java.util.Random; import java.util.Timer; import java.util.
TimerTask; import java.awt.image.BufferedImage; import javax.imageio.ImageIO;
import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.
JPanel; /** * */ public class ShootGame extends JPanel { public static final int
WIDTH= 400; // Panel width public static final int HEIGHT = 654; // Panel height /** Current state of the game :
START RUNNING PAUSE GAME_OVER */ private int state; private static final int
START= 0; private static final int RUNNING = 1; private static final int PAUSE =
2; private static final int GAME_OVER = 3; private int score = 0; // score private
Timer timer; // timer private int intervel = 1000 / 100; // time interval ( millisecond ) public static
BufferedImage background; public static BufferedImage start; public static
BufferedImage airplane; public static BufferedImage bee; public static
BufferedImage bullet; public static BufferedImage hero0; public static
BufferedImage hero1; public static BufferedImage pause; public static
BufferedImage gameover; private FlyingObject[] flyings = {}; // Enemy aircraft array private
Bullet[] bullets = {}; // Bullet array private Hero hero = new Hero(); // Hero machine static {
// Static code block , Initialize image resources try { background = ImageIO.read(ShootGame.class .getResource(
"background.png")); start = ImageIO.read(ShootGame.class.getResource("start.png"
)); airplane = ImageIO .read(ShootGame.class.getResource("airplane.png")); bee =
ImageIO.read(ShootGame.class.getResource("bee.png")); bullet = ImageIO.read(
ShootGame.class.getResource("bullet.png")); hero0 = ImageIO.read(ShootGame.class
.getResource("hero0.png")); hero1 = ImageIO.read(ShootGame.class.getResource(
"hero1.png")); pause = ImageIO.read(ShootGame.class.getResource("pause.png"));
gameover= ImageIO .read(ShootGame.class.getResource("gameover.png")); } catch (
Exception e) { e.printStackTrace(); } } /** painting */ @Override public void paint(
Graphics g) { g.drawImage(background, 0, 0, null); // Draw the background picture paintHero(g); // Hero drawing machine
paintBullets(g); // Draw bullets paintFlyingObjects(g); // Painting flying objects paintScore(g); // Draw score
paintState(g); // Draw game state } /** Hero drawing machine */ public void paintHero(Graphics g) { g.
drawImage(hero.getImage(), hero.getX(), hero.getY(), null); } /** Draw bullets */ public
void paintBullets(Graphics g) { for (int i = 0; i < bullets.length; i++) {
Bullet b= bullets[i]; g.drawImage(b.getImage(), b.getX() - b.getWidth() / 2, b.
getY(), null); } } /** Painting flying objects */ public void paintFlyingObjects(Graphics g) { for
(int i = 0; i < flyings.length; i++) { FlyingObject f = flyings[i]; g.drawImage(
f.getImage(), f.getX(), f.getY(), null); } } /** Draw score */ public void paintScore(
Graphics g) { int x = 10; // x coordinate int y = 25; // y coordinate Font font = new Font(Font.
SANS_SERIF, Font.BOLD, 22); // typeface g.setColor(new Color(0xFF0000)); g.setFont(
font); // Set font g.drawString("SCORE:" + score, x, y); // Draw score y=y+20; // y Coordinate increment 20
g.drawString("LIFE:" + hero.getLife(), x, y); // Draw life } /** Draw game state */ public void
paintState(Graphics g) { switch (state) { case START: // Startup status g.drawImage(start
, 0, 0, null); break; case PAUSE: // Pause state g.drawImage(pause, 0, 0, null); break
; case GAME_OVER: // Game termination status g.drawImage(gameover, 0, 0, null); break; } }
public static void main(String[] args) { JFrame frame = new JFrame("Fly");
ShootGame game= new ShootGame(); // Panel objects frame.add(game); // Add panel to JFrame in
frame.setSize(WIDTH, HEIGHT); // Set size frame.setAlwaysOnTop(true); // Set it always on top
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Default close operation frame.
setIconImage(new ImageIcon("images/icon.jpg").getImage()); // Set the icon for the form frame.
setLocationRelativeTo(null); // Set the initial position of the form frame.setVisible(true); // Call as soon as possible paint
game.action(); // Start execution } /** Start execution code */ public void action() { // Mouse monitoring events
MouseAdapter l= new MouseAdapter() { @Override public void mouseMoved(
MouseEvent e) { // Mouse movement if (state == RUNNING) { // Mobile hero machine in running state -- With mouse position int x = e.
getX(); int y = e.getY(); hero.moveTo(x, y); } } @Override public void
mouseEntered(MouseEvent e) { // Mouse in if (state == PAUSE) { // Running in pause state state =
RUNNING; } } @Override public void mouseExited(MouseEvent e) { // Mouse exit if (state
== RUNNING) { // The game is not over , Set it to pause state = PAUSE; } } @Override public void
mouseClicked(MouseEvent e) { // Mouse click switch (state) { case START: state =
RUNNING; // Running in startup state break; case GAME_OVER: // game over , Clean up the scene flyings = new
FlyingObject[0]; // Clear the flying objects bullets = new Bullet[0]; // Clear the bullet hero = new Hero();
// Re create hero machine score = 0; // Clear the score state = START; // The status is set to start break; } } }; this.
addMouseListener(l); // Handling mouse click operations this.addMouseMotionListener(l); // Handle mouse swipe
timer= new Timer(); // Main flow control timer.schedule(new TimerTask() { @Override public
void run() { if (state == RUNNING) { // running state enterAction(); // Entry of flying objects stepAction(
); // Take a step shootAction(); // Hero shooting bangAction(); // The bullet hit the flying object outOfBoundsAction();
// Delete flying objects and bullets checkGameOverAction(); // Check the end of the game } repaint(); // Repaint , call paint() method }
}, intervel, intervel); } int flyEnteredIndex = 0; // Entry count of flying objects /** Entry of flying objects */
public void enterAction() { flyEnteredIndex++; if (flyEnteredIndex % 40 == 0) {
// 400 Generate a flyer in milliseconds --10*40 FlyingObject obj = nextOne(); // Randomly generate a flying object flyings =
Arrays.copyOf(flyings, flyings.length + 1); flyings[flyings.length - 1] = obj; }
} /** Take a step */ public void stepAction() { for (int i = 0; i < flyings.length; i++)
{ // Flying objects take a step FlyingObject f = flyings[i]; f.step(); } for (int i = 0; i <
bullets.length; i++) { // Take a step Bullet b = bullets[i]; b.step(); } hero.step();
// Take a step } /** Flying objects take a step */ public void flyingStepAction() { for (int i = 0; i <
flyings.length; i++) { FlyingObject f = flyings[i]; f.step(); } } int shootIndex
= 0; // Shooting count /** Shooting */ public void shootAction() { shootIndex++; if (shootIndex
% 30 == 0) { // 300 One in a millisecond Bullet[] bs = hero.shoot(); // The hero fired a bullet bullets =
Arrays.copyOf(bullets, bullets.length + bs.length); // Expansion System.arraycopy(bs,
0, bullets, bullets.length - bs.length, bs.length); // Append array } } /** Bullet and flying object collision detection
*/ public void bangAction() { for (int i = 0; i < bullets.length; i++) { //
Go through all the bullets Bullet b = bullets[i]; bang(b); // Collision check between bullet and flying object } } /** Delete flying objects and bullets */
public void outOfBoundsAction() { int index = 0; // Indexes FlyingObject[]
flyingLives= new FlyingObject[flyings.length]; // A living flying object for (int i = 0; i <
flyings.length; i++) { FlyingObject f = flyings[i]; if (!f.outOfBounds()) {
flyingLives[index++] = f; // Keep those that don't cross the line } } flyings = Arrays.copyOf(flyingLives,
index); // Keep all flying objects that do not cross the boundary index = 0; // Index reset to 0 Bullet[] bulletLives = new Bullet[
bullets.length]; for (int i = 0; i < bullets.length; i++) { Bullet b = bullets[i
]; if (!b.outOfBounds()) { bulletLives[index++] = b; } } bullets = Arrays.copyOf
(bulletLives, index); // Keep the bullets that don't cross the line } /** Check the end of the game */ public void
checkGameOverAction() { if (isGameOver()==true) { state = GAME_OVER; // Change state } }
/** Check if the game is over */ public boolean isGameOver() { for (int i = 0; i < flyings.length
; i++) { int index = -1; FlyingObject obj = flyings[i]; if (hero.hit(obj)) { //
Check whether the hero plane collides with the flying object hero.subtractLife(); // Life reduction hero.setDoubleFire(0); // Double fire is off
index= i; // Index of flying objects encountered } if (index != -1) { FlyingObject t = flyings[index];
flyings[index] = flyings[flyings.length - 1]; flyings[flyings.length - 1] = t;
// Exchange with the last flyer flyings = Arrays.copyOf(flyings, flyings.length - 1); //
Delete flying objects } } return hero.getLife() <= 0; } /** Collision check between bullet and flying object */ public void bang
(Bullet bullet) { int index = -1; // Hit flyer index for (int i = 0; i < flyings.length
; i++) { FlyingObject obj = flyings[i]; if (obj.shootBy(bullet)) { // Judge whether to hit or not
index= i; // Index to record the flying object hit break; } } if (index != -1) { // There are flying objects hit
FlyingObject one= flyings[index]; // Record the flying object hit FlyingObject temp = flyings[
index]; // The hit flyer is exchanged with the last flyer flyings[index] = flyings[flyings.length - 1];
flyings[flyings.length - 1] = temp; flyings = Arrays.copyOf(flyings, flyings.
length- 1); // Delete the last flyer ( That's the one who was hit ) // inspect one Types of ( Enemy bonus points , Reward acquisition ) if (one instanceof Enemy)
{ // Inspection type , It's the enemy , Points will be added Enemy e = (Enemy) one; // Force type conversion score += e.getScore(); //
Bonus points } else { // If it is a reward , Set up rewards Award a = (Award) one; int type = a.getType(); //
Get reward type switch (type) { case Award.DOUBLE_FIRE: hero.addDoubleFire(); // Set double fire
break; case Award.LIFE: hero.addLife(); // Set up life adding break; } } } } /** * Randomly generated flying object *
* @return Flying object */ public static FlyingObject nextOne() { Random random = new
Random(); int type = random.nextInt(20); // [0,20) if (type < 4) { return new
Bee(); } else { return new Airplane(); } } }

Technology
©2019-2020 Toolsou All rights reserved,
What is a process , Concept of process ? Tang Seng team to lay off staff , Who will you lay off ?Oracle Database access performance optimization JVM Old age garbage collection Full GCweb Two front-end practical games ( Attached source code ) about linux command “iptables -F”, Don't carry it out easily navicat function sql File error 2021-06-03 A man is not born to be defeated Pandas And openpyxl Super combination of Library , bye ,Excel! It is never recommended to spend a lot of time studying for employment python