Hello, i have been working on a zombie defense game the past 3 days, and today i got to the point where i could compile my code and test it out before adding more features. The code compiled fine and the sprites are displayed but not updated to the screen, they are only drawn once. I have been trying to debug my code for the past hour to no success, hopefully your more experienced eyes can help solve the problem. I have also attached a zip file of all my code.
package org.zombiedefense; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.image.BufferStrategy; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing.JPanel; /* * The main hook of my game. This class will be the manager for the dsplay * and where all game logic is called/managed from. * * Display will consist of a loop that cycles through all entites within the game * making each entity move and then drawing the entities in the new place. It will * also allow the player to control their control. * * Game logic includes that it will be notified when entities within my game * detect certain events or actions such as collision with other entities, * enemy died, etc * * Max Winkler */ public class Game extends Canvas { //strategy allows me to use buffers/page flipping private BufferStrategy strategy; //to determine if game is running, main game loop is running private boolean isRunning = true; //List of all entities that currently exist in my game private ArrayList entities = new ArrayList(); //List of all entities that ned to be removed, because they died private ArrayList removeEntities = new ArrayList(); //Entity for player 1 private PlayerOneEntity player1; //speed that the player moves at private int moveSpeed = 450; //the time that the player last fired a shot private long lastShotFired = 0; //The interval between the player's shots, how fast can the player shoot in ms private int firingSpeed = 150; //the number of enemies left private int enemyCount; //true if left arrow key is pressed private boolean leftArrowPressed = false; //true if right arrow key is pressed private boolean rightArrowPressed = false; //true if the player is firing private boolean currentlyFiring = false; private boolean onePlayer = true; private boolean twoPlayer = false; private boolean gameStarted = false; private String string = ""; //Constructor to create a window and set up the game public Game() { //create a frame for our game JFrame frame = new JFrame("Max's Zombie Defense"); //get a hold on the frame and then set the resolution JPanel panel = (JPanel)frame.getContentPane(); panel.setPreferredSize(new Dimension(1024,768)); panel.setLayout(null); //set up canvas size setBounds(0,0,1024,768); panel.add(this); //stop AWT from repainting our canvas as i am going to do that manually with buffers setIgnoreRepaint(true); //make the window visible frame.pack(); frame.setResizable(false); frame.setVisible(true); //check if user closed window frame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); //adding a keyboard input listener to respond to keypresses addKeyListener(new KeyInputHandler()); //get focus so our program gets the key presses requestFocus(); //create the 2 way buffers createBufferStrategy(2); strategy = getBufferStrategy(); } private void startGame() { entities.clear(); leftArrowPressed = false; rightArrowPressed = false; currentlyFiring = false; player1 = new PlayerOneEntity(this,"sprites/mmright1.png",40,500); entities.add(player1); enemyCount = 0; for (int i=0;i < 10; i++) { EnemyEntity enemy = new EnemyEntity(this,"sprites/franken1.png",900+(i*25),500); entities.add(enemy); enemyCount++; } } public void removeEntity(Entity entity) { removeEntities.add(entity); } public void gameOver() { string = "The Zombies have your BRAINS!"; onePlayer = false; twoPlayer = false; } public void gameWin() { string = "You Have Survived, For Another Day!"; onePlayer = false; twoPlayer = false; } public void enemyKilled() { enemyCount--; if (enemyCount == 0) { gameWin(); } } public void ableToShoot () { if ((System.currentTimeMillis() - lastShotFired) < firingSpeed) { return; } lastShotFired = System.currentTimeMillis(); BulletEntity bullet = new BulletEntity(this,"sprites/bullet.gif",player1.getX()+30,player1.getY()+50); entities.add(bullet); } public void gameLoop() { long lastLoopTime = System.currentTimeMillis(); while (isRunning) { long lastUpdate = System.currentTimeMillis() - lastLoopTime; lastLoopTime = System.currentTimeMillis(); Graphics2D graphics = (Graphics2D) strategy.getDrawGraphics(); DrawBG bg = new DrawBG(graphics); graphics.setColor(Color.orange); graphics.drawString(string,512,450); //graphics.drawString("Press '1' or '2' for Single-Player or Two-Player",512,550); string = ""; startGame(); gameStarted = true; if (gameStarted) { for (int i=0;i<entities.size();i++) { Entity entity = (Entity) entities.get(i); entity.move(); } } for (int i=0;i<entities.size();i++) { Entity entity = (Entity) entities.get(i); entity.draw(graphics); } for (int i=0;i<entities.size();i++) { for (int j=i+1;j<entities.size();j++) { Entity firstEntity = (Entity) entities.get(i); Entity checkAgainst = (Entity) entities.get(j); if (firstEntity.collisionDetection(checkAgainst)) { firstEntity.collisionWith(checkAgainst); checkAgainst.collisionWith(firstEntity); } } } entities.removeAll(removeEntities); removeEntities.clear(); graphics.dispose(); strategy.show(); player1.setHorizontalVelocity(0); if ((leftArrowPressed) && (!rightArrowPressed)) { player1.setHorizontalVelocity(-moveSpeed); } else if ((rightArrowPressed) && (!leftArrowPressed)) { player1.setHorizontalVelocity(moveSpeed); } if (currentlyFiring) { ableToShoot(); } try { Thread.sleep(10); } catch (Exception e) {} } } private class KeyInputHandler extends KeyAdapter { public void keyPressed(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftArrowPressed = true; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightArrowPressed = true; } if (e.getKeyCode() == KeyEvent.VK_UP) { currentlyFiring = true; } if (e.getKeyCode() == KeyEvent.VK_1) { onePlayer = true; } } public void keyReleased(KeyEvent e) { if (e.getKeyCode() == KeyEvent.VK_LEFT) { leftArrowPressed = false; } if (e.getKeyCode() == KeyEvent.VK_RIGHT) { rightArrowPressed = false; } if (e.getKeyCode() == KeyEvent.VK_UP) { currentlyFiring = false; } } } public static void main(String[] args) { Game g = new Game(); g.gameLoop(); } }package org.zombiedefense; import java.awt.Graphics; import java.awt.Image; public class Sprite { //sprite image private Image image; //create new sprite based on image public Sprite(Image image) { this.image = image; } //return width in pixels of sprite public int getWidth() { return image.getWidth(null); } //get height in pixels of sprite public int getHeight() { return image.getHeight(null); } //draw the sprite public void draw(Graphics g,int x,int y) { g.drawImage(image,x,y,null); } }javagame.zippackage org.zombiedefense; import java.awt.Graphics; import java.awt.Rectangle; /* * the entity is meant to detect collisions */ public abstract class Entity { // x/y location of entity protected int x; protected int y; //sprite that this entity uses protected Sprite sprite; //velocity/speed of entity protected int velX; protected int velY; //rectangle for this entity during collision detection private Rectangle thisEntity = new Rectangle(); //rectangle for other entities during collision detection private Rectangle otherEntity = new Rectangle(); //Constructor for this entity public Entity(String refImage,int x,int y) { sprite = SpriteManager.get().getSprite(refImage); this.x = x; this.y = y; } //Entity move based on velocity and current location public void move() { x += velX; y += velY; } public void setHorizontalVelocity(int velX) { this.velX = velX; } public void setVerticalVelocity(int velY) { this.velY = velY; } public int getHorizontalVelocity() { return velX; } public int getVerticalVelocity() { return velY; } //Draw Entity with graphics context, then calls Sprite class draw public void draw(Graphics context) { sprite.draw(context,x,y); } //logic associated with entity defined by subclass public void doLogic() { } public int getX() { return x; } public int getY() { return y; } //check if this entity has collided with another entity, return true if they do public boolean collisionDetection(Entity checkEntity) { thisEntity.setBounds(x,y,sprite.getWidth(),sprite.getHeight()); otherEntity.setBounds(checkEntity.x,checkEntity.y,checkEntity.sprite.getWidth(),checkEntity.sprite.getHeight()); return thisEntity.intersects(otherEntity); } //says that this entity collided with another and the entity that this entity collided with public abstract void collisionWith(Entity collidedEntity); }
EDIT: mod please close. sorry but i found the bug .