Game gold coin version 2.0 new bomb function new game countdown function new victory failure detection function using the rupeng game engine to create a form one-dimensional array set gold coins,

Source: Internet
Author: User

Game gold coin version 2.0 new bomb function new game countdown function new victory failure detection function using the rupeng game engine to create a form one-dimensional array set gold coins,

The game code is as follows:

package com.swift;

import java.awt.Color;
import java.awt.Point;
import java.awt.event.KeyEvent;

import com.rupeng.game.GameCore;
/ **
 * @author swift
 * @version 2.0
 * @category added a bomb function, if the elf eats a bomb and immediately dies, the game ends;
 * @category added game countdown function, countdown 60 seconds
 * @category added a game victory and failure function. After eating gold coins, you win, and you lose the bomb
 * /
public class Coin2 implements Runnable {

    public static void main (String [] args) {
        GameCore.start (new Coin2 ());
    }

    @Override
    public void run () {
        // Set the form size, title, background
        GameCore.setGameSize (800, 345);
        GameCore.setGameTitle ("a small game that uses the keyboard to control the movement of the wizard");
        GameCore.loadBgView ("bg.jpg");
        // Set the appearance and appearance of female elves
        int spriteGirl = 0;
        GameCore.createSprite (spriteGirl, "guizi");
        GameCore.playSpriteAnimate (spriteGirl, "run", true);
        GameCore.setSpritePosition (spriteGirl, 140, 190);
        // Use the array to place the number, abscissa, and ordinate of 8 gold coins
        int [] coinNum = {1, 2, 3, 4, 5, 6, 7, 8};
        int [] coinXDate = {100, 200, 300, 400, 500, 600, 700, 730};
        int [] coinYDate = {140, 180, 150, 190, 140, 170, 160, 140};
        // Use the array to place the number, abscissa and ordinate of 3 bombs
        int [] bombNum = {9, 10, 11};
        int [] bombXDate = {250, 450, 650};
        int [] bombYDate = {160, 160, 190};
        // boolean [] coinIsDead = new boolean [8];
        
        // Set the gold coin picture displayed in the upper right corner and the number of gold coins eaten
        GameCore.createImage (0);
        GameCore.setImageSource (0, "bigCoin.png");
        GameCore.setImagePosition (0, 710, 20);
        GameCore.createText (0, "NUM");
        GameCore.setTextColor (0, Color.WHITE);
        GameCore.setTextPosition (0, 650, 25);
        GameCore.createText (1, "0");
        GameCore.setTextPosition (1, 760, 25);
        GameCore.setTextColor (1, Color.WHITE);
        
        // Set the time countdown displayed in the upper left corner
        GameCore.createText (3, "Time-Left");
        GameCore.setTextColor (3, Color.WHITE);
        GameCore.setTextFontSize (3, 20);
        GameCore.setTextPosition (3, 25, 5);
        GameCore.createText (4, "60");
        GameCore.setTextPosition (4,60, 25);
        GameCore.setTextColor (4, Color.WHITE);
        GameCore.setTextFontSize (4, 28);
        
        // Set the time to start the game countdown
        long timeBegin = System.currentTimeMillis ();

        // Set the gold coin to be alive before eating it. Another method to initialize the array boolean []
        boolean [] coinIsDead = {false, false, false, false, false, false, false, false};
        // Number of deaths of gold coin elves
        int textNum = 0;
        // Set the appearance of the gold coin sprite in the game according to the horizontal and vertical coordinates
        for (int i = 0; i <coinNum.length; i ++) {
            GameCore.createSprite (coinNum [i], "coin");
            GameCore.setSpritePosition (coinNum [i], coinXDate [i], coinYDate [i]);
            GameCore.playSpriteAnimate (coinNum [i], "rotate", true);
        }
        // Set the appearance of the bomb sprite in the game according to the horizontal and vertical coordinates
        for (int i = 0; i <bombNum.length; i ++) {
            GameCore.createSprite (bombNum [i], "bomb");
            GameCore.playSpriteAnimate (bombNum [i], "laser", true);
            GameCore.setSpritePosition (bombNum [i], bombXDate [i], bombYDate [i]);
        }
        // Create a new explosion sprite and wait for the trigger
        int newBombNum = 12;
        GameCore.createSprite (newBombNum, "bomb");
        
        // Create text when winning or losing, wait for trigger
        int newTextNum = 2;
        
        // Get infinite keyboard information in an infinite loop, change the position of female elf
        for (;;) {
            int codeNum = GameCore.getPressedKeyCode ();
            Point position = GameCore.getSpritePosition (spriteGirl);
            // If you press the up or W key to move up and set the upper boundary to 135
            if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
                if (position.y> 135) {
                    GameCore.setSpritePosition (spriteGirl, position.x, --position.y);
                    GameCore.pause (1);
                }
            }
            // If you press the down or S key to move down and set the lower boundary to 190
            if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
                if (position.y <190) {
                    GameCore.setSpritePosition (spriteGirl, position.x, ++ position.y);
                    GameCore.pause (1);
                }
            }
            // If you press the left or A key to move to the left and set the left border to 0
            if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
                if (position.x> 0) {
                    GameCore.setSpriteFlipX (spriteGirl, true);
                    GameCore.setSpritePosition (spriteGirl, --position.x, position.y);
                    GameCore.pause (2);
                }
            }
            // If you press the right or D key to move to the right and set the right border to 730
            if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
                if (position.x <730) {
                    GameCore.setSpriteFlipX (spriteGirl, false);
                    GameCore.setSpritePosition (spriteGirl, ++ position.x, position.y);
                    GameCore.pause (1);
                }
            }
            // Get the current female elf position
            Point pGirl = GameCore.getSpritePosition (spriteGirl);
            // Judge the distance between the female elf and any gold coin, close enough to disappear, and set the gold coin is dead
            for (int i = 0; i <coinNum.length; i ++) {
                if (coinIsDead [i]) {
                    continue;
                }
                Point pCoin = GameCore.getSpritePosition (coinNum [i]);

                double distance = Math.sqrt ((Math.pow ((pCoin.x-pGirl.x), 2) + Math.pow ((pCoin.y-pGirl.y), 2)));
                if (distance <30) {
                    GameCore.hideSprite (coinNum [i]);
                    coinIsDead [i] = true;
                    textNum = 0; // This sentence has a great meaning, and it is cleared every time in this infinite loop, otherwise the number is infinitely crazy
                    for (int j = 0; j <coinNum.length; j ++) {
                        if (coinIsDead [j] == true) {// I wrote this as i, so as long as you eat one, it is 8, it should be j
                            textNum ++;
                        }
                    }
                    GameCore.setText (1, Integer.toString (textNum)); // This sentence is placed outside the loop, otherwise
                }
                if (textNum == 8) {
                    // output text
                    GameCore.createText (newTextNum, "You Win");
                    GameCore.setTextPosition (newTextNum, 300, 150);
                    GameCore.setTextColor (newTextNum, Color.RED);
                    GameCore.setTextFontSize (newTextNum, 88);
                    GameCore.pause (3000);
                    GameCore.exit ();
                    
                }
            }
            // Judge the distance between the female elf and any bomb, close enough to explode the bomb, the game fails
            for (int i = 0; i <bombNum.length; i ++) {
                Point pBomb = GameCore.getSpritePosition (bombNum [i]);
                
                double distance = Math.sqrt ((Math.pow ((pBomb.x-pGirl.x), 2) + Math.pow ((pBomb.y-pGirl.y), 2)));
                if (distance <20) {
                    // Hide the original bomb elf
                    GameCore.hideSprite (bombNum [i]);
                    // A new explosion sprite appears
                    GameCore.playSpriteAnimate (newBombNum, "fire", true);
                    // A new bomb appears at the position of the original bomb
                    GameCore.setSpritePosition (newBombNum, pBomb.x, pBomb.y);
                    GameCore.pause (3000);
                    // output text
                    GameCore.createText (newTextNum, "You Lose");
                    GameCore.setTextPosition (newTextNum, 300, 150);
                    GameCore.setTextColor (newTextNum, Color.RED);
                    GameCore.setTextFontSize (newTextNum, 88);
                    GameCore.pause (2500);
                    GameCore.exit ();
                }
            }
            // Set the end time of the game countdown
            long timeEnd = System.currentTimeMillis ();
            int timeText = (int) (60- (timeEnd-timeBegin) / 1000);
            GameCore.setText (4, Integer.toString (timeText));
            if (timeText == 0) {
                GameCore.alert ("The time to clear the game is over and it will end soon.");
                GameCore.exit ();
            }
        }

    }
}
(1) New bomb function, if the elf dies immediately after eating the bomb, the game ends;
(2) New game countdown function, countdown 60 seconds
(3) Added a game victory and failure function. After eating gold coins, you win, and you lose the bomb

Game package and materials:
https://pan.baidu.com/s/1jHI54Po

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.