- Item da lista classe Game
public class Game extends Canvas implements Runnable, KeyListener {
/**
*
*/
private static final long serialVersionUID = 1L;
public static JFrame frame;
private Thread thread;
public final int WIDTH = 160;
public final int HEIGHT = 120;
public final int SCALE = 3;
private boolean isRunning = true;
private BufferedImage image;
public List<Entiti> entities;
public Spritesheet spritesheet;
private Jogador jogador() {
return null;
}
public Game() {
addKeyListener(this);
this.setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
initFrame();
image = new BufferedImage(WIDTH,HEIGHT,BufferedImage.TYPE_INT_BGR);
entities = new ArrayList<Entiti>();
spritesheet = new Spritesheet("/sprithesheet.png");
Jogador jogador = new Jogador (0,0,16,16,spritesheet.getSprite(32,0,16,16));
entities.add(jogador);
}
public synchronized void Start(){
thread = new Thread(this);
isRunning = true;
thread.start();
}
public synchronized void Stop(){
isRunning = false;
try {
thread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void initFrame() {
frame = new JFrame("Game01");
frame.add(this);
frame.setResizable(false);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
public static void main (String[]args) {
Game game = new Game();
game.Start();
}
public void tick() {
for(int i = 0; i <entities.size(); i++) {
Entiti e = entities.get(i);
e.tick(e);
}
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs == null) {
this.createBufferStrategy(3);
return;
}
Graphics g = image.getGraphics();
g.setColor(new Color(0,0,0));
g.fillRect(0, 0, HEIGHT,WIDTH);
for(int i = 0; i <entities.size(); i++) {
Entiti e = entities.get(i);
e.render(g);
}
g.dispose();
g = bs.getDrawGraphics();
g.drawImage(image,0,0, WIDTH*SCALE,HEIGHT*SCALE,null);
bs.show();
}
public void run(){
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns =1000000000 / amountOfTicks;
double delta = 0;
int frame = 0;
double timer = System.currentTimeMillis();
while(isRunning) {
long now = System.nanoTime();
delta +=(now - lastTime ) / ns;
lastTime = now;
if(delta >= 1) {
tick();
render();
frame ++;
delta -- ;
}
if(System.currentTimeMillis() - timer >= 1000) {
System.out.println("FPS" + frame);
frame = 0;
timer += 1000;
}
}
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_LEFT) {
System.out.println("A");
}else if
(e.getKeyCode() == KeyEvent.VK_RIGHT){
System.out.println("D");
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
- Item da lista classe Entiti(era pra ser Entity)
classe Entiti public class Entiti{
private int x;
private int y;
private int width;
private int height;
private BufferedImage sprite;
public Entiti(int x,int y, int width, int height,BufferedImage sprite) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.sprite = sprite;
}
public int getX() {
return this.x;
}
public int getY() {
return this.y;
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public void tick() {
}
public void render(Graphics g) {
g.drawImage(sprite,this.getX(),this.getY(), null);
}
public void tick(Entiti e) {
// TODO Auto-generated method stub
}
}
- Item da lista classe Jogador
public class Jogador extends Entiti{
public static boolean RIGHT;
public Jogador(int x, int y, int width, int height, BufferedImage sprite) {
super(x,y,width,height,sprite);
}
}