Java realizes code sharing on on-screen comment Mini Game
This article introduces the knowledge of "java to achieve on-screen Mini Game code sharing". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Parent class
Import java.awt.*;public class GameObject {/ / the parent class of the game object, Image img; double xPowery; int speed = 3; int width,height; public void drawSelf (Graphics g) {g.drawImage (img, (int) x, (int) y public GameObject);} public GameObject (Image img, double x, double y, int XspeedQuint Y_speed, int width, int height) {this.img = img; this.x = x; this.y = y; this.speed = speed; this.width = width This.height = height;} public GameObject (Image img, double x, double y) {this.img = img; this.x = x; this.y = y;} public GameObject () {} / / returns the rectangle where the object is located, which is convenient for subsequent collision detection public Rectangle getRect () {return new Rectangle ((int) x, (int) ymoving widthdepartment height);}
On-screen comment
Import com.sun.xml.internal.ws.model.wsdl.WSDLPortProperties;import java.awt.*;public class Shell extends GameObject {double degree; public Shell () {x = 200; y = 200; width = 10; height = 10; speed = 2; speed = 2; / / Radian degree = Math.random () * Math.PI*2;} public void draw (Graphics g) {Color c = g.getColor (); g.setColor (Color.YELLOW); g.fillOval ((int) x, (int) / / shells fly along any angle x+=speed*Math.cos (degree); y+=speed*Math.sin (degree); if (x Constant.GAME_WIDTH-width) {degree = Math.PI-degree;} if (y Constant.GAME_HEIGHT-height) {degree =-degree;} g.setColor (c);}}
Aircraft class
Import java.awt.*;import java.awt.event.KeyAdapter;import java.awt.event.KeyEvent;public class Plane extends GameObject {boolean left,right,up,down; boolean live = true; / / whether the plane is alive or not these Boolean values determine the state when touching the upper and lower left and right borders / / if you draw public void drawSelf (Graphics g) {if (live) {g.drawImage (img, (int) x, (int) y, null) / move if (left) {x-= speed;} if (right) {x + = speed;} if (up) {y-= speed;} if (down) {y + = speed;} public Plane (Image img,double x camera double y) {this.img = img; this.x = x; this.y = y; this.speed = 3; this.width = img.getWidth (null); this.height = img.getHeight (null) according to the direction } / / Press a key to increase the corresponding direction public void addDirection (KeyEvent e) {switch (e.getKeyCode ()) {case KeyEvent.VK_LEFT: left = true; break; case KeyEvent.VK_UP: up = true; break; case KeyEvent.VK_RIGHT: right = true; break; case KeyEvent.VK_DOWN: down = true; break Press a key to cancel the corresponding direction public void minusDirection (KeyEvent e) {switch (e.getKeyCode ()) {case KeyEvent.VK_LEFT: left = false; break; case KeyEvent.VK_UP: up = false; break; case KeyEvent.VK_RIGHT: right = false; break; case KeyEvent.VK_DOWN: down = false; break;}
Explosion type
Import java.awt.*;// explosion type public class Explode {double xonomy; / / the location of the explosion static Image [] imgs = new Image [16]; static {for (int item0witi)