Advertisement
Guest User

Untitled

a guest
Apr 9th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package javaGame;
  2.  
  3. import org.lwjgl.input.Mouse;
  4. import org.newdawn.slick.*;
  5. import org.newdawn.slick.state.*;
  6.  
  7. public class Play extends BasicGameState{
  8. //Animations
  9. Animation sprite, movingUp, movingDown, movingLeft, movingRight;
  10. //Images
  11. Image worldMap;
  12. Image resumeButton;
  13. Image menuButton;
  14. //Variables
  15. boolean goMenu = false;
  16. int duration[] = {200, 200};
  17. float spritePositionX = 0;
  18. float spritePositionY = 0;
  19. float shiftX = spritePositionX + 400;
  20. float shiftY = spritePositionY + 300;
  21. //Mouse
  22. public int mousePosX;
  23. public int mousePosY;
  24.  
  25. public Play (int state){
  26.  
  27. }
  28. public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
  29. //Image Arrays
  30. worldMap = new Image ("res/grassbackground.png");
  31. Image[] walkUp = {new Image("res/movingUp.png"), new Image("res/movingUp.png")};
  32. Image[] walkDown = {new Image("res/movingDown.png"), new Image("res/movingDown.png")};
  33. Image[] walkLeft = {new Image("res/movingLeft.png"), new Image("res/movingLeft.png")};
  34. Image[] walkRight = {new Image("res/movingRight.png"), new Image("res/movingRight.png")};
  35. //Animation
  36. movingUp = new Animation(walkUp, duration, false);
  37. movingDown = new Animation(walkDown, duration, false);
  38. movingLeft = new Animation(walkLeft, duration, false);
  39. movingRight = new Animation(walkRight, duration, false);
  40.  
  41. sprite = movingDown;
  42. }
  43. public void render(GameContainer gc, StateBasedGame sbg, Graphics g)throws SlickException{
  44. //Images
  45. resumeButton = new Image("res/Resume Button.png");
  46. menuButton = new Image("res/Main Menu Button.png");
  47.  
  48. g.drawImage(worldMap, spritePositionX, spritePositionY);
  49. sprite.draw(shiftX,shiftY);
  50. g.drawString("Sprite's coordinates: "+ "\n X: " + spritePositionX + ("\n Y: " + spritePositionY),10,25);
  51.  
  52. if (goMenu == true){
  53. g.drawImage(resumeButton, 300, 240);
  54. g.drawImage(menuButton, 300, 285);
  55. if(goMenu == false){
  56. g.clear();
  57. }
  58. }
  59.  
  60. }
  61. public void update(GameContainer gc, StateBasedGame sbg, int delta)throws SlickException{
  62. //Mouse Input
  63. Input input = gc.getInput();
  64. mousePosX = Mouse.getX();
  65. mousePosY = 600 - Mouse.getY();
  66. //Keyboard Input
  67. if(input.isKeyDown(Input.KEY_W) && goMenu == false){
  68. sprite = movingUp;
  69. spritePositionY += delta*.2f;
  70. if(spritePositionY >= 350){
  71. spritePositionY -= delta*.2f;
  72. }
  73. }
  74. if(input.isKeyDown(Input.KEY_S) && goMenu == false){
  75. sprite = movingDown;
  76. spritePositionY -= delta *.2f;
  77. if(spritePositionY <= -230){
  78. spritePositionY += delta*.2f;
  79. }
  80. }
  81. if(input.isKeyDown(Input.KEY_D) && goMenu == false){
  82. sprite = movingRight;
  83. spritePositionX -= delta *.2f;
  84. if(spritePositionX <= -362){
  85. spritePositionX += delta*.2f;
  86. }
  87. }
  88. if(input.isKeyDown(Input.KEY_A) && goMenu == false){
  89. sprite = movingLeft;
  90. spritePositionX += delta*.2f;
  91. if(spritePositionX >= 402){
  92. spritePositionX -= delta*.2f;
  93. }
  94. }
  95. if(input.isKeyDown(Input.KEY_ESCAPE)){
  96. goMenu = true;
  97. }
  98. //Buttons
  99. if((mousePosX>=300 && mousePosX <= 510)&&(mousePosY>=285&&mousePosY<=320 && goMenu == true)){
  100. if(Mouse.isButtonDown(0)){
  101. sbg.enterState(0);
  102. System.out.println("Going to Main Menu from Play");
  103. }
  104. }
  105. if((mousePosX>=300 && mousePosX <=510)&&(mousePosY >=240 && mousePosY <= 275 && goMenu == true)){
  106. if(Mouse.isButtonDown(0)){
  107. goMenu = false;
  108. System.out.println("Exiting Ingame Menu");
  109. }
  110. }
  111.  
  112. }
  113. public int getID(){
  114. return 1;
  115. }
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement