Advertisement
Guest User

Untitled

a guest
Jul 28th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. package crust.Main;
  2. /*
  3.  * Crust2d
  4.  *
  5.  * Written by Trevor Hall
  6.  *
  7.  * Project Started: 2/23/2011
  8.  *
  9.  * This is the main window, and the start of my project,
  10.  * all implementations will be based around it
  11.  *
  12.  */
  13. import java.awt.*;
  14. import java.awt.event.KeyAdapter;
  15. import java.awt.event.KeyEvent;
  16. import java.awt.image.*;
  17. import java.awt.Event;
  18. import java.util.Random;
  19. import javax.swing.JFrame;
  20.  
  21. public class Game {
  22.    
  23.     /*
  24.      * This is the serialVersion UID
  25.      * It does not prevent pregnancy
  26.      * You are one letter off
  27.      */
  28.     private static final long serialVersionUID = 1L;
  29.  
  30.     /*
  31.      * no arguments needed as of 2/23/11
  32.      * @param args
  33.      */
  34.    
  35.     /*
  36.      * --This is where the important stuff starts--
  37.      */
  38.     //Main Objects
  39.  
  40.       // Create Colors
  41.     public static Color sky = new Color(135,206,250);
  42.     public static Color groundA = new Color(205,133,63);
  43.  
  44.       public static void main( String[] args ) {
  45.                    
  46.         // Create game jFRAME
  47.         JFrame app = new JFrame();
  48.         app.setIgnoreRepaint( true );
  49.         app.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  50.                    
  51.         // Create canvas for painting...
  52.         Canvas canvas = new Canvas();
  53.         canvas.setIgnoreRepaint( true );
  54.         canvas.setSize( 640, 480 );
  55.                    
  56.         // Add canvas to game window...
  57.         app.add( canvas );
  58.         app.pack();
  59.         app.setVisible( true );
  60.                    
  61.         // Create BackBuffer...
  62.         canvas.createBufferStrategy( 2 );
  63.         BufferStrategy buffer = canvas.getBufferStrategy();
  64.  
  65.         // Get graphics configuration...
  66.         GraphicsEnvironment ge =
  67.         GraphicsEnvironment.getLocalGraphicsEnvironment();
  68.         GraphicsDevice gd = ge.getDefaultScreenDevice();
  69.         GraphicsConfiguration gc = gd.getDefaultConfiguration();
  70.  
  71.         // Create off-screen drawing surface
  72.         BufferedImage bi = gc.createCompatibleImage( 640, 480 );
  73.         BufferedImage playfield = gc.createCompatibleImage( 1000,1000 );
  74.      
  75.        
  76.         // Objects needed for rendering...
  77.         Graphics graphics = null;
  78.         Graphics2D g2d = null;
  79.         Graphics2D gpf = null;
  80.         Color background = Color.BLACK;
  81.         Random rand = new Random();
  82.         gpf = playfield.createGraphics();
  83.         gpf.setColor(sky);
  84.         gpf.fillRect(0, 0, 999, 999);
  85.         gpf.setColor(groundA);
  86.         gpf.fillRect(0,300,999,699);
  87.         gpf.setFont( new Font( "Courier New", Font.PLAIN, 72 ) );
  88.      
  89.         gpf.drawString( String.format( "THIN CRUST LAYER" ), 0, 200 );
  90.        
  91.                    
  92.         // View position
  93.         int rx = 0;
  94.         int ry = 0;
  95.        
  96.         // Variables for FPS
  97.         int fps = 0;
  98.         int frames = 0;
  99.         long totalTime = 0;
  100.         long curTime = System.currentTimeMillis();
  101.         long lastTime = curTime;
  102.                    
  103.         while( true ) {
  104.           try {
  105.             // count Frames per second...
  106.             lastTime = curTime;
  107.             curTime = System.currentTimeMillis();
  108.             totalTime += curTime - lastTime;
  109.             if( totalTime > 1000 ) {
  110.               totalTime -= 1000;
  111.               fps = frames;
  112.               frames = 0;
  113.             }
  114.             ++frames;
  115.             int rnd = rand.nextInt(15);
  116.             if(rnd == 4){
  117.                 //   gpf.drawString( String.format( "CRUST2d" ), 200, 100 );
  118.             }
  119.            
  120.          
  121.             // clear back buffer...
  122.             g2d = bi.createGraphics();
  123.             g2d.setColor( background );
  124.             g2d.fillRect( 0, 0, 639, 479 );
  125.             playfield = TickPhys(playfield);                            
  126.             // display frames per second...
  127.             g2d.setFont( new Font( "Courier New", Font.PLAIN, 12 ) );
  128.             g2d.setColor( Color.GREEN );
  129.             g2d.drawImage( playfield, rx, ry, null );
  130.             g2d.drawString( String.format( "FPS: %s", fps ), 20, 20 );
  131.  
  132.             // Blit image and flip...
  133.             graphics = buffer.getDrawGraphics();
  134.             graphics.drawImage( bi, 0, 0, null );
  135.             if( !buffer.contentsLost() )
  136.               buffer.show();
  137.                                    
  138.             // Let the OS have a little time...
  139.             Thread.yield();
  140.           } finally {
  141.             // release resources
  142.             if( graphics != null )
  143.               graphics.dispose();
  144.             if( g2d != null )
  145.               g2d.dispose();
  146.           }
  147.         }
  148.       }
  149.      
  150.       public static BufferedImage TickPhys(BufferedImage sand_buffer)
  151.       {
  152.          Random rand = new Random();
  153.          int dir;
  154.          for (int y = 479; y > 0; --y)
  155.          {
  156.             for(int x = 639; x > 0; --x)
  157.             {
  158.                if(sand_buffer.getRGB(x, y) == groundA.getRGB())
  159.                {
  160.                      dir = (rand.nextInt(3) - 1);
  161.                       if(sand_buffer.getRGB(x+dir, y+1) == sky.getRGB()){
  162.                           sand_buffer.setRGB(x+dir, y+1, groundA.getRGB());
  163.                           sand_buffer.setRGB(x, y, sky.getRGB());
  164.                       }
  165.                       else
  166.                       {
  167.                         if(sand_buffer.getRGB(x+dir, y+1) != groundA.getRGB()){
  168.                           //if(getpixel(sand_buffer, x+dir, y) == backcol){
  169.                            //  putpixel(sand_buffer, x+dir, y, snowcol);
  170.                            //  putpixel(sand_buffer, x, y, backcol);
  171.                           //}
  172.                         }
  173.                      }
  174.                }
  175.             }
  176.             }
  177.          return sand_buffer;
  178.       }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement