Skip to content

Instantly share code, notes, and snippets.

@sephto
Created March 26, 2013 02:45
Show Gist options
  • Save sephto/5242720 to your computer and use it in GitHub Desktop.
Save sephto/5242720 to your computer and use it in GitHub Desktop.
package com.badlogic.androidgames.mrnom;
public class Meteor {
public int x, y;
public Meteor(int x, int y) {
this.x = x;
this.y = y;
}
}
public class World {
static final int WORLD_WIDTH = 75;
static final int WORLD_HEIGHT = 114;
static final int SCORE_INCREMENT = 10;
static final float TICK_INITIAL = 0.1f;
static final float TICK_DECREMENT = 0.05f;
public Astronaut astro;
Meteor meteor []= new Meteor[1000];
public Coins coins;
public boolean gameOver = false;;
public int score = 0;
boolean fields[][] = new boolean[WORLD_WIDTH][WORLD_HEIGHT];
Random random = new Random();
float tickTime = 0;
float tick = TICK_INITIAL;
int pUpX = 0;
int pUpY = 0;
boolean moved = false;
int xSpeed = 0;
int ySpeed = 0;
int coinCount = 0;
public World() {
astro = new Astronaut();
placeCoin();
spawnMeteor();
}
// places coin in random place
private void placeCoin() {
String debug;
for (int x = 0; x < WORLD_WIDTH; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
fields[x][y] = false;
}
}
fields[pUpX][pUpY] = true;
pUpX = random.nextInt(WORLD_WIDTH);
pUpY = random.nextInt(WORLD_HEIGHT);
while (true) {
if (fields[pUpX][pUpY] == false)
break;
if (pUpX >= WORLD_WIDTH) {
pUpX = 0;
if (pUpY >= WORLD_HEIGHT) {
pUpY = 0;
}
}
}
coins = new Coins(pUpX, pUpY, random.nextInt(3));
debug = " pUp x: -- " + pUpX + " pUp y: -- " + pUpY + " TickTime: ";
Log.d("Location of power up", debug);
}
// moves the meteor
private void moveMeteor() {
for (int i = 0; i < meteor.length; i++){
meteor[i].y += xSpeed;
meteor[i].x += ySpeed;
if (meteor[i].x < 0)
meteor[i].x = 81;
if (meteor[i].x > 81)
meteor[i].x = 0;
if (meteor[i].y < 0)
meteor[i].y = 118;
if (meteor[i].y > 118)
meteor[i].y = 0;
}
}
// same method as coin spawns meteor at random speed at random direction.
private void spawnMeteor() {
int levels = 2;
if (coinCount % 3 == 0) {
levels = +2;
}
int tempX = random.nextInt(levels);
int tempY = random.nextInt(levels);
if (tempX == 0)
xSpeed = 0;
else if (tempX == 1)
xSpeed = -1;
else if (tempX == 2)
xSpeed = 1;
else if (tempX == 3)
xSpeed = -2;
else if (tempX == 4)
xSpeed = 2;
else if (tempX == 5)
xSpeed = -3;
else if (tempX == 6)
xSpeed = 3;
else if (tempX == 7)
xSpeed = -4;
else if (tempX == 8)
xSpeed = 4;
else if (tempX == 9)
xSpeed = -5;
else if (tempX == 10)
xSpeed = 5;
else if (tempX == 11)
xSpeed = -6;
else if (tempX == 12)
xSpeed = 6;
else if (tempX == 13)
xSpeed = -7;
else if (tempX == 14)
xSpeed = 7;
else if (tempX == 15)
xSpeed = -8;
else if (tempX == 16)
xSpeed = 8;
if (tempY == 0)
ySpeed = 0;
else if (tempY == 1)
ySpeed = -1;
else if (tempY == 2)
ySpeed = 1;
else if (tempY == 3)
ySpeed = 2;
else if (tempY == 4)
ySpeed = -2;
else if (tempY == 5)
ySpeed = 3;
else if (tempY == 6)
ySpeed = -3;
else if (tempY == 7)
ySpeed = -4;
else if (tempY == 8)
ySpeed = 4;
else if (tempY == 9)
ySpeed = 5;
else if (tempY == 10)
ySpeed = -5;
else if (tempY == 11)
ySpeed = 6;
else if (tempY == 12)
ySpeed = -6;
else if (tempY == 13)
ySpeed = 7;
else if (tempY == 14)
ySpeed = -7;
else if (tempY == 15)
ySpeed = 8;
else if (tempY == 16)
ySpeed = -8;
if (xSpeed ==0){
ySpeed =1;
}else if(ySpeed ==0){
xSpeed =1;
}
for (int x = 0; x < WORLD_WIDTH; x++) {
for (int y = 0; y < WORLD_HEIGHT; y++) {
fields[x][y] = false;
}
}
fields[pUpX][pUpY] = true;
pUpX = random.nextInt(WORLD_WIDTH);
pUpY = random.nextInt(WORLD_HEIGHT);
while (true) {
if (fields[pUpX][pUpY] == false)
break;
if (pUpX >= WORLD_WIDTH) {
pUpX = 0;
if (pUpY >= WORLD_HEIGHT) {
pUpY = 0;
}
}
}
for(int i=0; i<meteor.length ;i++)
meteor[i] = new Meteor(ppX, pUpY);
}
// updates screen
public void update(float deltaTime) {
if (gameOver)
return;
tickTime += deltaTime;
while (tickTime > tick) {
astro.advance();
moveMeteor();
moved = true;
tickTime -= tick;
score += deltaTime;
for(int i =0; i< meteor.length; i++)
if ((astro.player1.x <= meteor[i].x + 6 && astro.player1.y <= meteor[i].y + 6)
&& (astro.player1.x >= meteor[i].x - 6 && astro.player1.y >= meteor[i].y - 6)) {
gameOver = true;
return;
}
if ((astro.player1.x <= coins.x + 10 && astro.player1.y <= coins.y + 10)
&& (astro.player1.x >= coins.x - 10 && astro.player1.y >= coins.y - 10)) {
placeCoin();
moved = false;
spawnMeteor();
coinCount += 1;
score += SCORE_INCREMENT;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment