Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Canvas;
- import java.awt.Color;
- import java.awt.Font;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.Point;
- import java.awt.image.BufferStrategy;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- public class Game extends Canvas implements Runnable {
- public static final long serialVersionUID = 1L;
- public static final short WH = 400, W = 400+6, H = 400+28;
- public static Graphics g;
- public static boolean pause = false;
- public static void main (String[] args) {
- Game game = new Game();
- game.addKeyListener(new Input());
- game.setFocusable(true);
- BufferedImage icon = new BufferedImage(32,32,BufferedImage.TYPE_INT_RGB);
- Graphics2D g2d = icon.createGraphics();
- g2d.setColor(Player.C1);
- g2d.fillRect(0,0,icon.getWidth(),icon.getHeight()/2);
- g2d.setColor(Player.C2);
- g2d.fillRect(0,icon.getHeight()/2,icon.getWidth(),icon.getHeight()/2);
- JFrame frame = new JFrame("The Astronaut");
- frame.add(game);
- frame.pack();
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setSize(W,H);
- frame.setResizable(false);
- frame.addKeyListener(new Input());
- frame.setLocationRelativeTo(null);
- frame.setIconImage(icon);
- frame.setVisible(true);
- Level.load(Spawn.levelX,Spawn.levelY);
- Spawn.respawn();
- game.start();
- }
- public void start () {
- setCursor(getToolkit().createCustomCursor(new BufferedImage(1, 1, BufferedImage.TYPE_4BYTE_ABGR),new Point(0,0),""));
- new Thread(this).start();
- }
- public void run () {
- long before = System.nanoTime(), now;
- double unprocessed = 0;
- long counter = System.currentTimeMillis();
- byte a = 0;
- while (true) {
- if (!pause) {
- now = System.nanoTime();
- unprocessed += (now-before)*60/1_000_000_000D;
- while (unprocessed >= 1) {
- update();
- render();
- unprocessed--;
- a++;
- }
- before = now;
- if (System.currentTimeMillis()-counter >= 1_000) {
- System.out.println("UPS/FPS: " + a);
- counter = System.currentTimeMillis();
- a = 0;
- }
- } else {
- before = System.nanoTime();
- unprocessed = 0;
- a = 0;
- counter = System.currentTimeMillis();
- Input.left = false;
- Input.right = false;
- Input.up = false;
- Input.down = false;
- Input.ctrl = false;
- if (!Input.p){
- pause = false;
- }
- }
- }
- }
- public void update () {
- if (Input.p || !hasFocus()){
- pause = true;
- Input.p = true;
- return;
- }
- Level.update();
- }
- public void render () {
- BufferStrategy bs = getBufferStrategy();
- if (bs == null) {
- createBufferStrategy(3);
- return;
- }
- g = bs.getDrawGraphics();
- Level.render();
- if (pause) {
- g.setColor(new Color(0F,0F,0F,1/3F));
- g.fillRect(0,0,Game.WH,Game.WH);
- g.setFont(new Font("Calibri", Font.PLAIN, 20));
- g.setColor(Color.WHITE);
- g.drawString("Press P to continue",Game.WH/2,Game.WH/2);
- }
- g.dispose();
- bs.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement