码迷,mamicode.com
首页 > 其他好文 > 详细

Mario

时间:2017-02-25 00:05:42      阅读:264      评论:0      收藏:0      [点我收藏+]

标签:poi   tga   dimen   eve   time   mesi   ext   坐标   walk   

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

import com.rupeng.game.GameCore;

public class EatCoin2 implements Runnable
{

 @Override
 public void run()
 {
  // 第一步,创建8个金币,8个金币编号,1个是否被吃的boolean数组
  /*
  int[] coinXData = { 60, 91, 200, 200, 320, 214, 380, 171 };
  int[] coinYData = { 200, 144, 190, 25, 400, 150, 289, 300 };
  */
  int[] coinXData=new int[10];
  int[] coinYData=new int[10];
  for(int i=0;i<coinXData.length;i++)
  {
   int coinXData1=GameCore.rand(0, 600);
   int coinYData1=GameCore.rand(0, 400);
   coinXData[i]=coinXData1;
   coinYData[i]=coinYData1;  
   
  }
  
  
  
  int[] bombXData = new int[7];
  int[] bombYData = new int[7];
  for(int i=0;i<bombXData.length;i++)
  {
   int bX=GameCore.rand(10, 600);
   int bY=GameCore.rand(0, 500);
   bombXData[i]=bX;
   bombYData[i]=bY;
  }
  
  int[] bombNums = { 9, 10, 11, 12 ,13,14,15};
  int[] coinNums = { 1, 2, 3, 4, 5, 6, 7, 8 ,50,51};
  boolean[] isCoinDead = new boolean[8];
  // 遍历每个金币
  int leftTimeTextNum = 1;
  GameCore.createText(leftTimeTextNum, "还剩20秒");
  GameCore.setTextPosition(leftTimeTextNum, 830, 20);
  GameCore.setTextColor(leftTimeTextNum, Color.PINK);
  GameCore.setTextFontSize(leftTimeTextNum, 28);
  for (int i = 0; i < coinNums.length; i++)
  {
   int x = coinXData[i];
   int y = coinYData[i];
   int coinNum = coinNums[i];
   GameCore.createSprite(coinNum, "coin");
   GameCore.setSpritePosition(coinNum, x, y);
   GameCore.playSpriteAnimate(coinNum, "rotate", true);

  }
  for (int i = 0; i < bombNums.length; i++)
  {
   int bombX = bombXData[i];
   int bombY = bombYData[i];
   int bombNum = bombNums[i];
   GameCore.createSprite(bombNum, "bomb");
   GameCore.setSpritePosition(bombNum, bombX, bombY);
   GameCore.playSpriteAnimate(bombNum, "fire", true);

  }
  int marioNum = 0;
  GameCore.createSprite(marioNum, "mario");
  GameCore.setSpritePosition(marioNum, 0, 0);
  GameCore.playSpriteAnimate(marioNum, "walk", true);
  GameCore.setSpriteFlipX(marioNum, true);

  Dimension gameSize = GameCore.getGameSize();
  int gameWidth = gameSize.width;
  int gameHeight = gameSize.height - 20;
  Dimension marioSize = GameCore.getSpriteSize(marioNum);
  int marioWidth = marioSize.width;
  int marioHeight = marioSize.height;

  int bigCoinNum = 0;
  int coinCountText = 0;
  GameCore.createImage(bigCoinNum, "coin.png");
  GameCore.setImagePosition(bigCoinNum, 700, 0);
  GameCore.createText(coinCountText, "X0");
  GameCore.setTextPosition(coinCountText, 750, 0);
  GameCore.setTextFontSize(coinCountText, 50);
  GameCore.setTextColor(coinCountText, Color.red);

  long startMills = System.currentTimeMillis();

  while (true)
  {
   GameCore.loadBgView("大片草地.png");
   int kc = GameCore.getPressedKeyCode();
   Point marioPos1 = GameCore.getSpritePosition(marioNum);
   int marioX1 = marioPos1.x;
   int marioY1 = marioPos1.y;
   if (kc == KeyEvent.VK_LEFT)
   {
    if (marioX1 > 0)
    {
     GameCore.setSpriteFlipX(marioNum, false);
     GameCore.setSpritePosition(marioNum, marioX1 - 1, marioY1);
    }
   } else if (kc == KeyEvent.VK_RIGHT)
   {
    if (marioX1 < gameWidth - marioWidth)
    {
     GameCore.setSpriteFlipX(marioNum, true);
     GameCore.setSpritePosition(marioNum, marioX1 + 1, marioY1);
    }
   } else if (kc == KeyEvent.VK_UP)
   {
    if (marioY1 > 0)
    {
     GameCore.setSpritePosition(marioNum, marioX1, marioY1 - 1);
    }
   } else if (kc == KeyEvent.VK_DOWN)
   {
    if (marioY1 < gameHeight - marioHeight)
    {
     GameCore.setSpritePosition(marioNum, marioX1, marioY1 + 1);
    }
   } // 遍历是否被吃的每个值

   for (int i = 0; i < isCoinDead.length; i++)
   {
    int coinNum = coinNums[i];
    Dimension coinSize = GameCore.getSpriteSize(coinNum);
    int coinWidth = coinSize.width;
    int coinHeight = coinSize.height;
    // mario中心点坐标,金币中心点坐标。
    Point marioPos = GameCore.getSpritePosition(marioNum);
    int marioX = marioPos.x;
    int marioY = marioPos.y;
    int marioXCenter = marioX + marioWidth / 2;
    int marioYCenter = marioY + marioHeight / 2;

    int coinXCenter = coinXData[i] + coinWidth / 2;
    int coinYcenter = coinYData[i] + coinHeight / 2;

    double distant = Math.pow((marioXCenter - coinXCenter) * (marioXCenter - coinXCenter)
      + (marioYCenter - coinYcenter) * (marioYCenter - coinYcenter), 0.5);
    int eatCount = 0;

    if (distant < 15)
    {
     GameCore.hideSprite(coinNum);
     GameCore.pause(10);
     isCoinDead[i] = true;
    }
    for (int j = 0; j < isCoinDead.length; j++)
    {
     if (isCoinDead[j])
     {
      eatCount++;
     }

    }
    GameCore.setText(coinCountText, "X" + eatCount);

   }

   for (int i = 0; i < bombNums.length; i++)
   {
    int coinNum = coinNums[i];
    Point marioPos = GameCore.getSpritePosition(marioNum);
    int marioX = marioPos.x;
    int marioY = marioPos.y;
    int marioXCenter = marioX + marioWidth / 2;
    int marioYCenter = marioY + marioHeight / 2;
    int bombNum = bombNums[i];
    Dimension bombSize = GameCore.getSpriteSize(bombNum);
    int bombWidth = bombSize.width;
    int bombHeight = bombSize.height;

    int bombXCenter = bombXData[i] + bombWidth / 2;
    int bombYCenter = bombYData[i] + bombHeight / 2;
    double distant1 = Math.pow((marioXCenter - bombXCenter) * (marioXCenter - bombXCenter)
      + (marioYCenter - bombYCenter) * (marioYCenter - bombYCenter), 0.5);
    if (distant1 < 15)
    {
     GameCore.hideSprite(marioNum);
     GameCore.pause(2000);
     GameCore.exit();

    }

   }

   long currentMills = System.currentTimeMillis();
   long leftSeconds = 20 - (currentMills - startMills) / 1000;
   GameCore.setText(leftTimeTextNum, "还剩" + leftSeconds + "秒");
   if (leftSeconds < 10)
   {
    GameCore.setTextColor(leftTimeTextNum, leftSeconds % 2 == 0 ? Color.RED : Color.GREEN);
    if (leftSeconds == 1)
    {
     GameCore.exit();
    }

   }

  }

 }

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

 }

}

Mario

标签:poi   tga   dimen   eve   time   mesi   ext   坐标   walk   

原文地址:http://www.cnblogs.com/RichardRabbit/p/6440550.html

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