Original Post
Hey guys can anyone spot why my game has smooth graphics after the first level? I think it may be an initialization thing but I'm not seeing it. I need fresh eyes! :P Heres the class thats giving problems: package a3; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Iterator; import java.util.Observable; import java.util.Random; import javax.swing.JOptionPane; import javax.swing.Timer; /** * This class encapsulates the gameworld rules/logic and gamestate data. * @author Paul V Johnson III * */ public class GameWorld extends Observable implements ActionListener{ private GameCollection gameObjects; //Game state variables private double minScore; private int lives; private int level; private int ownedSquares; private int tickets; private int clock; private int elapsed; private Timer time; /** * @return the minScore */ public double getMinScore() { return minScore; } /** * @return the level */ public int getLevel() { return level; } /** * Constructor Gameworld() initializes the game level. */ public GameWorld(){ level = 0; resetGame(++level); time = new Timer(20,this); } /*@SuppressWarnings("unchecked") public GameCollection getSelected(){ GameObject n; GameCollection selectedObjects = new GameCollection(); Iterator itr = gameObjects.iterator(); while (itr.hasNext()){ if ((n = (GameObject) itr.next()) instanceof ISelectable){ selectedObjects.add(n); } } return selectedObjects; }*/ /** * This method is to start the game timer. */ public void startClock(){ time.start(); } /** * This method is to stop the game timer. */ public void stopClock(){ time.stop(); } /** * This method returns the number of monsterballs calculated by level * @return the quantity of monsterballs per level. */ private int gameMonsters() { return 3+((level-1)*1); } /** * This method calculates the dynamic timeticket time by level. * @return the dynamic tickettime */ private int levelTicketTime(){ return 30-((level-1)*5); } /** * Getter showClock to read the clock game state data. * @return clock */ public int getClock(){ return clock; } /** * This method dynamically updates the positions of all gameObjects that are movable. * @param i */ @SuppressWarnings("unchecked") private void updatePositions(int i){ Iterator itr = gameObjects.iterator(); GameObject n; while(itr.hasNext()){ n = (GameObject) itr.next(); n.move(i); } setChanged(); notifyObservers(); } /** * This method decrements the lives value * Alerts the user that an action has been registered. * The car is reset to it's starting position. */ @SuppressWarnings("unchecked") public void bam(){ lives--; Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).reset(); break; } } if(isGameOver()) gameOver(); setChanged(); notifyObservers(); } /** * When the game is over the user is prompted if they would like to play again. * If so, the game is reset to level 1. */ public void gameOver(){ int n = JOptionPane.showConfirmDialog(null, "Would you like to try again?", "GAME OVER", JOptionPane.YES_NO_OPTION); if (n == JOptionPane.YES_OPTION) resetGame(1); else System.exit(1); } /** * This method determines if the game is over * @return true if the game is over * @return false if the game is still in play */ private boolean isGameOver() { return lives <= 0; } /** * This method resets the game in the event that the game is over and wants to be replayed * or when the player has advanced to the next level. * @param lvl */ private void resetGame(int lvl){ ownedSquares = 396; level = lvl; minScore = levelMinScore(); clock = gameTime(); if (level ==1) lives = 3; //else lives is carried on to the next level tickets = 0; gameObjects = new GameCollection(); setFields(); Car.getTheCar().reset(); gameObjects.add(Car.getTheCar()); //create the monster balls. index 1-startBalls for (int k = 0; k 0) JOptionPane.showMessageDialog(null, "Congrats! You have advanced to the next level!", "YOU WON", JOptionPane.INFORMATION_MESSAGE); if (gameTime()==0 && !isGameOver()){ if(JOptionPane.showConfirmDialog(null, "You beat the game! Play again?", "YOU BEAT THE GAME!", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){ resetGame(1); } else System.exit(1); } } else{ //Game still in play setChanged(); notifyObservers(); } } /** * This method calculates the percentage of owned squares * @return the percentage of owned squares */ public double calcOwned(){ return (double)(ownedSquares/850.0); } /** * This method adds the time within the TimeTicket to the game clock. */ @SuppressWarnings("unchecked") private void addTime(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof TimeTickets){ clock += ((TimeTickets) n).getTime(); break; } } setChanged(); notifyObservers(); } /** * This method is a getter for the class field tickets * @return tickets */ private int getTickets() { return tickets; } /** * This method adds 1 unit to the current speed of the car. */ @SuppressWarnings("unchecked") public void acellerating(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).setSpeed(5); setChanged(); } } notifyObservers(); } /** * This method decreases the car's speed by 1, but not lower than 0 */ @SuppressWarnings("unchecked") public void braking(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ if(((Car) n).getSpeed() > 0){ ((Car) n).setSpeed(-1); setChanged(); } else JOptionPane.showMessageDialog(null, "Acelleration is already 0. Cannot slow down when stopped.", "Can't do that", JOptionPane.WARNING_MESSAGE); } } notifyObservers(); } /** * This method changes the direction of the car to head North */ @SuppressWarnings("unchecked") public void headNorth(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goNorth(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head East */ @SuppressWarnings("unchecked") public void headEast(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goEast(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head South */ @SuppressWarnings("unchecked") public void headSouth(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goSouth(); setChanged(); } } notifyObservers(); } /** * This method changes the direction of the car to head West */ @SuppressWarnings("unchecked") public void headWest(){ Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof Car){ ((Car) n).goWest(); setChanged(); } } notifyObservers(); } /** * This method returns a boolean value based off if the percentage of squares is met */ public boolean checkWon() { return calcOwned() >= minScore; } /** * This getter returns the value of the lives game state data * @return lives */ public int getLives() { return this.lives; } /** * This method ensures that there are TimeTickets before adding the TimeTicket * time and removing it from the gameObject Vector. */ @SuppressWarnings("unchecked") public void checkTickets() { if(getTickets()>0){ addTime(); Iterator itr = gameObjects.iterator(); while(itr.hasNext()){ GameObject n; if ((n = (GameObject) itr.next()) instanceof TimeTickets){ gameObjects.remove(n); setChanged(); break; } } //Removing TimeTicket from world. tickets--; notifyObservers(); } else JOptionPane.showMessageDialog(null, "You do not have any TimeTickets.", "Can't do that", JOptionPane.WARNING_MESSAGE); } public GameCollection getGameObjects() { return this.gameObjects; } @Override public void actionPerformed(ActionEvent e) { TickCommand.getTickCommand(this).actionPerformed(e); } public void addTimeTicket() { gameObjects.add(new TimeTickets(levelTicketTime())); tickets++; setChanged(); notifyObservers(); } }