PrettyPrint

Sunday, 10 March 2013

Creating The Game - 10/03/2013

This week has been a little less productive than the previous two weeks but there has still been progress and the project is very close to completion which may be one more week of work before it is fully completed. This week Stephen has handed me all of the remaining Graphics assets including the bricks for level 2 and also all of the menus that will be used within the game such as the pause menu etc. After receiving all of these materials I decided to focus on the creation of all of the menus within the game allowing the user to navigate around the game.

First of all is the main menu that the user will be first greeted with when entering the game. Here the user will be able to start the game, look at the controls or exit the game. To start with I set up the main menu class allowing me to get started on adding the main menu image to the screen the code for this is as follows:

Main Menu Initial Code:
import org.newdawn.slick.state.*;
import org.newdawn.slick.*;

public class Menu extends BasicGameState {

 private Image mainMenu;
 public static Music MainMusic;

 public Menu(int state) {

 }

public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
  mainMenu = new Image("data/MainMenu.png");
  MainMusic = new Music("data/Title_Music.ogg");
  MainMusic.loop();
 }

public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
   mainMenu.draw(0,0);
 }

public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
  Input input = gc.getInput();

  float mouseX = 0, mouseY = 0;

  mouseX = gc.getInput().getMouseX();
  mouseY = gc.getInput().getMouseY();

 }

 public int getID(){
  return 0;
 }
}

This code draws the main menu image to the screen and also get the current x and y co-ordinates of the mouse pointer on the screen, this was then used to see if the user was within the acceptable bound of the buttons before clicking allowing them to perform certain actions. After doing this I moved on to allowing the user to start the game from this screen and also looking at the controls and exiting the game. The code for that is below:

Starting The Game:


if(!controls && !controls2){ 
                if((mouseX > 512.0 && mouseX < 735.0) && (mouseY > 556.0 && mouseY < 642.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    MainMusic.stop();
    Play.Level1.loop();
    sbg.enterState(1);
    }
  }

Exiting The Game:


  
if((mouseX > 512.0 && mouseX < 735.0) && (mouseY > 680.0 && mouseY < 767.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    System.exit(0);
   }
  }

Contols Page:


  
if((mouseX > 512.0 && mouseX < 735.0) && (mouseY > 804.0 && mouseY < 890.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    controls = true;
   }
  }

else if (!controls2){
   
  if((mouseX > 1025.0 && mouseX < 1245.0) && (mouseY > 842.0 && mouseY < 928.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    controls = false;
    controls2=true;
   }
  }
   
  if((mouseX > 42.0 && mouseX < 260.0) && (mouseY > 842.0 && mouseY < 928.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    controls = false;
   }
  }
   
 } else {
   
  if((mouseX > 1025.0 && mouseX < 1245.0) && (mouseY > 842.0 && mouseY < 928.0)){
   if (input.isMousePressed(Input.MOUSE_LEFT_BUTTON)){
    controls2=false;
   }
  }
 }

All of the code above checks if the mouse is in a certain bounded area between two sets of different co-ordinates, if that mouse if and the left mouse button is clicked then certain actions are performed. The controls page code also checks to see which page of the controls the user is on and responds accordingly either exiting back to the main menu or continuing onto the next page of controls.

As the controls page will be drawn on top of the main menu temporarily then the render code also had to be updated to draw the correct page when the correct button was pressed. The updated render code is below:

Updated Render Loop:


public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {
  if(!controls && !controls2){
   mainMenu.draw(0,0);
  } else if(!controls2) {
   controlsMenu.draw(0,0);
  } else {
   controlsMenu2.draw(0,0);
  }
 }

Updated Initialisation:


 public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {
  mainMenu = new Image("data/MainMenu.png");
  controlsMenu = new Image("data/Controls.png");
  controlsMenu2 = new Image("data/ControlsPageNME.png");
  MainMusic = new Music("data/Title_Music.ogg");
  MainMusic.loop();
 }

No comments:

Post a Comment