Last active
December 31, 2015 09:09
-
-
Save Hulzenga/7965337 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package lib; | |
import org.newdawn.slick.*; | |
import org.newdawn.slick.state.*; | |
public class Play extends BasicGameState { | |
Animation Player, movingLeft, movingRight, notMoving, notMoving1; | |
Image worldMap; | |
boolean quit = false; | |
boolean isJumping = false; | |
int[] duration = { 200, 200, 200, 200, 200, 200, 200 }; | |
float playerPositionX = 0; | |
float playerPositionY = 0; | |
float shiftX = playerPositionX + 384; | |
float shiftY = playerPositionY + 384; | |
//gameplay constants, adjust these to get desired behaviour | |
final float JUMP_VELOCITY = 20.0f; | |
final float JUMP_BOOST = 1.0f; | |
final float GRAVITY = 10.0f; | |
float velocityY = 0; | |
public Play(int state) { | |
} | |
public void init(GameContainer gc, StateBasedGame sbg) | |
throws SlickException { | |
worldMap = new Image("res/worldMap.png"); | |
Image[] walkLeft = { new Image("res/playerL1.png"), | |
new Image("res/playerL2.png"), new Image("res/playerL3.png"), | |
new Image("res/playerL4.png"), new Image("res/playerL5.png"), | |
new Image("res/playerL6.png"), new Image("res/playerL7.png") }; | |
Image[] walkRight = { new Image("res/playerR1.png"), | |
new Image("res/playerR2.png"), new Image("res/playerR3.png"), | |
new Image("res/playerR4.png"), new Image("res/playerR5.png"), | |
new Image("res/playerR6.png"), new Image("res/playerR7.png") }; | |
Image[] stopWalking = { new Image("res/playerR1.png"), | |
new Image("res/playerR1.png"), new Image("res/playerR1.png"), | |
new Image("res/playerR1.png"), new Image("res/playerR1.png"), | |
new Image("res/playerR1.png"), new Image("res/playerR1.png") }; | |
Image[] stopWalking1 = { new Image("res/playerL1.png"), | |
new Image("res/playerL1.png"), new Image("res/playerL1.png"), | |
new Image("res/playerL1.png"), new Image("res/playerL1.png"), | |
new Image("res/playerL1.png"), new Image("res/playerL1.png") }; | |
movingLeft = new Animation(walkLeft, duration, false); | |
movingRight = new Animation(walkRight, duration, false); | |
notMoving = new Animation(stopWalking, duration, false); | |
notMoving1 = new Animation(stopWalking1, duration, false); | |
Player = notMoving; | |
} | |
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) | |
throws SlickException { | |
worldMap.draw(playerPositionX, playerPositionY); | |
Player.draw(shiftX, shiftY); | |
if (quit == true) { | |
g.drawString("Resume (R)", 250, 100); | |
g.drawString("Main Menu (M)", 250, 150); | |
g.drawString("Quit Game (Q)", 250, 200); | |
if (quit == false) { | |
g.clear(); | |
} | |
} | |
} | |
public void update(GameContainer gc, StateBasedGame sbg, int delta) | |
throws SlickException { | |
Player.update(delta); | |
Input input = gc.getInput(); | |
String lastKeyPressed = null; | |
if (input.isKeyDown(Input.KEY_W)) { | |
if (!isJumping) { | |
velocityY = JUMP_VELOCITY; | |
isJumping = true; | |
playerPositionY += velocityY*delta * .1f; | |
} else if (velocityY > 0) { //this means you jump higher if you continue to press w while jumping | |
velocityY += delta*JUMP_BOOST; | |
} | |
} | |
if (isJumping) { | |
playerPositionY += velocityY * .1f; | |
velocityY -= delta*GRAVITY; | |
if (playerPositionY < 0.0f) { //this checks for collision with the ground | |
isJumping = false; | |
playerPositionY = 0.0f; | |
} | |
} | |
if (input.isKeyDown(Input.KEY_A)) { | |
Player = movingLeft; | |
playerPositionX += delta * .1f; | |
lastKeyPressed = "A"; | |
} | |
if (input.isKeyDown(Input.KEY_D)) { | |
Player = movingRight; | |
playerPositionX -= delta * .1f; | |
lastKeyPressed = "D"; | |
} | |
if ((lastKeyPressed == "A") | |
&& (!input.isKeyDown(Input.KEY_A) && (!input | |
.isKeyDown(Input.KEY_D)))) { | |
Player = notMoving1; | |
} | |
if ((lastKeyPressed == "D") | |
&& (!input.isKeyDown(Input.KEY_A) && (!input | |
.isKeyDown(Input.KEY_D)))) { | |
Player = notMoving; | |
} | |
if (input.isKeyDown(Input.KEY_ESCAPE)) { | |
quit = true; | |
} | |
if (quit == true) { | |
if (input.isKeyDown(Input.KEY_R)) { | |
quit = false; | |
} | |
if (input.isKeyDown(Input.KEY_M)) { | |
sbg.enterState(0); | |
} | |
if (input.isKeyDown(Input.KEY_Q)) { | |
System.exit(0); | |
} | |
} | |
} | |
public int getID() { | |
return 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment