Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- GameController.java
- Main class
- package com.gradedunit.asteroids.window;
- import com.gradedunit.asteroids.gameframework.ObjectId;
- import com.gradedunit.asteroids.objects.ObjectController;
- import com.gradedunit.asteroids.objects.Player;
- import java.awt.*;
- import java.awt.image.BufferStrategy;
- public class GameController extends Canvas implements Runnable {
- private boolean running = false;
- private Thread thread;
- //objects
- ObjectController objects;
- public static void main(String args[]) {
- new Screen(800, 600, "Asteroids", new GameController());
- }
- public synchronized void start() {
- if (running) { //if the thread has already started then don't bother with this
- return;
- }
- running = true;
- thread = new Thread(this, "GameController Thread"); //creating the main thread that will run the game
- thread.start();
- }
- @Override
- public void run() {
- init();
- this.requestFocus();
- calcStats();
- }
- private void calcStats() {
- /*
- source for code
- https://www.youtube.com/watch?v=Zh7YiiEuJFw&index=2&list=PLWms45O3n--54U-22GDqKMRGlXROOZtMx
- calculates the frames generated per second
- calculates how many updates have been done (ticks) per second
- the program will try to do 60 updates per second.
- */
- System.out.println(getName() + " Thread has begun");
- long lastTime = System.nanoTime();
- double amountOfTicks = 60.0; //how many frames are allowed to be displayed per second
- double ns = 1000000000 / amountOfTicks;
- double delta = 0;
- long timer = System.currentTimeMillis();
- int updates = 0; //how many updates there have been per second
- int frames = 0; //how many frames have been generated per second
- while (running) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- tick();
- updates++;
- delta--;
- }
- render();
- frames++;
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- System.out.println("FPS: " + frames + " TICKS: " + updates);
- frames = 0;
- updates = 0;
- }
- }
- }
- private void tick() {
- objects.tick();
- }
- private void render() {
- BufferStrategy bs = this.getBufferStrategy();
- if(bs == null){
- this.createBufferStrategy(3); //loads the images needed before they are actually required
- return;
- }
- Graphics g = bs.getDrawGraphics(); //get graphics context for buffering
- //draw area start
- g.setColor(Color.white);
- g.fillRect(0,0, getWidth(), getHeight());
- objects.render(g);
- //draw area ending
- g.dispose();
- bs.show();
- }
- private void init(){
- objects = new ObjectController();
- objects.addObject(new Player(new int[]{300, 500, 400}, new int[]{300, 300, 400}, ObjectId.Player));
- }
- }
- GameObject.java
- super class for player and future game objects to inherit from
- package com.gradedunit.asteroids.gameframework;
- import java.awt.*;
- import java.util.LinkedList;
- //Super class for game objects such as player, asteroid, bullet, etc
- public abstract class GameObject {
- protected int[] x, y;
- protected float dx = 0, dy = 0;
- protected float theta = 0;
- protected ObjectId id;
- protected Polygon shape;
- public GameObject(int[] x, int[] y, ObjectId id){
- this.x = x;
- this.y = y;
- this.id = id;
- }
- public abstract void tick(LinkedList<GameObject> object); //used for collision detection
- public abstract void render(Graphics g, Polygon shape);
- public abstract int[] getX();
- public abstract int[] getY();
- public abstract void setX(int[] x);
- public abstract void setY(int[] y);
- public abstract float getDX();
- public abstract float getDY();
- public abstract void setDX(float dx);
- public abstract void setDY(float dy);
- public abstract ObjectId getId();
- public abstract Polygon getShape();
- }
- Player.java
- player class itself
- package com.gradedunit.asteroids.objects;
- import com.gradedunit.asteroids.gameframework.GameObject;
- import com.gradedunit.asteroids.gameframework.ObjectId;
- import java.awt.*;
- import java.util.LinkedList;
- public class Player extends GameObject {
- public Player(int[] x, int[] y, ObjectId id) {
- super(x, y, id);
- this.shape = new Polygon(x, y,3);
- }
- @Override
- public void tick(LinkedList<GameObject> object) {
- }
- @Override
- public void render(Graphics g, Polygon shape) {
- g.drawPolygon(shape);
- }
- @Override
- public int[] getX() {
- return x;
- }
- @Override
- public int[] getY() {
- return y;
- }
- @Override
- public void setX(int[] x) {
- this.x = x;
- }
- @Override
- public void setY(int[] y) {
- this.y = y;
- }
- @Override
- public float getDX() {
- return dx;
- }
- @Override
- public float getDY() {
- return dy;
- }
- @Override
- public void setDX(float dx) {
- this.dx = dx;
- }
- @Override
- public void setDY(float dy) {
- this.dy = dy;
- }
- @Override
- public ObjectId getId() {
- return id;
- }
- @Override
- public Polygon getShape() {
- return shape;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement