Skip to content

Instantly share code, notes, and snippets.

@AnatoliiStepaniuk
Created June 2, 2015 19:08
Show Gist options
  • Save AnatoliiStepaniuk/c6fe63bea242f05a52e8 to your computer and use it in GitHub Desktop.
Save AnatoliiStepaniuk/c6fe63bea242f05a52e8 to your computer and use it in GitHub Desktop.
package com.tutorial.main;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferStrategy;
import java.util.Random;
public class Game extends Canvas implements Runnable{
public static Handler handler = new Handler();
private static final long serialVersionUID = 1550691097823471818L;
public static final int WIDTH = 640, HEIGHT = WIDTH / 12 * 9;
private Thread thread;
private boolean running = false;
public static HUD hud = new HUD();
private Spawn spawner;
public static Menu menu;
private Help help;
public STATE gameState = STATE.Menu;
public Game() {
menu = new Menu(this);
help = new Help(this);
this.addKeyListener(new KeyInput(this));
this.addMouseListener(menu);
this.addMouseListener(help);
new Window(WIDTH, HEIGHT, "Let's Build a Game!", this);
spawner = new Spawn();
}
public enum STATE {
Menu,
Help,
Game
};
public synchronized void start(){
thread = new Thread(this);
thread.start();
running = true;
}
public synchronized void stop(){
try{
thread.join();
running = false;
}catch(Exception e){
e.printStackTrace();
}
}
public void run(){
this.requestFocus();
long lastTime = System.nanoTime();
double amountOfTicks = 60.0;
double ns = 1000000000 / amountOfTicks;
double delta = 0;
long timer = System.currentTimeMillis();
int frames = 0;
while(running){
long now = System.nanoTime();
delta += (now - lastTime) / ns;
lastTime = now;
while(delta >= 1){
tick();
delta--;
}
if(running)
render();
frames++;
if(System.currentTimeMillis() - timer > 1000){
timer += 1000;
//System.out.println("FPS: " + frames);
frames = 0;
}
}
stop();
}
private void tick(){
handler.tick();
if(gameState == STATE.Game){
hud.tick();
spawner.tick();
}else if(gameState == STATE.Menu){
menu.tick();
}else if(gameState == STATE.Help){
help.tick();
}
}
private 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);
handler.render(g);
if(gameState == STATE.Game){
hud.render(g);
}
else if(gameState == STATE.Menu){
menu.render(g);
}else if(gameState == STATE.Help){
help.render(g);
}
g.dispose();
bs.show();
} // end render
public static int clamp(int var, int min, int max){
if (var >= max)
return var = max;
else if (var <= min)
return var = min;
else
return var;
}
public static void main(String[] args){
new Game();
}
}
package com.tutorial.main;
import java.awt.Graphics;
import java.util.LinkedList;
public class Handler {
LinkedList<GameObject> objectList = new LinkedList<GameObject>();
public void tick(){
for(int i = 0; i < objectList.size(); i++){
GameObject tempObject = objectList.get(i);
tempObject.tick();
}
}
public void render(Graphics g){
for(int i = 0; i < objectList.size(); i++){
if(objectList.size() != 0){
GameObject tempObject = objectList.get(i);
tempObject.render(g);
}
}
}
public void addObject(GameObject object){
objectList.add(object);
}
public void removeObject(GameObject object){
objectList.remove(object);
}
public void clearEnemies(){
for(int i = 0; i < objectList.size(); i++){
GameObject tempObject = objectList.get(i);
if(tempObject.getId() == ID.Player){
objectList.clear();
addObject(new Player(tempObject.getX(),tempObject.getY(), ID.Player));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment