Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (538)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (600)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: StonePickaxes (posted 2012-08-05 22:05:28, viewed 144 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126  
package com.natekramber.doors;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;

import com.natekramber.doors.gfx.Screen;

public class Main extends Canvas implements Runnable {
   private static final long serialVersionUID = 1L;
   private static final int width = 400, height = 200, scale = 2;
   private static final String title = "Basic game";
   private boolean running = false;
   private long lastFrame;
   private BufferedImage image;
   
   public static void main(String[] args) {
      Main main = new Main();
      main.setMinimumSize(new Dimension(width * scale, height * scale));
      main.setMaximumSize(new Dimension(width * scale, height * scale));
      main.setPreferredSize(new Dimension(width * scale, height * scale));
      
      JFrame frame = new JFrame(title);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.add(main);
      frame.pack();
      frame.setResizable(false);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
      
      main.start();
   }
   
   private Game game;
   private Screen screen;
   
   public void start() {
      running = true;
      new Thread(this).start();
   }
   
   public void stop() {
      running = false;
   }
   
   public void init() {
      getDelta();
      image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      game = new Game(new InputHandler(this));
   }
   
   public void tick() {
      game.tick(getDelta());
   }
   
   private int getDelta() {
      long time = getTime();
      int delta = (int) (time - lastFrame);
      lastFrame = time;
      return delta;
   }
   
   private long getTime() {
      return System.nanoTime() / 1000000;
   }
   
   public void render() {
      Graphics2D g = (Graphics2D) image.getGraphics();
      BufferStrategy bs = getBufferStrategy();
      if (bs == null) {
         createBufferStrategy(3);
         requestFocus();
         screen = new Screen(width, height, g, "img/sprites.png");
         return;
      }
      
      g.setColor(new Color(0x00));
      g.fillRect(0, 0, width, height);
      
      game.render(screen);
      
      g = (Graphics2D) bs.getDrawGraphics();
      g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
      g.dispose();
      bs.show();
   }
   
   public void run() {
      long lastTime = System.nanoTime();
      double unprocessed = 0;
      double nsPerTick = 1000000000.0 / 60;
      int frames = 0;
      int ticks = 0;
      long lastTimer1 = System.currentTimeMillis();

      init();

      while (running) {
         long now = System.nanoTime();
         unprocessed += (now - lastTime) / nsPerTick;
         lastTime = now;

         while (unprocessed >= 1) {
            ticks++;
            tick();
            unprocessed = 0;
         }
         
         frames++;
         render();
         
         if (System.currentTimeMillis() - lastTimer1 > 1000) {
            lastTimer1 += 1000;
            System.out.println(ticks + " ticks, " + frames + " fps");
            frames = 0;
            ticks = 0;
         }
      }
      System.exit(0);
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (28 views)
2014-12-15 09:26:44

Mr.CodeIt (19 views)
2014-12-14 19:50:38

BurntPizza (35 views)
2014-12-09 22:41:13

BurntPizza (70 views)
2014-12-08 04:46:31

JscottyBieshaar (32 views)
2014-12-05 12:39:02

SHC (44 views)
2014-12-03 16:27:13

CopyableCougar4 (40 views)
2014-11-29 21:32:03

toopeicgaming1999 (108 views)
2014-11-26 15:22:04

toopeicgaming1999 (94 views)
2014-11-26 15:20:36

toopeicgaming1999 (29 views)
2014-11-26 15:20:08
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!