Java 吃金币游戏设计与制作,下载版后补,代码没问题


package com.swift;

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

import com.rupeng.game.GameCore;

public class SpriteCoin implements Runnable {

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

    @Override
    public void run() {
        GameCore.setGameSize(800, 345);
        GameCore.setGameTitle("用键盘操控精灵移动的小游戏");
        GameCore.loadBgView("bg.jpg");
        // 女精灵
        int spriteGirl = 0;
        GameCore.createSprite(spriteGirl, "guizi");
        GameCore.playSpriteAnimate(spriteGirl, "run", true);
        GameCore.setSpritePosition(spriteGirl, 140, 190);
        // 使用数组放置8个金币
        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 };
        //boolean[] coinIsDead = new boolean[8];
        

        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);

        // 另一种初始化数组方法 boolean[]
        boolean[] coinIsDead={false,false,false,false,false,false,false,false};
        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);
        }
        for (;;) {

            int codeNum = GameCore.getPressedKeyCode();
            Point position = GameCore.getSpritePosition(spriteGirl);
            System.out.println(position.x + " " + position.y);

            if (codeNum == KeyEvent.VK_UP || codeNum == KeyEvent.VK_W) {
                if (position.y > 135) {
                    System.out.println("up");
                    GameCore.setSpritePosition(spriteGirl, position.x, --position.y);
                    GameCore.pause(11);
                }
            }

            if (codeNum == KeyEvent.VK_DOWN || codeNum == KeyEvent.VK_S) {
                if (position.y < 190) {
                    System.out.println("down");
                    GameCore.setSpritePosition(spriteGirl, position.x, ++position.y);
                    GameCore.pause(11);
                }
            }
            if (codeNum == KeyEvent.VK_LEFT || codeNum == KeyEvent.VK_A) {
                if (position.x > 0) {
                    System.out.println("left");
                    GameCore.setSpriteFlipX(spriteGirl, true);
                    GameCore.setSpritePosition(spriteGirl, --position.x, position.y);
                    GameCore.pause(2);
                }
            }
            if (codeNum == KeyEvent.VK_RIGHT || codeNum == KeyEvent.VK_D) {
                if (position.x < 730) {
                    System.out.println("right");
                    GameCore.setSpriteFlipX(spriteGirl, false);
                    GameCore.setSpritePosition(spriteGirl, ++position.x, position.y);
                    GameCore.pause(1);
                }
            }

            Point pGirl = GameCore.getSpritePosition(spriteGirl);
            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;
                    int textNum = 0;//这句含义很大,无限循环每次到这清零,不然数字无限疯长
                    for(int j=0;j<coinNum.length;j++) {
                        if(coinIsDead[j]==true){//我把这写成了i,所以只要吃掉一个,就是8了,应该是j
                            textNum++;
                        }
                    }
                    GameCore.setText(1, Integer.toString(textNum));//这句放在循环外边,不然
                }
            }
        }

    }

}

 

 

posted @ 2017-10-21 22:05  Advancing-Swift  阅读(798)  评论(0编辑  收藏  举报