Created
June 10, 2014 20:21
-
-
Save Lumaio/45da1a4b71c69abd9e16 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 com.loom.sa.objects; | |
public class Config { | |
public static final int SIZE = 16; | |
public static final int START_X = 3; | |
public static final int START_Y = 3; | |
} |
This file contains hidden or 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 com.loom.sa.main; | |
import org.newdawn.slick.AppGameContainer; | |
import org.newdawn.slick.GameContainer; | |
import org.newdawn.slick.SlickException; | |
import org.newdawn.slick.state.StateBasedGame; | |
import com.loom.sa.states.s_Main; | |
import com.loom.sa.states.s_Play; | |
public class Main extends StateBasedGame { | |
private static String[] refs = new String[2]; | |
public Main(String name) { | |
super(name); | |
} | |
public void initStatesList(GameContainer gc) throws SlickException { | |
this.addState(new s_Main()); | |
this.addState(new s_Play()); | |
} | |
public static void main(String[] args) throws SlickException { | |
AppGameContainer game = new AppGameContainer(new Main("Square Adventures")); | |
game.setDisplayMode(800, 600, false); | |
game.setTargetFrameRate(60); | |
game.start(); | |
} | |
} |
This file contains hidden or 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 com.loom.sa.objects; | |
public class Player { | |
public String name; | |
public int b_num = 30; | |
public int b_type = 1; | |
public int x = 1; | |
public int y = 1; | |
public Player(String name) { | |
this.name = name; | |
} | |
} |
This file contains hidden or 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 com.loom.sa.states; | |
import java.awt.Font; | |
import java.util.ArrayList; | |
import java.util.Random; | |
import org.newdawn.slick.Color; | |
import org.newdawn.slick.GameContainer; | |
import org.newdawn.slick.Graphics; | |
import org.newdawn.slick.SlickException; | |
import org.newdawn.slick.TrueTypeFont; | |
import org.newdawn.slick.geom.Rectangle; | |
import org.newdawn.slick.gui.AbstractComponent; | |
import org.newdawn.slick.gui.ComponentListener; | |
import org.newdawn.slick.gui.TextField; | |
import org.newdawn.slick.state.BasicGameState; | |
import org.newdawn.slick.state.StateBasedGame; | |
import com.loom.sa.objects.Player; | |
public class s_Main extends BasicGameState { | |
private TextField nameField; | |
private Font font; | |
public static TrueTypeFont ttf; | |
public static Player player; | |
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { | |
font = new Font("Arial", Font.BOLD, 12); | |
ttf = new TrueTypeFont(font, false); | |
nameField = new TextField(gc, ttf, 225, 200, 300, 25, new ComponentListener() { | |
public void componentActivated(AbstractComponent ac) { | |
player = new Player(nameField.getText()); | |
System.out.println(player.name); | |
sbg.enterState(1); | |
nameField.setAcceptingInput(false); | |
nameField.setText(""); | |
} | |
}); | |
nameField.setBackgroundColor(Color.darkGray); | |
} | |
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { | |
g.setColor(Color.darkGray); | |
nameField.render(gc, g); | |
ttf.drawString(nameField.getX(), nameField.getY()-15, "Player Name"); | |
} | |
public void update(GameContainer gc, StateBasedGame sbg, int dt) throws SlickException { | |
} | |
public static Player getPlayer() { | |
return player; | |
} | |
public int getID() { | |
return 0; | |
} | |
} |
This file contains hidden or 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 com.loom.sa.states; | |
import org.newdawn.slick.Color; | |
import org.newdawn.slick.GameContainer; | |
import org.newdawn.slick.Graphics; | |
import org.newdawn.slick.Input; | |
import org.newdawn.slick.SlickException; | |
import org.newdawn.slick.TrueTypeFont; | |
import org.newdawn.slick.geom.Rectangle; | |
import org.newdawn.slick.state.BasicGameState; | |
import org.newdawn.slick.state.StateBasedGame; | |
import com.loom.sa.objects.Config; | |
import com.loom.sa.objects.Player; | |
import com.loom.sa.objects.Tile; | |
public class s_Play extends BasicGameState { | |
private Player player; | |
private TrueTypeFont ttf; | |
private boolean run = false; | |
private Tile[][] map; | |
private int size = Config.SIZE; | |
private int x = Config.START_X; | |
private int y = Config.START_Y; | |
private boolean fill=false; | |
public void init(GameContainer gc, StateBasedGame sbg) throws SlickException { | |
ttf = s_Main.ttf; | |
map = new Tile[20][20]; | |
//Populate map | |
for (int i = 0; i < map.length; i++) { | |
for (int j = 0; j < map.length; j++) { | |
map[i][j] = new Tile(0); | |
} | |
} | |
} | |
public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException { | |
ttf.drawString(200, 500, "Player Name: "+player.name); | |
//Draw map | |
for (int i = 0; i < map.length; i++) { | |
for (int j = 0; j < map.length; j++) { | |
if (map[i][j].type==0) { | |
g.setColor(Color.darkGray); | |
fill=false; | |
} | |
if (i == player.x && j == player.y) { | |
g.setColor(Color.green); | |
fill=true; | |
} | |
if (fill) | |
g.fill(new Rectangle((i+x)*size, (j+y)*size, size, size)); | |
else | |
g.draw(new Rectangle((i+x)*size, (j+y)*size, size, size)); | |
} | |
} | |
} | |
public void update(GameContainer gc, StateBasedGame sbg, int dt) throws SlickException { | |
runOnce(); | |
getInput(gc, sbg); | |
} | |
public void getInput(GameContainer gc, StateBasedGame sbg) { | |
Input i = gc.getInput(); | |
i.enableKeyRepeat(); | |
if (i.isKeyPressed(Input.KEY_UP)) { | |
move(0, -1); | |
}if (i.isKeyPressed(Input.KEY_DOWN)) { | |
move(0, 1); | |
}if (i.isKeyPressed(Input.KEY_LEFT)) { | |
move(-1, 0); | |
}if (i.isKeyPressed(Input.KEY_RIGHT)) { | |
move(1, 0); | |
} | |
} | |
public void move(int x, int y) { | |
if (player.x+x > -1 && player.x+x<map.length && player.y+y > -1 && player.y+y<map.length) { | |
if (!(map[player.x+x][player.y+y].type == 1)) { | |
if (map[player.x+x][player.y+y].type == 2) { | |
map[player.x+x][player.y+y].type=3; | |
}else { | |
player.x+=x; | |
player.y+=y; | |
} | |
} | |
} | |
} | |
public void runOnce() { | |
if (!run){ | |
player = s_Main.getPlayer(); | |
run=true; | |
} | |
} | |
public int getID() { | |
return 1; | |
} | |
} |
This file contains hidden or 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 com.loom.sa.objects; | |
public class Tile { | |
public int type = 0; | |
public Tile(int type) { | |
this.type = type; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment