Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. package me.zeroo;
  2.  
  3. import me.zeroo.graphics.window;
  4.  
  5. import java.awt.*;
  6. import java.awt.image.BufferStrategy;
  7. import java.util.Random;
  8.  
  9. public class Game extends Canvas implements Runnable {
  10.  
  11.     public static void say(String m) { System.out.println("[System] " + m); }
  12.  
  13.     private int[] windowSize = {
  14.             720,
  15.             720 / 12 * 9
  16.     };
  17.  
  18.     private String[] info = {
  19.             "dunno",
  20.             "BETA 1.0",
  21.             "Zeroo"
  22.     };
  23.  
  24.     Thread thread;
  25.     public static boolean running = false;
  26.  
  27.     public synchronized void start() {
  28.         if(running) return;
  29.         try {
  30.             say("Attempting to create thread...");
  31.             thread = new Thread(this);
  32.             say("Attempting to start thread...");
  33.             thread.start();
  34.         } catch (Exception e) {
  35.             e.printStackTrace();
  36.         }
  37.         running = true;
  38.     }
  39.     public synchronized void stop() {
  40.         if(!running) return;
  41.         try {
  42.             say("Attempting to stop thread...");
  43.             thread.join();
  44.         } catch (Exception e) {
  45.             e.printStackTrace();
  46.         }
  47.         running = false;
  48.     }
  49.  
  50.     private void tick() {}
  51.     private void render() {
  52.         BufferStrategy bs = getBufferStrategy();
  53.         if(bs == null) {
  54.             createBufferStrategy(3);
  55.         } else {
  56.             try {
  57.  
  58.                 Graphics g = bs.getDrawGraphics();
  59.  
  60.                 //--- simple terrain
  61.                 g.setColor(Color.cyan);
  62.                 g.fillRect(0, 0, windowSize[0], windowSize[1]);
  63.  
  64.                 //--- dirt
  65.                 g.setColor(new Color(205,133,63));
  66.                 g.fillRect(0, windowSize[0] / 2, windowSize[0], windowSize[1] / 2);
  67.  
  68.                 g.setColor(Color.black);
  69.                 Random random = new Random();
  70.                 for(int i = 0; i < 500; i++) {
  71.                     int x = random.nextInt(windowSize[0] + 1 - windowSize[1]) + windowSize[1];
  72.                     int y = random.nextInt(windowSize[0] + 1 - windowSize[1]) + windowSize[1];
  73.                     g.fillRect(x, y, 1, 1);
  74.                 }
  75.  
  76.                 // === DRAW === //
  77.  
  78.                 // === DRAW === //
  79.  
  80.                 bs.show();
  81.                 g.dispose();
  82.             } catch (Exception e) {
  83.                 e.printStackTrace();
  84.             }
  85.         }
  86.     }
  87.  
  88.     @Override
  89.     public void run() {
  90.         long lastTime = System.nanoTime();
  91.         double amountOfTicks = 60.0;
  92.         double ns = 1000000000 / amountOfTicks;
  93.         double delta = 0;
  94.         long timer = System.currentTimeMillis();
  95.         int frames = 0;
  96.         while(running)
  97.         {
  98.             long now = System.nanoTime();
  99.             delta += (now - lastTime) / ns;
  100.             lastTime = now;
  101.             while(delta >=1)
  102.             {
  103.                 tick();
  104.                 delta--;
  105.             }
  106.             if(running)
  107.                 render();
  108.             frames++;
  109.  
  110.             if(System.currentTimeMillis() - timer > 1000)
  111.             {
  112.                 timer += 1000;
  113.                 //say("FPS: "+ frames);
  114.                 frames = 0;
  115.             }
  116.         }
  117.         stop();
  118.     }
  119.  
  120.     public Game() {
  121.         new window(info[0] + " " + info[1], windowSize[0], windowSize[1], this);
  122.         run();
  123.     }
  124.  
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement