Guest User

Untitled

a guest
Oct 7th, 2014
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.82 KB | None | 0 0
  1. package temp;
  2.  
  3. import java.awt.AWTEvent;
  4. import java.awt.Color;
  5. import java.awt.Graphics2D;
  6. import java.awt.event.KeyEvent;
  7. import java.awt.image.BufferStrategy;
  8. import java.util.ArrayList;
  9. import java.util.Random;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. public class RandomPlatforms extends JFrame
  14. {
  15.     private static final int TICK_DELAY = 30000000;
  16.     private static final int TICKS_PER_SECOND = 1000000000 / TICK_DELAY;
  17.     static final float PIXELS_PER_METRE = 10;
  18.     static final float MAX_X_LEVEL = 10000;
  19.     static final float SCREEN_HEIGHT = 1000;
  20.     static final float SCREEN_WIDTH = 1000;
  21.     static final float SCREEN_WIDTH_DIV_2 = SCREEN_WIDTH / 2;
  22.     static final float START_X = SCREEN_WIDTH_DIV_2;
  23.     static final float START_Y = SCREEN_HEIGHT / 2;
  24.     static final float PLAYER_PLATFORM_ACCELERATION = 2 * PIXELS_PER_METRE / TICKS_PER_SECOND;
  25.     static final float PLAYER_AIR_ACCELERATION = PLAYER_PLATFORM_ACCELERATION;
  26.     static final float MAX_PLAYER_X_VELOCITY = 60F * PIXELS_PER_METRE / TICKS_PER_SECOND;
  27.     static final float SCROLL_X_RATE = 10F * PIXELS_PER_METRE / TICKS_PER_SECOND;
  28.     static final float GRAVITY = 9.8F * PIXELS_PER_METRE / TICKS_PER_SECOND;
  29.     static final float PLAYER_JUMP_ACCELERATION = 100F * PIXELS_PER_METRE / TICKS_PER_SECOND;
  30.     static final int DESIRED_PLATFORMS_ON_SCREEN = 8;
  31.     static final int PLAYER_WIDTH = 20;
  32.     static final int PLAYER_HEIGHT = 40;
  33.     static final int NO_OF_PLATFORM_ROADS = 2;
  34.  
  35.     boolean[] key = new boolean[Short.MAX_VALUE << 1];
  36.     BufferStrategy strategy;
  37.     Random rand = new Random(1);
  38.  
  39.     class Platform
  40.     {
  41.         static final float MIN_WIDTH = 30;
  42.         static final float MAX_WIDTH = 400;
  43.         static final float STANDARD_WIDTH = 200;
  44.         static final float STANDARD_HEIGHT = 20;
  45.         float x;
  46.         float y;
  47.         float width;
  48.         float height;
  49.         boolean generatedNextPlatform = false;
  50.         Color colour = new Color(0xAAAAAA);
  51.  
  52.         Platform(float x, float y)
  53.         {
  54.             this(x, y, STANDARD_WIDTH);
  55.         }
  56.  
  57.         Platform(float x, float y, float length)
  58.         {
  59.             this(x, y, length, STANDARD_HEIGHT);
  60.         }
  61.  
  62.         Platform(float x, float y, float length, float height)
  63.         {
  64.             this.x = x;
  65.             this.y = y;
  66.             this.width = length;
  67.             this.height = height;
  68.         }
  69.  
  70.         /**
  71.          * Simple collision player-platform detection
  72.          *
  73.          * Checks to see whether the player X co ordinate is between the platform start and end then checks to see whether the player Y co ordidate has moved from above to just below the top of the platform.
  74.          *
  75.          *
  76.          * @param player
  77.          * @return
  78.          */
  79.         boolean collide(Player player)
  80.         {
  81.             if (player.x >= x && player.x <= x + width)
  82.             {
  83.                 if (player.y == y || (player.oldY < y && player.y >= y))
  84.                 {
  85.                     player.y = y;
  86.                     return true;
  87.                 }
  88.             }
  89.             return false;
  90.         }
  91.  
  92.     }
  93.  
  94.     class Player
  95.     {
  96.         float x, y, oldX, oldY, velX, velY;
  97.     }
  98.  
  99.     static public void main(String args[])
  100.     {
  101.         new RandomPlatforms();
  102.     }
  103.  
  104.     RandomPlatforms()
  105.     {
  106.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  107.         enableEvents(AWTEvent.KEY_EVENT_MASK);
  108.         setSize((int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
  109.         setVisible(true);
  110.         createBufferStrategy(2);
  111.         strategy = getBufferStrategy();
  112.  
  113.         Player player = new Player();
  114.         player.x = START_X;
  115.         player.y = START_Y;
  116.  
  117.         float screenX = 0;
  118.  
  119.         boolean onPlatform = false;
  120.         boolean decelerating = true;
  121.         ArrayList<Platform> platforms = new ArrayList<RandomPlatforms.Platform>();
  122.         ArrayList<Platform> discardedPlatforms = new ArrayList<RandomPlatforms.Platform>();
  123.         ArrayList<Platform> addedPlatforms = new ArrayList<RandomPlatforms.Platform>();
  124.  
  125.         for (int i = 0; i < NO_OF_PLATFORM_ROADS; i++)
  126.         {
  127.             Platform platform = new Platform(START_X, START_Y);
  128.             platform.colour = new Color(rand.nextInt(0xFFFFFF));
  129.             platforms.add(platform);
  130.         }
  131.  
  132.         long lastTime = System.nanoTime();
  133.         while (player.x < MAX_X_LEVEL)
  134.         {
  135.  
  136.             // update player position
  137.             player.oldX = player.x;
  138.             player.oldY = player.y;
  139.             player.x += player.velX;
  140.             player.y += player.velY;
  141.            
  142.             if (player.y>SCREEN_HEIGHT+PLAYER_HEIGHT)
  143.             {
  144.                 break;
  145.             }
  146.            
  147.             // check to see whether player is on or collided with a platform
  148.             for (Platform platform : platforms)
  149.             {
  150.                 onPlatform = platform.collide(player);
  151.                 if (onPlatform)
  152.                 {
  153.                     break;
  154.                 }
  155.             }
  156.  
  157.             float playerAcc;
  158.            
  159.             // on a platform the following occurs:
  160.             // 1. player Y velocity is 0
  161.             // 2. player can initiate a jump
  162.             // 3. when a player stops pressing movement keys, the player velocity rapidly decelerates
  163.             if (onPlatform)
  164.             {
  165.                 player.velY = 0;
  166.                 decelerating = true;
  167.                 if (key[KeyEvent.VK_UP])
  168.                 {
  169.                     player.velY -= PLAYER_JUMP_ACCELERATION;
  170.                 }
  171.  
  172.                 playerAcc = PLAYER_PLATFORM_ACCELERATION;
  173.             }
  174.             // off a platform the following occurs:
  175.             // 1. gravity increases player's Y velocity
  176.             // 2. when a player stops pressing movement keys, the player keeps velocity unchanged
  177.             else
  178.             {
  179.                 decelerating = false;
  180.                 player.velY += GRAVITY;
  181.  
  182.                 playerAcc = PLAYER_AIR_ACCELERATION;
  183.             }
  184.  
  185.             if (key[KeyEvent.VK_LEFT])
  186.             {
  187.                 player.velX -= playerAcc;
  188.                
  189.                 // ensure deceleration does not occur when initiating or continuing left movement.
  190.                 if (player.velX <= 0)
  191.                 {
  192.                     decelerating = false;
  193.                 }
  194.             }
  195.             if (key[KeyEvent.VK_RIGHT])
  196.             {
  197.                 player.velX += playerAcc;
  198.                
  199.                 // ensure deceleration does not occur when initiating or continuing right movement.
  200.                 if (player.velX >= 0)
  201.                 {
  202.                     decelerating = false;
  203.                 }
  204.             }
  205.  
  206.             // clamp player X velocity
  207.             player.velX = player.velX > MAX_PLAYER_X_VELOCITY ? MAX_PLAYER_X_VELOCITY : player.velX;
  208.             player.velX = player.velX < -MAX_PLAYER_X_VELOCITY ? -MAX_PLAYER_X_VELOCITY : player.velX;
  209.  
  210.             // rapidly decelerate player when allowed and the player has stopped pressing a movement key
  211.             if (decelerating)
  212.             {
  213.                 player.velX = player.velX * 0.1F;
  214.                 if (player.velX < 0.01)
  215.                 {
  216.                     player.velX = 0;
  217.                 }
  218.             }
  219.  
  220.             // scroll the world. Will scroll slowly if player is not advancing or will match the player speed if the player reaches the middle of the screen.
  221.             screenX += player.x >= screenX + SCREEN_WIDTH_DIV_2 ? Math.max(SCROLL_X_RATE, player.velX) : SCROLL_X_RATE;
  222.  
  223.             // Generate Platforms as the world scrolls...
  224.             for (Platform platform : platforms)
  225.             {
  226.                 // generate new platform if this platform is fully exposed and has not yet spawned a new platform
  227.                 if (!platform.generatedNextPlatform && platform.x + platform.width < screenX + SCREEN_WIDTH)
  228.                 {
  229.                     float apogeeTicks = PLAYER_JUMP_ACCELERATION / GRAVITY; // the number of game ticks at which the player's jump is at its highest
  230.                     float floorTicks = ((SCREEN_HEIGHT - platform.y) / PIXELS_PER_METRE) / GRAVITY + apogeeTicks; // the number of game ticks when the play would fall out of the bottom of the screen
  231.  
  232.                     // intialise a random number
  233.                     float t = rand.nextFloat();
  234.  
  235.                     // pick a sector of the player's jump arc:
  236.                     switch (rand.nextInt(4))
  237.                     {
  238.                     // sector where the player is increasing altitude
  239.                     case 0:
  240.                     case 1:
  241.                         t *= apogeeTicks;
  242.                         break;
  243.                     // sector where the player has started to decrease altitude to 1/3 the distance to out side of screen
  244.                     case 2:
  245.                         t = apogeeTicks + t * (floorTicks / 3);
  246.                         break;
  247.                     // the remaining arc.
  248.                     case 3:
  249.                         t = apogeeTicks + (floorTicks / 3) + t * (floorTicks * 2 / 3 - apogeeTicks);
  250.                         break;
  251.  
  252.                     }
  253.  
  254.                     float apogeeY = platform.y - PLAYER_JUMP_ACCELERATION * apogeeTicks + (GRAVITY / 2) * apogeeTicks * apogeeTicks; // the height of the top of the players jump arc
  255.                     float yAtTimeT = platform.y - PLAYER_JUMP_ACCELERATION * t + (GRAVITY / 2) * t * t; // the height at the chosen point on the jump arc.
  256.  
  257.                     // compress the jump arc horizontally to choose the gap between the new platform and current platform...
  258.                     float tt = t;
  259.                     switch (rand.nextInt(3))
  260.                     {
  261.                     // near
  262.                     case 0:
  263.                         tt *= 0.333F;
  264.                         break;
  265.                     // medium
  266.                     case 1:
  267.                         tt *= 0.666F;
  268.                         break;
  269.                     // default is far
  270.                     }
  271.  
  272.                     float newPlatformX = platform.x + platform.width + MAX_PLAYER_X_VELOCITY * tt; // the new platform X position
  273.  
  274.                     // new platform Y position. if the t value is such that it is when the player is jumping then only values between the current platform Y and the jump apogee can be used otherwise
  275.                     // any Y value between the apogee and the bottom of the screen can be used.
  276.                     float newPlatformY = (t < apogeeTicks) ? apogeeY + rand.nextFloat() * (SCREEN_HEIGHT - apogeeY) : apogeeY + rand.nextFloat() * (SCREEN_HEIGHT - yAtTimeT);
  277.  
  278.                     // create the new platform object
  279.                     Platform newPlatform = new Platform(newPlatformX, newPlatformY, (Platform.MAX_WIDTH - Platform.MIN_WIDTH) * rand.nextFloat() + Platform.MIN_WIDTH);
  280.                     newPlatform.colour = platform.colour;
  281.                     addedPlatforms.add(newPlatform);
  282.  
  283.                     platform.generatedNextPlatform = true;
  284.                 }
  285.  
  286.                 // find any platforms outside of the screen
  287.                 if (platform.x + platform.width < screenX)
  288.                 {
  289.                     discardedPlatforms.add(platform);
  290.                 }
  291.             }
  292.  
  293.             // delete any platforms outside of the screen
  294.             for (Platform platform : discardedPlatforms)
  295.             {
  296.                 platforms.remove(platform);
  297.             }
  298.             discardedPlatforms.clear();
  299.  
  300.             // add any new platforms created this tick to the collectin of platforms
  301.             for (Platform platform : addedPlatforms)
  302.             {
  303.                 platforms.add(platform);
  304.             }
  305.             addedPlatforms.clear();
  306.  
  307.             // render
  308.  
  309.             Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
  310.  
  311.             g.setColor(new Color(0x333333));
  312.             g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
  313.  
  314.             for (Platform platform : platforms)
  315.             {
  316.                 g.setColor(platform.colour);
  317.                 g.fill3DRect((int) (platform.x - screenX), (int) platform.y, (int) platform.width, (int) platform.height, true);
  318.  
  319.             }
  320.  
  321.             g.setColor(Color.YELLOW);
  322.             g.fillOval((int) (player.x - screenX), (int) (player.y - PLAYER_HEIGHT), PLAYER_WIDTH, PLAYER_HEIGHT);
  323.  
  324.             strategy.show();
  325.  
  326.             /**
  327.              * give other processes some CPU time
  328.              */
  329.             do
  330.             {
  331.                 Thread.yield();
  332.             } while (System.nanoTime() - lastTime < 0);
  333.             lastTime += (TICK_DELAY);
  334.         }
  335.        
  336.         Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
  337.    
  338.         if (player.x>MAX_X_LEVEL)
  339.         {
  340.             g.setColor(new Color(0x11EE11));
  341.             g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
  342.             g.setColor(Color.WHITE);
  343.             g.drawString("YOU WIN", SCREEN_WIDTH_DIV_2, START_Y);      
  344.         }
  345.         else
  346.         {
  347.             g.setColor(new Color(0xEE1111));
  348.             g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
  349.             g.setColor(Color.WHITE);
  350.             g.drawString("YOU LOOSE", SCREEN_WIDTH_DIV_2, START_Y);
  351.         }
  352.         strategy.show();
  353.     }
  354.  
  355.     public void processEvent(AWTEvent e)
  356.     {
  357.         boolean down = false;
  358.         switch (e.getID())
  359.         {
  360.         case KeyEvent.KEY_PRESSED:
  361.             down = true;
  362.         case KeyEvent.KEY_RELEASED:
  363.             key[((KeyEvent) e).getKeyCode()] = down;
  364.             break;
  365.         }
  366.         super.processEvent(e);
  367.     }
  368. }
Advertisement
Add Comment
Please, Sign In to add comment