# Pico-8 Coding Challenges
###### tags: `code club`
## Coding Fundamentals
There are four things every programming language needs, and virtually all of them have these in some form or other. How can you do them in Scratch?
* `Save` a value in a variable
* `Choose` whether to run some code, or which code to run
* `Repeat` some code multiple times
* `Reuse` code by putting it in a function
In order to be truly *useful* a programming language also has to be able to take input from the outside (ask the user a question, read a file) and return output from it's processing (save a file, render an image). What kinds of input and output does Pico-8 have? What kinds are missing, and why?
## Some useful Pico-8 functions
Full manual is at https://www.lexaloffle.com/pico-8.php?page=manual
Parameters in [brackets] are optional.
* `print(text, [x], [y], [colour])` print text, optionally at x,y
* `rect(x1, y1, x2, y2, [colour])` draw the outline of a rectangle
* `rectfill(x1, y1, x2, y2, [colour])` fill a rectangle
* `cls([colour])` clear the screen to (optionally) colour
* `circ(x, y, radius, [colour])` draw outline of circle centred at x,y
* `circfill(x, y, radius, [colour])` fill a circle
* `line(x1, y1, x2, y2, [colour])` draw a line from x1,y1 to x2,y2
* `spr(n, x, y, [width], [height], [flip_x], [flip_y])` draw sprite n
* `btn(i, [player])` is button pressed?
* `sfx(n, [channel], [offset], [length])` play sound effect n
## Pico-8 Challenges
* [ ] Draw a yellow circle centered on and inside of a red square
* [ ] Draw a red square centered on and inside of a yellow circle
* [ ] Draw 50 lines between random points
* [ ] Draw a green square 16 x 16 that moves around randomly
* [ ] Draw a red square 16 x 16 that moves based on player keypresses
* [ ] Using the past two challenges, detect if the squares collide. Move the random square away from the player on collision.
* [ ] Keep track of points using the last three challenges. Start the player with 3 lives and remove one for every collision. Game over at 0 lives. Display the lives in the upper left-hand corner of the screen.
* [ ] Make a sound in the sound effects editor and play it for every collision in the above challenge.
* [ ] How can we make the squares game harder? Make the random square bigger? Move it faster? Move it slower than the player, but always towards the player instead of randomly? Try some of these and test out your own ideas.
* [ ] Draw random yellow circles with radius of 2 from time to time (one every 10 seconds). Keep a circle for 30 seconds, then remove it. If the player collides with a circle before it expires they gain a point. Show the points in the upper right-hand part of the screen.
* [ ] If the player picks up 10 coins (circles) they gain 1 life.
* [ ] In the sprite editor draw a sprite for the player square and use it instead of drawing a square. Draw a sprite for the pursuing square (maybe a wolf?) and use it instead of drawing a square.
* [ ] Draw some sprites for background: sand, trees, rocks, whatever you like. Use the map editor to build a background and then draw the map to the screen instead of clearing the screen each frame.
* [ ] Restrict movement of the spites: let boulders, walls, etc. block their path. How can the pursuer still run towards the player?
* [ ] In the music editor, create a short tune. Play it as background music when game is playing.
* [ ] Give the game a title screen before play begins.
* [ ] Give the game an end screen when the player dies, showing points scored.