Mario

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

}

}

时间: 2024-10-13 05:08:54

Mario的相关文章

Super Mario

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4417 Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in

hdu-4417 Super Mario(树状数组 + 划分树)

题目链接: Super Mario Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 32768/32768 K (Java/Others) Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is

HDU4417-Super Mario

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2183    Accepted Submission(s): 1061 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

hdu_4417_Super Mario(主席树)

题目链接:hdu_4417_Super Mario 题意: 给你n个树,有m个询问,每个询问有一个区间和一个k,问你这个区间内不大于k的数有多少个. 题解: 考虑用主席树的话就比较裸,当然也可以用其他的写 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 5 const int N=1e5+7; 6 int a[N],t,n,m,hsh[N],hsh_le

HDU 4417 Super Mario (树状数组/线段树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble agai

hdu4417 Super Mario 树状数组离线/划分树

http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2720    Accepted Submission(s): 1322 Problem Description Mario is world-famous plumber

HDU 4417 Super Mario(离线线段树or树状数组)

Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss's castle as a l

hdu 4417 Super Mario(离线树状数组|划分树)

Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2584    Accepted Submission(s): 1252 Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping a

HDU-4417-Super Mario(划分树+二分)

Problem Description Mario is world-famous plumber. His "burly" figure and amazing jumping ability reminded in our memory. Now the poor princess is in trouble again and Mario needs to save his lover. We regard the road to the boss's castle as a l