Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.00 KB | None | 0 0
  1. GameController.java
  2. Main class
  3.  
  4. package com.gradedunit.asteroids.window;
  5.  
  6. import com.gradedunit.asteroids.gameframework.ObjectId;
  7. import com.gradedunit.asteroids.objects.ObjectController;
  8. import com.gradedunit.asteroids.objects.Player;
  9.  
  10. import java.awt.*;
  11. import java.awt.image.BufferStrategy;
  12.  
  13. public class GameController extends Canvas implements Runnable {
  14.     private boolean running = false;
  15.     private Thread thread;
  16.  
  17.     //objects
  18.     ObjectController objects;
  19.  
  20.     public static void main(String args[]) {
  21.         new Screen(800, 600, "Asteroids", new GameController());
  22.     }
  23.  
  24.     public synchronized void start() {
  25.         if (running) { //if the thread has already started then don't bother with this
  26.             return;
  27.         }
  28.  
  29.         running = true;
  30.         thread = new Thread(this, "GameController Thread"); //creating the main thread that will run the game
  31.         thread.start();
  32.  
  33.     }
  34.  
  35.     @Override
  36.     public void run() {
  37.         init();
  38.         this.requestFocus();
  39.         calcStats();
  40.  
  41.  
  42.     }
  43.  
  44.     private void calcStats() {
  45.         /*
  46.         source for code
  47.         https://www.youtube.com/watch?v=Zh7YiiEuJFw&index=2&list=PLWms45O3n--54U-22GDqKMRGlXROOZtMx
  48.         calculates the frames generated per second
  49.         calculates how many updates have been done (ticks) per second
  50.         the program will try to do 60 updates per second.
  51.          */
  52.         System.out.println(getName() + " Thread has begun");
  53.         long lastTime = System.nanoTime();
  54.         double amountOfTicks = 60.0; //how many frames are allowed to be displayed per second
  55.         double ns = 1000000000 / amountOfTicks;
  56.         double delta = 0;
  57.         long timer = System.currentTimeMillis();
  58.         int updates = 0; //how many updates there have been per second
  59.         int frames = 0; //how many frames have been generated per second
  60.         while (running) {
  61.             long now = System.nanoTime();
  62.             delta += (now - lastTime) / ns;
  63.             lastTime = now;
  64.             while (delta >= 1) {
  65.                 tick();
  66.                 updates++;
  67.                 delta--;
  68.             }
  69.             render();
  70.             frames++;
  71.  
  72.             if (System.currentTimeMillis() - timer > 1000) {
  73.                 timer += 1000;
  74.                 System.out.println("FPS: " + frames + " TICKS: " + updates);
  75.                 frames = 0;
  76.                 updates = 0;
  77.             }
  78.  
  79.         }
  80.     }
  81.  
  82.     private void tick() {
  83.         objects.tick();
  84.     }
  85.  
  86.  
  87.     private void render() {
  88.         BufferStrategy bs = this.getBufferStrategy();
  89.         if(bs == null){
  90.             this.createBufferStrategy(3); //loads the images needed before they are actually required
  91.             return;
  92.         }
  93.         Graphics g = bs.getDrawGraphics(); //get graphics context for buffering
  94.  
  95.         //draw area start
  96.  
  97.         g.setColor(Color.white);
  98.         g.fillRect(0,0, getWidth(), getHeight());
  99.  
  100.         objects.render(g);
  101.  
  102.         //draw area ending
  103.         g.dispose();
  104.         bs.show();
  105.     }
  106.  
  107.     private void init(){
  108.         objects = new ObjectController();
  109.  
  110.         objects.addObject(new Player(new int[]{300, 500, 400}, new int[]{300, 300, 400}, ObjectId.Player));
  111.     }
  112.  
  113. }
  114.  
  115. GameObject.java
  116. super class for player and future game objects to inherit from
  117. package com.gradedunit.asteroids.gameframework;
  118.  
  119. import java.awt.*;
  120. import java.util.LinkedList;
  121.  
  122. //Super class for game objects such as player, asteroid, bullet, etc
  123.  
  124. public abstract class GameObject {
  125.     protected int[] x, y;
  126.     protected float dx = 0, dy = 0;
  127.     protected float theta = 0;
  128.     protected ObjectId id;
  129.     protected Polygon shape;
  130.  
  131.     public GameObject(int[] x, int[] y, ObjectId id){
  132.         this.x = x;
  133.         this.y = y;
  134.         this.id = id;
  135.  
  136.     }
  137.  
  138.     public abstract void tick(LinkedList<GameObject> object); //used for collision detection
  139.     public abstract void render(Graphics g, Polygon shape);
  140.  
  141.     public abstract int[] getX();
  142.     public abstract int[] getY();
  143.     public abstract void setX(int[] x);
  144.     public abstract void setY(int[] y);
  145.  
  146.     public abstract float getDX();
  147.     public abstract float getDY();
  148.     public abstract void setDX(float dx);
  149.     public abstract void setDY(float dy);
  150.  
  151.     public abstract ObjectId getId();
  152.  
  153.     public abstract Polygon getShape();
  154. }
  155.  
  156. Player.java
  157. player class itself
  158. package com.gradedunit.asteroids.objects;
  159.  
  160. import com.gradedunit.asteroids.gameframework.GameObject;
  161. import com.gradedunit.asteroids.gameframework.ObjectId;
  162.  
  163. import java.awt.*;
  164. import java.util.LinkedList;
  165.  
  166. public class Player extends GameObject {
  167.     public Player(int[] x, int[] y, ObjectId id) {
  168.         super(x, y, id);
  169.         this.shape = new Polygon(x, y,3);
  170.     }
  171.  
  172.     @Override
  173.     public void tick(LinkedList<GameObject> object) {
  174.  
  175.     }
  176.  
  177.     @Override
  178.     public void render(Graphics g, Polygon shape) {
  179.         g.drawPolygon(shape);
  180.     }
  181.  
  182.     @Override
  183.     public int[] getX() {
  184.         return x;
  185.     }
  186.  
  187.     @Override
  188.     public int[] getY() {
  189.         return y;
  190.     }
  191.  
  192.     @Override
  193.     public void setX(int[] x) {
  194.         this.x = x;
  195.     }
  196.  
  197.     @Override
  198.     public void setY(int[] y) {
  199.         this.y = y;
  200.     }
  201.  
  202.     @Override
  203.     public float getDX() {
  204.         return dx;
  205.     }
  206.  
  207.     @Override
  208.     public float getDY() {
  209.         return dy;
  210.     }
  211.  
  212.     @Override
  213.     public void setDX(float dx) {
  214.         this.dx = dx;
  215.     }
  216.  
  217.     @Override
  218.     public void setDY(float dy) {
  219.         this.dy = dy;
  220.     }
  221.  
  222.     @Override
  223.     public ObjectId getId() {
  224.         return id;
  225.     }
  226.  
  227.     @Override
  228.     public Polygon getShape() {
  229.         return shape;
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement