Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package crust.Main;
- /*
- * Crust2d
- *
- * Written by Trevor Hall
- *
- * Project Started: 2/23/2011
- *
- * This is the main window, and the start of my project,
- * all implementations will be based around it
- *
- */
- import java.awt.*;
- import java.awt.event.KeyAdapter;
- import java.awt.event.KeyEvent;
- import java.awt.image.*;
- import java.awt.Event;
- import java.util.Random;
- import javax.swing.JFrame;
- public class Game {
- /*
- * This is the serialVersion UID
- * It does not prevent pregnancy
- * You are one letter off
- */
- private static final long serialVersionUID = 1L;
- /*
- * no arguments needed as of 2/23/11
- * @param args
- */
- /*
- * --This is where the important stuff starts--
- */
- //Main Objects
- // Create Colors
- public static Color sky = new Color(135,206,250);
- public static Color groundA = new Color(205,133,63);
- public static void main( String[] args ) {
- // Create game jFRAME
- JFrame app = new JFrame();
- app.setIgnoreRepaint( true );
- app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
- // Create canvas for painting...
- Canvas canvas = new Canvas();
- canvas.setIgnoreRepaint( true );
- canvas.setSize( 640, 480 );
- // Add canvas to game window...
- app.add( canvas );
- app.pack();
- app.setVisible( true );
- // Create BackBuffer...
- canvas.createBufferStrategy( 2 );
- BufferStrategy buffer = canvas.getBufferStrategy();
- // Get graphics configuration...
- GraphicsEnvironment ge =
- GraphicsEnvironment.getLocalGraphicsEnvironment();
- GraphicsDevice gd = ge.getDefaultScreenDevice();
- GraphicsConfiguration gc = gd.getDefaultConfiguration();
- // Create off-screen drawing surface
- BufferedImage bi = gc.createCompatibleImage( 640, 480 );
- BufferedImage playfield = gc.createCompatibleImage( 1000,1000 );
- // Objects needed for rendering...
- Graphics graphics = null;
- Graphics2D g2d = null;
- Graphics2D gpf = null;
- Color background = Color.BLACK;
- Random rand = new Random();
- gpf = playfield.createGraphics();
- gpf.setColor(sky);
- gpf.fillRect(0, 0, 999, 999);
- gpf.setColor(groundA);
- gpf.fillRect(0,300,999,699);
- gpf.setFont( new Font( "Courier New", Font.PLAIN, 72 ) );
- gpf.drawString( String.format( "THIN CRUST LAYER" ), 0, 200 );
- // View position
- int rx = 0;
- int ry = 0;
- // Variables for FPS
- int fps = 0;
- int frames = 0;
- long totalTime = 0;
- long curTime = System.currentTimeMillis();
- long lastTime = curTime;
- while( true ) {
- try {
- // count Frames per second...
- lastTime = curTime;
- curTime = System.currentTimeMillis();
- totalTime += curTime - lastTime;
- if( totalTime > 1000 ) {
- totalTime -= 1000;
- fps = frames;
- frames = 0;
- }
- ++frames;
- int rnd = rand.nextInt(15);
- if(rnd == 4){
- // gpf.drawString( String.format( "CRUST2d" ), 200, 100 );
- }
- // clear back buffer...
- g2d = bi.createGraphics();
- g2d.setColor( background );
- g2d.fillRect( 0, 0, 639, 479 );
- playfield = TickPhys(playfield);
- // display frames per second...
- g2d.setFont( new Font( "Courier New", Font.PLAIN, 12 ) );
- g2d.setColor( Color.GREEN );
- g2d.drawImage( playfield, rx, ry, null );
- g2d.drawString( String.format( "FPS: %s", fps ), 20, 20 );
- // Blit image and flip...
- graphics = buffer.getDrawGraphics();
- graphics.drawImage( bi, 0, 0, null );
- if( !buffer.contentsLost() )
- buffer.show();
- // Let the OS have a little time...
- Thread.yield();
- } finally {
- // release resources
- if( graphics != null )
- graphics.dispose();
- if( g2d != null )
- g2d.dispose();
- }
- }
- }
- public static BufferedImage TickPhys(BufferedImage sand_buffer)
- {
- Random rand = new Random();
- int dir;
- for (int y = 479; y > 0; --y)
- {
- for(int x = 639; x > 0; --x)
- {
- if(sand_buffer.getRGB(x, y) == groundA.getRGB())
- {
- dir = (rand.nextInt(3) - 1);
- if(sand_buffer.getRGB(x+dir, y+1) == sky.getRGB()){
- sand_buffer.setRGB(x+dir, y+1, groundA.getRGB());
- sand_buffer.setRGB(x, y, sky.getRGB());
- }
- else
- {
- if(sand_buffer.getRGB(x+dir, y+1) != groundA.getRGB()){
- //if(getpixel(sand_buffer, x+dir, y) == backcol){
- // putpixel(sand_buffer, x+dir, y, snowcol);
- // putpixel(sand_buffer, x, y, backcol);
- //}
- }
- }
- }
- }
- }
- return sand_buffer;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement