Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package temp;
- import java.awt.AWTEvent;
- import java.awt.Color;
- import java.awt.Graphics2D;
- import java.awt.event.KeyEvent;
- import java.awt.image.BufferStrategy;
- import java.util.ArrayList;
- import java.util.Random;
- import javax.swing.JFrame;
- public class RandomPlatforms extends JFrame
- {
- private static final int TICK_DELAY = 30000000;
- private static final int TICKS_PER_SECOND = 1000000000 / TICK_DELAY;
- static final float PIXELS_PER_METRE = 10;
- static final float MAX_X_LEVEL = 10000;
- static final float SCREEN_HEIGHT = 1000;
- static final float SCREEN_WIDTH = 1000;
- static final float SCREEN_WIDTH_DIV_2 = SCREEN_WIDTH / 2;
- static final float START_X = SCREEN_WIDTH_DIV_2;
- static final float START_Y = SCREEN_HEIGHT / 2;
- static final float PLAYER_PLATFORM_ACCELERATION = 2 * PIXELS_PER_METRE / TICKS_PER_SECOND;
- static final float PLAYER_AIR_ACCELERATION = PLAYER_PLATFORM_ACCELERATION;
- static final float MAX_PLAYER_X_VELOCITY = 60F * PIXELS_PER_METRE / TICKS_PER_SECOND;
- static final float SCROLL_X_RATE = 10F * PIXELS_PER_METRE / TICKS_PER_SECOND;
- static final float GRAVITY = 9.8F * PIXELS_PER_METRE / TICKS_PER_SECOND;
- static final float PLAYER_JUMP_ACCELERATION = 100F * PIXELS_PER_METRE / TICKS_PER_SECOND;
- static final int DESIRED_PLATFORMS_ON_SCREEN = 8;
- static final int PLAYER_WIDTH = 20;
- static final int PLAYER_HEIGHT = 40;
- static final int NO_OF_PLATFORM_ROADS = 2;
- boolean[] key = new boolean[Short.MAX_VALUE << 1];
- BufferStrategy strategy;
- Random rand = new Random(1);
- class Platform
- {
- static final float MIN_WIDTH = 30;
- static final float MAX_WIDTH = 400;
- static final float STANDARD_WIDTH = 200;
- static final float STANDARD_HEIGHT = 20;
- float x;
- float y;
- float width;
- float height;
- boolean generatedNextPlatform = false;
- Color colour = new Color(0xAAAAAA);
- Platform(float x, float y)
- {
- this(x, y, STANDARD_WIDTH);
- }
- Platform(float x, float y, float length)
- {
- this(x, y, length, STANDARD_HEIGHT);
- }
- Platform(float x, float y, float length, float height)
- {
- this.x = x;
- this.y = y;
- this.width = length;
- this.height = height;
- }
- /**
- * Simple collision player-platform detection
- *
- * 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.
- *
- *
- * @param player
- * @return
- */
- boolean collide(Player player)
- {
- if (player.x >= x && player.x <= x + width)
- {
- if (player.y == y || (player.oldY < y && player.y >= y))
- {
- player.y = y;
- return true;
- }
- }
- return false;
- }
- }
- class Player
- {
- float x, y, oldX, oldY, velX, velY;
- }
- static public void main(String args[])
- {
- new RandomPlatforms();
- }
- RandomPlatforms()
- {
- setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- enableEvents(AWTEvent.KEY_EVENT_MASK);
- setSize((int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
- setVisible(true);
- createBufferStrategy(2);
- strategy = getBufferStrategy();
- Player player = new Player();
- player.x = START_X;
- player.y = START_Y;
- float screenX = 0;
- boolean onPlatform = false;
- boolean decelerating = true;
- ArrayList<Platform> platforms = new ArrayList<RandomPlatforms.Platform>();
- ArrayList<Platform> discardedPlatforms = new ArrayList<RandomPlatforms.Platform>();
- ArrayList<Platform> addedPlatforms = new ArrayList<RandomPlatforms.Platform>();
- for (int i = 0; i < NO_OF_PLATFORM_ROADS; i++)
- {
- Platform platform = new Platform(START_X, START_Y);
- platform.colour = new Color(rand.nextInt(0xFFFFFF));
- platforms.add(platform);
- }
- long lastTime = System.nanoTime();
- while (player.x < MAX_X_LEVEL)
- {
- // update player position
- player.oldX = player.x;
- player.oldY = player.y;
- player.x += player.velX;
- player.y += player.velY;
- if (player.y>SCREEN_HEIGHT+PLAYER_HEIGHT)
- {
- break;
- }
- // check to see whether player is on or collided with a platform
- for (Platform platform : platforms)
- {
- onPlatform = platform.collide(player);
- if (onPlatform)
- {
- break;
- }
- }
- float playerAcc;
- // on a platform the following occurs:
- // 1. player Y velocity is 0
- // 2. player can initiate a jump
- // 3. when a player stops pressing movement keys, the player velocity rapidly decelerates
- if (onPlatform)
- {
- player.velY = 0;
- decelerating = true;
- if (key[KeyEvent.VK_UP])
- {
- player.velY -= PLAYER_JUMP_ACCELERATION;
- }
- playerAcc = PLAYER_PLATFORM_ACCELERATION;
- }
- // off a platform the following occurs:
- // 1. gravity increases player's Y velocity
- // 2. when a player stops pressing movement keys, the player keeps velocity unchanged
- else
- {
- decelerating = false;
- player.velY += GRAVITY;
- playerAcc = PLAYER_AIR_ACCELERATION;
- }
- if (key[KeyEvent.VK_LEFT])
- {
- player.velX -= playerAcc;
- // ensure deceleration does not occur when initiating or continuing left movement.
- if (player.velX <= 0)
- {
- decelerating = false;
- }
- }
- if (key[KeyEvent.VK_RIGHT])
- {
- player.velX += playerAcc;
- // ensure deceleration does not occur when initiating or continuing right movement.
- if (player.velX >= 0)
- {
- decelerating = false;
- }
- }
- // clamp player X velocity
- player.velX = player.velX > MAX_PLAYER_X_VELOCITY ? MAX_PLAYER_X_VELOCITY : player.velX;
- player.velX = player.velX < -MAX_PLAYER_X_VELOCITY ? -MAX_PLAYER_X_VELOCITY : player.velX;
- // rapidly decelerate player when allowed and the player has stopped pressing a movement key
- if (decelerating)
- {
- player.velX = player.velX * 0.1F;
- if (player.velX < 0.01)
- {
- player.velX = 0;
- }
- }
- // 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.
- screenX += player.x >= screenX + SCREEN_WIDTH_DIV_2 ? Math.max(SCROLL_X_RATE, player.velX) : SCROLL_X_RATE;
- // Generate Platforms as the world scrolls...
- for (Platform platform : platforms)
- {
- // generate new platform if this platform is fully exposed and has not yet spawned a new platform
- if (!platform.generatedNextPlatform && platform.x + platform.width < screenX + SCREEN_WIDTH)
- {
- float apogeeTicks = PLAYER_JUMP_ACCELERATION / GRAVITY; // the number of game ticks at which the player's jump is at its highest
- 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
- // intialise a random number
- float t = rand.nextFloat();
- // pick a sector of the player's jump arc:
- switch (rand.nextInt(4))
- {
- // sector where the player is increasing altitude
- case 0:
- case 1:
- t *= apogeeTicks;
- break;
- // sector where the player has started to decrease altitude to 1/3 the distance to out side of screen
- case 2:
- t = apogeeTicks + t * (floorTicks / 3);
- break;
- // the remaining arc.
- case 3:
- t = apogeeTicks + (floorTicks / 3) + t * (floorTicks * 2 / 3 - apogeeTicks);
- break;
- }
- float apogeeY = platform.y - PLAYER_JUMP_ACCELERATION * apogeeTicks + (GRAVITY / 2) * apogeeTicks * apogeeTicks; // the height of the top of the players jump arc
- float yAtTimeT = platform.y - PLAYER_JUMP_ACCELERATION * t + (GRAVITY / 2) * t * t; // the height at the chosen point on the jump arc.
- // compress the jump arc horizontally to choose the gap between the new platform and current platform...
- float tt = t;
- switch (rand.nextInt(3))
- {
- // near
- case 0:
- tt *= 0.333F;
- break;
- // medium
- case 1:
- tt *= 0.666F;
- break;
- // default is far
- }
- float newPlatformX = platform.x + platform.width + MAX_PLAYER_X_VELOCITY * tt; // the new platform X position
- // 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
- // any Y value between the apogee and the bottom of the screen can be used.
- float newPlatformY = (t < apogeeTicks) ? apogeeY + rand.nextFloat() * (SCREEN_HEIGHT - apogeeY) : apogeeY + rand.nextFloat() * (SCREEN_HEIGHT - yAtTimeT);
- // create the new platform object
- Platform newPlatform = new Platform(newPlatformX, newPlatformY, (Platform.MAX_WIDTH - Platform.MIN_WIDTH) * rand.nextFloat() + Platform.MIN_WIDTH);
- newPlatform.colour = platform.colour;
- addedPlatforms.add(newPlatform);
- platform.generatedNextPlatform = true;
- }
- // find any platforms outside of the screen
- if (platform.x + platform.width < screenX)
- {
- discardedPlatforms.add(platform);
- }
- }
- // delete any platforms outside of the screen
- for (Platform platform : discardedPlatforms)
- {
- platforms.remove(platform);
- }
- discardedPlatforms.clear();
- // add any new platforms created this tick to the collectin of platforms
- for (Platform platform : addedPlatforms)
- {
- platforms.add(platform);
- }
- addedPlatforms.clear();
- // render
- Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
- g.setColor(new Color(0x333333));
- g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
- for (Platform platform : platforms)
- {
- g.setColor(platform.colour);
- g.fill3DRect((int) (platform.x - screenX), (int) platform.y, (int) platform.width, (int) platform.height, true);
- }
- g.setColor(Color.YELLOW);
- g.fillOval((int) (player.x - screenX), (int) (player.y - PLAYER_HEIGHT), PLAYER_WIDTH, PLAYER_HEIGHT);
- strategy.show();
- /**
- * give other processes some CPU time
- */
- do
- {
- Thread.yield();
- } while (System.nanoTime() - lastTime < 0);
- lastTime += (TICK_DELAY);
- }
- Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
- if (player.x>MAX_X_LEVEL)
- {
- g.setColor(new Color(0x11EE11));
- g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
- g.setColor(Color.WHITE);
- g.drawString("YOU WIN", SCREEN_WIDTH_DIV_2, START_Y);
- }
- else
- {
- g.setColor(new Color(0xEE1111));
- g.fillRect(0, 0, (int) SCREEN_WIDTH, (int) SCREEN_HEIGHT);
- g.setColor(Color.WHITE);
- g.drawString("YOU LOOSE", SCREEN_WIDTH_DIV_2, START_Y);
- }
- strategy.show();
- }
- public void processEvent(AWTEvent e)
- {
- boolean down = false;
- switch (e.getID())
- {
- case KeyEvent.KEY_PRESSED:
- down = true;
- case KeyEvent.KEY_RELEASED:
- key[((KeyEvent) e).getKeyCode()] = down;
- break;
- }
- super.processEvent(e);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment