In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article is about how to pick up mooncakes on Mini Game by Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Main design
Design the game interface and implement it with swing
Design background
Design score object-moon cake, add one point when you encounter it
Design obstacles-the moon will die if you touch it.
Monitor the left and right keys of the mouse to control the movement of the basket
Design integral system
Set the resource folder to resource (which can be set in Project Manage) because you want to use the pictures inside
Function screenshot
Game start interface:
The code implements the game startup class public class Start {public static void main (String [] args) throws URISyntaxException {new EatGame ();}} core class public class EatGame extends JFrame implements ActionListener, MouseListener {private final int WIDTH = 400; private final int HEIGHT = 800; private int score; private int ticks; private boolean gameOver = false; private GameControl control; private List moons; private List cakes; private Timer timer; private ChangeE changeE / * Constructor for objects of class FallingGame * / public EatGame () throws URISyntaxException {changeE = new ChangeE (150,650, "player.jpg", WIDTH); moons = new ArrayList (); cakes = new ArrayList (); addMoonAndCake (); control = new GameControl (changeE, moons, cakes); timer = new Timer (20, this) / / add keybinds control.addAction ("Left",-20, KeyEvent.VK_LEFT); control.addAction ("Right", 20, KeyEvent.VK_RIGHT); / / add components add (control); addMouseListener (this); control.addMouseListener (this); setTitle ("eat moon cakes"); setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE) SetSize (WIDTH, HEIGHT); setResizable (false); setVisible (true); timer.start ();} / * add moon and moon cakes * / public void addMoonAndCake () {Random r = new Random (); int x, y; x = 60 + (r.nextInt (35) * 2) Y = r.nextInt (20) + moons.size () * 30; Moon moon1 = new Moon (x, y, "moon.png"); moons.add (moon1); x = 190 + r.nextInt (35) * 2; y = r.nextInt (20) + moons.size () * 30; Moon moon2 = new Moon (x, y, "moon.png"); moons.add (moon2) X = 80 + (r.nextInt (35) * 2); y = r.nextInt (20) + moons.size () * 50; Cake cake = new Cake (x, y, "cake.png"); cakes.add (cake);} / * * Update window * / private void updateFrame () {ticks++; for (int I = 0; I)
< moons.size(); i++) { Moon moon = moons.get(i); if(ticks % 25 == 0 && moon.getSpeed() < 10) { moon.setSpeed(moon.getSpeed() + 2); } } Iterator moonIterator = moons.iterator(); while (moonIterator.hasNext()) { Moon moon = moonIterator.next(); // 超出屏幕 if(moon.getY() >HEIGHT) {moonIterator.remove ();} else moon.move ();} Iterator cakeIterator = cakes.iterator (); while (cakeIterator.hasNext ()) {Cake cake = cakeIterator.next (); / / beyond the screen if (cake.getY () > HEIGHT) {cakeIterator.remove () } else cake.move ();} if (moons.size () = = 0) {addMoonAndCake ();}} private boolean checkCollision () {Rectangle rectangle = (Rectangle) changeE.getShape (); for (Moon moon: moons) {Ellipse2D circle = (Ellipse2D) moon.getShape () / / determine whether to collide with a circle if (circle.intersects (rectangle)) {gameOver = true;}} Iterator cakeIterator = cakes.iterator (); while (cakeIterator.hasNext ()) {Cake cake = cakeIterator.next (); Ellipse2D circle = (Ellipse2D) cake.getShape () If (circle.intersects (rectangle)) {score + +; / / score cakeIterator.remove ();}} return gameOver;} public void actionPerformed (ActionEvent e) {if (gameOver) {timer.stop (); control.drawEnd (control.getGraphics (), score) } else {/ / continue with game updateFrame (); checkCollision (); control.repaint ();} public void mouseClicked (MouseEvent e) {} public void mousePressed (MouseEvent e) {if (gameOver) {/ / reset game moons.clear (); control.removeAll () Control.updateUI (); score = 0; changeE.setX; changeE.setY; addMoonAndCake (); timer.start (); repaint (); gameOver = false } public void mouseReleased (MouseEvent e) {} public void mouseEntered (MouseEvent e) {} public void mouseExited (MouseEvent e) {}} Picture drawing / * Picture drawing * / public class GameControl extends JPanel {private InputMap inputMap; private ChangeE myChangeE; private List moons; private List cakes Public GameControl (ChangeE changeE, List moons, List cakes) {this.myChangeE = changeE; this.moons = moons; this.cakes = cakes; setBackground (Color.white); inputMap = getInputMap (WHEN_IN_FOCUSED_WINDOW);} @ Override protected void paintComponent (Graphics g) {super.paintComponent (g) / / draw background ImageIcon icon=new ImageIcon (Thread.currentThread (). GetContextClassLoader (). GetResource ("background.png")); Image img=icon.getImage (); g.drawImage (img, 0,0, this.getWidth (), this.getHeight (), this); / / draw player g.drawImage (myChangeE.getImage (), myChangeE.getX (), myChangeE.getY (), this) / / draw moon for (Moon moon: moons) {g.drawImage (moon.getImage (), moon.getX (), moon.getY (), this);} / / draw moon cakes for (Cake cake: cakes) {g.drawImage (cake.getImage (), cake.getX (), cake.getY (), this) }} public void drawEnd (Graphics g, int score) {g.setColor (Color.WHITE); g.fillRect (50,200,300,300); g.setColor (Color.RED); g.drawString ("Happy Mid-Autumn Festival and happy family!", 100,300); g.setColor (Color.BLUE) G.drawString ("the number of mooncakes you can eat is: + score, 100350); g.setColor (Color.BLACK); g.drawString (" mouse click replay ", 100400);} public void addAction (String name, int deltaX, int keyCode) {MoveAction moveAction = new MoveAction (name, deltaX); inputMap.put (KeyStroke.getKeyStroke (keyCode, 0), name) GetActionMap (). Put (name, moveAction);} private class MoveAction extends AbstractAction implements ActionListener {private int myDeltaX; public MoveAction (String name, int deltaX) {super (name); myDeltaX = deltaX;} public void actionPerformed (ActionEvent e) {myChangeE.move (myDeltaX);} Thank you for reading! This is the end of the article on "how to pick up mooncakes on Mini Game by Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!
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.