Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaGame;
- import org.lwjgl.input.Mouse;
- import org.newdawn.slick.*;
- import org.newdawn.slick.state.*;
- public class Play extends BasicGameState{
- //Animations
- Animation sprite, movingUp, movingDown, movingLeft, movingRight;
- //Images
- Image worldMap;
- Image resumeButton;
- Image menuButton;
- //Variables
- boolean goMenu = false;
- int duration[] = {200, 200};
- float spritePositionX = 0;
- float spritePositionY = 0;
- float shiftX = spritePositionX + 400;
- float shiftY = spritePositionY + 300;
- //Mouse
- public int mousePosX;
- public int mousePosY;
- public Play (int state){
- }
- public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
- //Image Arrays
- worldMap = new Image ("res/grassbackground.png");
- Image[] walkUp = {new Image("res/movingUp.png"), new Image("res/movingUp.png")};
- Image[] walkDown = {new Image("res/movingDown.png"), new Image("res/movingDown.png")};
- Image[] walkLeft = {new Image("res/movingLeft.png"), new Image("res/movingLeft.png")};
- Image[] walkRight = {new Image("res/movingRight.png"), new Image("res/movingRight.png")};
- //Animation
- movingUp = new Animation(walkUp, duration, false);
- movingDown = new Animation(walkDown, duration, false);
- movingLeft = new Animation(walkLeft, duration, false);
- movingRight = new Animation(walkRight, duration, false);
- sprite = movingDown;
- }
- public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
- //Images
- resumeButton = new Image("res/Resume Button.png");
- menuButton = new Image("res/Main Menu Button.png");
- g.drawImage(worldMap, spritePositionX, spritePositionY);
- sprite.draw(shiftX,shiftY);
- g.drawString("Sprite's coordinates: "+ "\n X: " + spritePositionX + ("\n Y: " + spritePositionY),10,25);
- if (goMenu == true){
- g.drawImage(resumeButton, 300, 240);
- g.drawImage(menuButton, 300, 285);
- if(goMenu == false){
- g.clear();
- }
- }
- }
- public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
- //Mouse Input
- Input input = gc.getInput();
- mousePosX = Mouse.getX();
- mousePosY = 600 - Mouse.getY();
- //Keyboard Input
- if(input.isKeyDown(Input.KEY_W) && goMenu == false){
- sprite = movingUp;
- spritePositionY += delta*.2f;
- if(spritePositionY >= 350){
- spritePositionY -= delta*.2f;
- }
- }
- if(input.isKeyDown(Input.KEY_S) && goMenu == false){
- sprite = movingDown;
- spritePositionY -= delta *.2f;
- if(spritePositionY <= -230){
- spritePositionY += delta*.2f;
- }
- }
- if(input.isKeyDown(Input.KEY_D) && goMenu == false){
- sprite = movingRight;
- spritePositionX -= delta *.2f;
- if(spritePositionX <= -362){
- spritePositionX += delta*.2f;
- }
- }
- if(input.isKeyDown(Input.KEY_A) && goMenu == false){
- sprite = movingLeft;
- spritePositionX += delta*.2f;
- if(spritePositionX >= 402){
- spritePositionX -= delta*.2f;
- }
- }
- if(input.isKeyDown(Input.KEY_ESCAPE)){
- goMenu = true;
- }
- //Buttons
- if((mousePosX>=300 && mousePosX <= 510)&&(mousePosY>=285&&mousePosY<=320 && goMenu == true)){
- if(Mouse.isButtonDown(0)){
- sbg.enterState(0);
- System.out.println("Going to Main Menu from Play");
- }
- }
- if((mousePosX>=300 && mousePosX <=510)&&(mousePosY >=240 && mousePosY <= 275 && goMenu == true)){
- if(Mouse.isButtonDown(0)){
- goMenu = false;
- System.out.println("Exiting Ingame Menu");
- }
- }
- }
- public int getID(){
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement