Starting locations for ships, and asteroids are now random

I also added a call to repaint the game's display when the ship hyper
jumps, this allows the display to update the ship's location while the
game is paused.
This commit is contained in:
2012-04-04 09:35:01 -04:00
parent c0f14c2f2e
commit c69b21bbf2
2 changed files with 10 additions and 5 deletions

View File

@@ -20,6 +20,7 @@
package com.RickBarrette.asteroids; package com.RickBarrette.asteroids;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random;
import java.util.Vector; import java.util.Vector;
/** /**
@@ -64,6 +65,7 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
private void downShip() { private void downShip() {
Random gen = new Random();
System.out.println("Ship collision dected"); System.out.println("Ship collision dected");
/* /*
* move the players ship's * move the players ship's
@@ -77,7 +79,7 @@ public class AsteroidGame extends Thread {
} }
if(s != null){ if(s != null){
s.setLocation(100, 100); s.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()));
s.allStop(); s.allStop();
} }
@@ -107,11 +109,12 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public void initLevel() { public void initLevel() {
Random gen = new Random();
/* /*
* added a asteroid per level * added a asteroid per level
*/ */
for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++) for(int i = 0; i < mGameFrame.getStatusBar().getLevel(); i ++)
addElement(new Asteroid(500, 500, 1, 10, 50, 3, 3, this)); addElement(new Asteroid(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()), 1, 10, 50, 3, 3, this));
} }
/** /**
@@ -127,6 +130,7 @@ public class AsteroidGame extends Thread {
* @author ricky barrette * @author ricky barrette
*/ */
public void newGame() { public void newGame() {
Random gen = new Random();
mWorld.clear(); mWorld.clear();
mGameFrame.setDisplayText(null); mGameFrame.setDisplayText(null);
@@ -137,7 +141,7 @@ public class AsteroidGame extends Thread {
mGameFrame.getStatusBar().setShotCount(0); mGameFrame.getStatusBar().setShotCount(0);
mGameFrame.getStatusBar().setLevel(1); mGameFrame.getStatusBar().setLevel(1);
mWorld.add(new Ship(100,100,0,.35,.98,.2,1)); mWorld.add(new Ship(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight()),0,.35,.98,.2,1));
initLevel(); initLevel();

View File

@@ -182,7 +182,8 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{
case KeyEvent.VK_H: case KeyEvent.VK_H:
if(isKeyPressed){ if(isKeyPressed){
Random myRNG = new Random(); Random myRNG = new Random();
ship.setLocation(myRNG.nextInt(mDisplay.getHeight()), myRNG.nextInt(mDisplay.getWidth())); ship.setLocation(myRNG.nextInt(mDisplay.getWidth()), myRNG.nextInt(mDisplay.getHeight()));
repaint();
} }
break; break;