码迷,mamicode.com
首页 > 编程语言 > 详细

Java 吃金币游戏设计与制作

时间:2017-10-22 00:13:21      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:nis   main   ble   source   ttext   position   epo   jpg   rup   

package com.swift;

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

import com.rupeng.game.GameCore;

public class PlayWithKey implements Runnable {

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

    @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, 760 };
        int[] coinYDate = { 140, 180, 150, 190, 140, 170, 160, 140 };
        boolean[] coinIsDead = new boolean[8];
        int textNum=0;
        
        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, Integer.toString(textNum));
        GameCore.setTextPosition(1, 760, 25);
        GameCore.setTextColor(1, Color.WHITE);
        
        // 另一种初始化数组方法 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);
                }
            }
            
            for (int i = 0; i < coinNum.length; i++) {
                
                Point pCoin=GameCore.getSpritePosition(coinNum[i]);
                Point pGirl=GameCore.getSpritePosition(spriteGirl);
                
                double distance=Math.sqrt((Math.pow((pCoin.x-pGirl.x),2)+Math.pow((pCoin.y-pGirl.y),2)));
                System.out.println(distance);
                if (distance<30) {
                    coinIsDead[i]=true;
                }
                if(coinIsDead[i]==true) {
                    System.out.println(coinNum[i]);
                    GameCore.hideSprite(coinNum[i]);
                    //textNum++;
                    //GameCore.setText(1, Integer.toString(textNum));
                }
            }
        }
        // GameCore.pause(10000);

    }

}

技术分享

 

Java 吃金币游戏设计与制作

标签:nis   main   ble   source   ttext   position   epo   jpg   rup   

原文地址:http://www.cnblogs.com/qingyundian/p/7707110.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!