Last active
February 9, 2018 18:54
-
-
Save demodude4u/8f4c698faf3fbf17aa9b10276ac33f09 to your computer and use it in GitHub Desktop.
Bare bones game prototype essentials to make a simple test game in Java2D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Canvas; | |
import java.awt.Color; | |
import java.awt.Dimension; | |
import java.awt.Graphics2D; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import java.awt.event.MouseEvent; | |
import java.awt.event.MouseListener; | |
import java.awt.event.MouseMotionListener; | |
import java.awt.image.BufferStrategy; | |
import javax.swing.JFrame; | |
/** | |
* Lazy and quick hacks to hit the ground running with a game mechanic or idea. | |
* <br> | |
* <br> | |
* A proper code design should be made for the actual game. | |
* | |
* @author Demod | |
*/ | |
public class GamePrototypeTemplate { | |
private static final Dimension SCREEN_SIZE = new Dimension(1024, 1024); | |
public static final int FPS = 30; | |
public static void main(String[] args) throws InterruptedException { | |
// Set up window | |
JFrame frame = new JFrame("Game Prototype Template"); | |
Canvas canvas = new Canvas(); | |
canvas.setPreferredSize(SCREEN_SIZE); | |
canvas.setFocusable(true); | |
frame.getContentPane().add(canvas); | |
frame.pack(); | |
frame.setLocationRelativeTo(null); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setVisible(true); | |
// TODO setup game variables here | |
// Initialize keyboard and mouse events | |
canvas.addMouseListener(new MouseListener() { | |
@Override | |
public void mouseClicked(MouseEvent e) { | |
} | |
@Override | |
public void mouseEntered(MouseEvent e) { | |
} | |
@Override | |
public void mouseExited(MouseEvent e) { | |
} | |
@Override | |
public void mousePressed(MouseEvent e) { | |
} | |
@Override | |
public void mouseReleased(MouseEvent e) { | |
} | |
}); | |
canvas.addMouseMotionListener(new MouseMotionListener() { | |
@Override | |
public void mouseDragged(MouseEvent e) { | |
} | |
@Override | |
public void mouseMoved(MouseEvent e) { | |
} | |
}); | |
canvas.addKeyListener(new KeyListener() { | |
@Override | |
public void keyPressed(KeyEvent e) { | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
}); | |
// Run game loop | |
long lastFrameNanoTime = System.nanoTime(); | |
while (true) { | |
try { | |
// TODO Game Logic | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
// Setup rendering for this frame | |
BufferStrategy bufferStrategy = canvas.getBufferStrategy(); | |
if (bufferStrategy == null) { // Buffers not setup yet | |
canvas.createBufferStrategy(3); // Triple buffering | |
bufferStrategy = canvas.getBufferStrategy(); | |
if (bufferStrategy == null) {// Screen isn't ready | |
Thread.sleep(250); // Give time to initialize | |
lastFrameNanoTime = System.nanoTime(); | |
continue; | |
} | |
} | |
Graphics2D g = (Graphics2D) bufferStrategy.getDrawGraphics(); | |
try { | |
// Background | |
g.setColor(Color.white); | |
g.fillRect(0, 0, canvas.getWidth(), canvas.getHeight()); | |
// TODO Game Rendering | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally { | |
g.dispose(); | |
bufferStrategy.show(); | |
} | |
// Wait until next frame should render | |
long nanoTime = System.nanoTime(); | |
long nextFrameNanoTime = lastFrameNanoTime + (1000000000L) / FPS; | |
if (nextFrameNanoTime - nanoTime > 0) { | |
long timeToSleepNanos = nextFrameNanoTime - nanoTime; | |
Thread.sleep(timeToSleepNanos / 1000000, (int) (timeToSleepNanos % 1000000)); | |
} else { | |
Thread.yield();// Give only a small amount of time for AWT | |
} | |
lastFrameNanoTime = nextFrameNanoTime; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment