package zeldaminiclone;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class Game extends Canvas implements Runnable, KeyListener{
public static int WIDTH = 480, HEIGHT = 480;
public Player player;
public Game() {
this.addKeyListenner(this);
this.setPreferredSize(new Dimension(WIDTH,HEIGHT));
player = new Player(0,0);
}
private void addKeyListenner(Game game) {
// TODO Auto-generated method stub
}
public void tick() {
player.tick();
}
public void render() {
BufferStrategy bs = this.getBufferStrategy();
if(bs ==null) {
this.createBufferStrategy(3);
return;
}
Graphics g = bs.getDrawGraphics();
g.setColor(Color.black);
g.fillRect(0,0,WIDTH,HEIGHT );
player.render(g);
bs.show();
}
public static void main(String[] args) {
Game game = new Game();
JFrame frame = new JFrame();
frame.add(game);
frame.setTitle("Mini Zelda");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
new Thread(game).start();
}
@Override
public void run() {
while(true) {
tick();
render();
try {
Thread.sleep(1000/60);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
if(e.getKeyCode()== KeyEvent.VK_RIGHT) {
player.right = true;
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT) {
player.left = true;
}
if(e.getKeyCode()== KeyEvent.VK_UP) {
player.up = true;
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN) {
player.down = true;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
if(e.getKeyCode()== KeyEvent.VK_RIGHT) {
player.right = false;
}
else if(e.getKeyCode()== KeyEvent.VK_LEFT) {
player.left = false;
}
if(e.getKeyCode()== KeyEvent.VK_UP) {
player.up = false;
}
else if(e.getKeyCode()== KeyEvent.VK_DOWN) {
player.down = false;
}
}
}
package zeldaminiclone;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
public class Player extends Rectangle{
public int spd = 4;
public boolean right,up,down,left;
public Player(int x, int y) {
super(x,y,32,32);
}
public void tick() {
if(right) {
x+=spd;
}else if(left) {
x-=spd;
}
if(up) {
y-=spd;
}else if(down) {
y+=spd;
}
}
public void render (Graphics g) {
g.setColor(Color.blue);
g.fillRect(x,y,width,height);
}
}