Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Java realizes code sharing on on-screen comment Mini Game

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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)

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report