Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.zeroo;
- import me.zeroo.graphics.window;
- import java.awt.*;
- import java.awt.image.BufferStrategy;
- import java.util.Random;
- public class Game extends Canvas implements Runnable {
- public static void say(String m) { System.out.println("[System] " + m); }
- private int[] windowSize = {
- 720,
- 720 / 12 * 9
- };
- private String[] info = {
- "dunno",
- "BETA 1.0",
- "Zeroo"
- };
- Thread thread;
- public static boolean running = false;
- public synchronized void start() {
- if(running) return;
- try {
- say("Attempting to create thread...");
- thread = new Thread(this);
- say("Attempting to start thread...");
- thread.start();
- } catch (Exception e) {
- e.printStackTrace();
- }
- running = true;
- }
- public synchronized void stop() {
- if(!running) return;
- try {
- say("Attempting to stop thread...");
- thread.join();
- } catch (Exception e) {
- e.printStackTrace();
- }
- running = false;
- }
- private void tick() {}
- private void render() {
- BufferStrategy bs = getBufferStrategy();
- if(bs == null) {
- createBufferStrategy(3);
- } else {
- try {
- Graphics g = bs.getDrawGraphics();
- //--- simple terrain
- g.setColor(Color.cyan);
- g.fillRect(0, 0, windowSize[0], windowSize[1]);
- //--- dirt
- g.setColor(new Color(205,133,63));
- g.fillRect(0, windowSize[0] / 2, windowSize[0], windowSize[1] / 2);
- g.setColor(Color.black);
- Random random = new Random();
- for(int i = 0; i < 500; i++) {
- int x = random.nextInt(windowSize[0] + 1 - windowSize[1]) + windowSize[1];
- int y = random.nextInt(windowSize[0] + 1 - windowSize[1]) + windowSize[1];
- g.fillRect(x, y, 1, 1);
- }
- // === DRAW === //
- // === DRAW === //
- bs.show();
- g.dispose();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- @Override
- public void run() {
- 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;
- //say("FPS: "+ frames);
- frames = 0;
- }
- }
- stop();
- }
- public Game() {
- new window(info[0] + " " + info[1], windowSize[0], windowSize[1], this);
- run();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement