STRUKTUR FILE JAVA
HASIL RUN
Run.java
package Core;
public class Run {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
new Window("Game Kotak").start();
}
}
Window.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package Core;
import inputs.KeyboardHandler;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import objects.Player;
/**
*
* @author Acer
*/
public class Window extends Canvas implements Runnable{
private static final long serialVersionUID=1L;
private Thread thread;
private boolean running=false;
private KeyboardHandler KListener=new KeyboardHandler(this);
public Player player=new Player(100,100,64,64);
public Window(String Title){
JFrame frame=new JFrame(Title);
frame.setSize(800,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(true);
frame.setVisible(true);
frame.add(this);
}
public void start(){
running=true;
thread=new Thread(this);
thread.start();
}
public void stop(){
running=false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run(){
while(running){
tick();
Render();
}
}
public void tick(){
player.tick();
}
public void Render(){
BufferStrategy bs=this.getBufferStrategy();
if(bs==null){
this.createBufferStrategy(2);
bs=this.getBufferStrategy();
}
Graphics g=bs.getDrawGraphics();
g.setColor(Color.white);
g.fillRect(0,0,this.getWidth(), this.getHeight());
player.Render(g);
bs.show();
g.dispose();
}
}
KeyboardHandler.java
package inputs;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import Core.Window;
public class KeyboardHandler implements KeyListener {
private Window w;
public KeyboardHandler(Window w){
this.w=w;
w.addKeyListener(this);
w.setFocusable(true);
w.requestFocusInWindow();
}
boolean MovingLeft=false;
//@Override
public void keyPressed(KeyEvent e) {
int key=e.getKeyCode();
if (key == KeyEvent.VK_D) {
w.player.velx=w.player.speed;
MovingLeft=false;
}
if(key==KeyEvent.VK_A){
w.player.velx=-w.player.speed;
MovingLeft=true;
}
}
//@Override
public void keyReleased(KeyEvent e) {
int key=e.getKeyCode();
if (key == KeyEvent.VK_D && !MovingLeft) {
w.player.velx=0;
}
if (key == KeyEvent.VK_A && MovingLeft) {
w.player.velx=0;
}
}
//@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
}
Player.java
package objects;
import java.awt.Color;
import java.awt.Graphics;
public class Player {
public int x,y,width, height;
public int velx, vely;
public int speed=1;
public Player(int x, int y, int width, int height){
this.x=x;
this.y=y;
this.width=width;
this.height=height;
}
public void tick(){
// player.tick();
x+=velx;
y+=vely;
}
public void Render(Graphics g){
g.setColor(Color.RED);
g.fillRect(x, y, height, width);
}
}
Tidak ada komentar:
Posting Komentar