
geom dsh
This game is from ldjam.com, which is currently unavailable.
You can try viewing the original page on Web Archive (may not load correctly).
You can try viewing the original page on Web Archive (may not load correctly).
geom dsh
*An entire game fit within the limited space of a tweet!*
Full game code:
```
o=[0,3,7],h=b=f=0,s=2,setup=_=>createCanvas(400,400),draw=_=>{R=rect,f++,s+=.001,background(9),
R(0,h,20),7<h&&(y-=.5,h+=y),(o).map(p=>{x=99*((p-s*f/99)%6+5.9),R(x,0,20),9>h&&9>x&&(s=2,f=0)})
,b>(c=s*99|0)||(b=c),fill(255),text(`#LDJam ${b} ${c}`,0,99)},keyPressed=_=>0<h||(y=8,h=8)
```
You can paste that code into the p5js editor to run the game.
Controls:
- Press any key to jump
- Die to restart
Documented code
Here's the documented code. Had to use a lot of tricks and cut a lot to get the game under 280 characters.
```
// Initialize obstacle array.
o=[0,3,7],
// Initialize height, best score and frame count.
h=b=f=0,
// Initialize speed.
s=2,
// Other variables like the current score and the vertical speed get initialized when they're first set.
// Create the canvas for the game.
setup=_=>createCanvas(400,400),
draw=_=>{
// Alias function name that are used more than once.
R=rect,
// Update frame count
f++,
// Slowly increase speed.
s+=.001,
// Clear the canvas. 9 is a very dark grey instead of 0 (black) to be slightly different to black but effectively it's still black.
background(9),
// Draw the player.
R(0,h,20),
// If the player is in the air...
// 7 is the height of the cube just before we reach the ground,
// Which means the last time we run this, the player will be on the ground.
// This avoids code that checks if the player is touching the ground.
// Uses the short-circuit behavior of the && operator to acts as a branch statement.
7<h&&(
// Apply gravity to vertical speed.
y-=.5,
// Apply vertical speed to height
h+=y
),
// For each obstacle. p is the position of the obstacle.
(o).map(p=>{
// Calculate the x position of the obstacle.
x=99*(
// An initial position that moves with the speed * frame count
(p-s*f/99)
// This uses module to make the obstacles loop around the screen.
// It also abuses the way negative modulos work to make some obstacles appear later.
// Because we add 5.9 at the end, the x position will only be 0 when (p-s*f) is negative.
// So if we pick larger values for p, those obstacles will be delayed,
// so the game gets more difficult over time.
// These numbers are also picked so that obstacles loop just after they reach the end of the screen,
// so the collision check use less characters.
%6+5.9),
// Draw the obstacle
R(x,0,20),
// Check collsion:
// If `h < 9` (the player is roughly on the ground) and
// `x < 9` (the obstacle is near the player)
// The obstacle check can be one-sided because of how the obstacles loop.
9>h&&9>x&&
// Reset the game by resetting the speed and frame count.
(s=2,f=0)}
),
// If the best score is less than the score
b>(
// Turn the speed into a score variable,
// multiplying to be a nicer number and converting to integer.
c=s*99|0
// Update the best score
)||(b=c),
// Text color to white
fill(255),
// Draw both the current score and the best one
text(`#LDJam ${b} ${c}`,0,99)
},
// Keyboard input: When any key is pressed...
keyPressed=_=>
// If the player is on the ground
0<h||(
// Set the verticle speed to the initial jumping speed
y=8,
// We also need to put the player above the 7<h check earlier
// which is used to see if they're jumping or not.
h=8
)
```
Ended up with enough space to fit the hashtag in there :)