Menu Initialisation
pauseMenu = new Image("data/PausedMenu.png");
deadMenu = new Image("data/deadMenu.png");
Sound Initialisation
death = new Sound("data/Death_Tune.ogg");
Render Loop Updated
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
if (!paused && !dead){
BG.draw(0,0);
BlockMap.tmap.render(0, 0);
g.drawAnimation(player, x , y);
g.drawAnimation(ai, AIx, AIy);
}else if(paused){
pauseMenu.draw(0,0);
}else if(dead){
deadMenu.draw(0,0);
}
}
Logic Loop Updated
public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
Input input = gc.getInput();
float floatDelta = delta*0.1f;
mouseX = gc.getInput().getMouseX();
mouseY = gc.getInput().getMouseY();
if(dead && !paused){
if ((mouseX > 468.0 && mouseX < 762.0) && (mouseY > 395.0 && mouseY < 495.0)){
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
y = 32f;
x = 32f;
AIx = 480;
AIy = 640;
dead = false;
Level1.play();
}
}
if ((mouseX > 468.0 && mouseX < 762.0) && (mouseY > 552.0 && mouseY < 652.0)){
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
System.exit(0);
}
}
}
if(paused && !dead){
if ((mouseX > 511.0 && mouseX < 730.0) && (mouseY > 375.0 && mouseY < 460.0)){
if(input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
paused = false;
}
}
if ((mouseX > 511.0 && mouseX < 730.0) && (mouseY > 500.0 && mouseY < 585.0)){
if( input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
System.exit(0);
}
}
}
if (!paused && !dead){
The code above checks to see which of the states the player is currently in, if the player is paused then the game checks to see if the played is clicking in any of the correct areas to perform the desired actions from the pause menu, this is also done if the player is dead and is trying to click on the restart button on the death menu. The very last line of the code above checks to see if the user is in a normal state e.g. not dead or paused and if so the normal game logic continues uninterrupted. Because the death menu is now set up ready to be used the correct code can be added to the AI collision detection to show the death menu when hit and also when the played falls down a pit in the level. The code for both of these is shown below:
Falling Death Code:
if (y > 960){
dead = true;
}
Updated AI Collision Code:
if(playerPoly.intersects(AIPoly)){
dead = true;
Level1.stop();
death.play();
}
As the main function of the game are now in place I can reuse all of the code that has been written for this level to create a second level for the game, but lastly on this level I will have to create an exit to the second level by using the door sprite that Stephen has created. The simple code to add a door to the level is below and the code to continue onto the next level will be added when the level is completed.
Initialising the Door:
door = new Image("data/Exit.png");
Drawing the Door (Updated Render Loop):
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
if (!paused && !dead){
BG.draw(0,0);
BlockMap.tmap.render(0, 0);
door.draw(1216, 576);
g.drawAnimation(player, x , y);
g.drawAnimation(ai, AIx, AIy);
}else if(paused){
pauseMenu.draw(0,0);
}else if(dead){
deadMenu.draw(0,0);
}
}
No comments:
Post a Comment