• 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));//这句放在循环外边,不然
                    }
                }
            }
    
        }
    
    }

  • 相关阅读:
    electron创建窗口常用配置参数
    Node.js读取文件相对路径写法注意
    计算机系统安装及相关知识
    U盘无法拷贝大于4G的文件解决办法汇总
    PE盘制作
    不同数据库连接四要素总结
    如何将打印内容转换为bmp位图文件
    DPI的理解
    jmeter 函数助手里的P,property的使用
    jmeter的Include Controller控件和Test Fragment控件和Module Controller控件
  • 原文地址:https://www.cnblogs.com/qingyundian/p/7707110.html
Copyright © 2020-2023  润新知