From c69b21bbf21c00cf2d11e3ad9ac46586575e3a45 Mon Sep 17 00:00:00 2001 From: Ricky Barrette Date: Wed, 4 Apr 2012 09:35:01 -0400 Subject: [PATCH] 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. --- .../src/com/RickBarrette/asteroids/AsteroidGame.java | 12 ++++++++---- .../src/com/RickBarrette/asteroids/GameFrame.java | 3 ++- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java index 3eeaf18..4ced978 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/AsteroidGame.java @@ -20,6 +20,7 @@ package com.RickBarrette.asteroids; import java.util.ArrayList; +import java.util.Random; import java.util.Vector; /** @@ -64,6 +65,7 @@ public class AsteroidGame extends Thread { * @author ricky barrette */ private void downShip() { + Random gen = new Random(); System.out.println("Ship collision dected"); /* * move the players ship's @@ -77,7 +79,7 @@ public class AsteroidGame extends Thread { } if(s != null){ - s.setLocation(100, 100); + s.setLocation(gen.nextInt(mGameFrame.getDisplayWidth()), gen.nextInt(mGameFrame.getDispalyHeight())); s.allStop(); } @@ -106,12 +108,13 @@ public class AsteroidGame extends Thread { * populates the world for a new game * @author ricky barrette */ - public void initLevel() { + public void initLevel() { + Random gen = new Random(); /* * added a asteroid per level */ 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 */ public void newGame() { + Random gen = new Random(); mWorld.clear(); mGameFrame.setDisplayText(null); @@ -137,7 +141,7 @@ public class AsteroidGame extends Thread { mGameFrame.getStatusBar().setShotCount(0); 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(); diff --git a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java index 180345a..3a48fa8 100644 --- a/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java +++ b/Asteroids/src/com/RickBarrette/asteroids/GameFrame.java @@ -182,7 +182,8 @@ public class GameFrame extends JFrame implements KeyListener, ActionListener{ case KeyEvent.VK_H: if(isKeyPressed){ 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;