Lab 2: Animations

Eli is evicting you and your crew from Mars! You received a transmission from Heila that you can come stay with her on Venus. You have to navigate there and land as fast as you can!

Setup

Include this at the top of your program:


include reactors

include image

  

data Posn:

| posn(x :: Number, y :: Number)

end

A posn is a datatype that probably looks familiar: it is a way to represent coordinates on a grid by using x and y values.

To create a new posn with an x-value of 3 and a y-value of 4:

my-posn = posn(3,4)

To access the x- and y-values of my-posn:


>>>my-posn.x

3

>>>my-posn.y

4

Try this out in the interactions window! What kind of values can you use for x and y? What happens if you try to add a z coordinate?

Part 1

1.1: Building the view

The first thing you need in order to launch your ship is a night sky and a ship to launch.

Write a function outer-space that creates a square black background and place your rocketship and Venus somewhere on it. They should be on opposite ends of the screen.

You might notice that this function does not have any inputs (something that also came up on homework 2). When might it be useful to write a function without any inputs?

Pyret has a useful function image-url for loading images when you don’t want to build an entire graphic from scratch. image-url takes in the url of an picture from the web and outputs it as a Pyret Image. Once it’s loaded, you can manipulate it the way you would any other Image from the image library.

We used these images:


SPACESHIP-URL = "https://cdn.pixabay.com/photo/2018/01/25/19/16/ufo-3106867_960_720.png"

VENUS-URL = "http://icons.iconarchive.com/icons/zairaam/bumpy-planets/256/03-venus-icon.png"

  

spaceship-image = image-url(SPACESHIP-URL)

venus-image = image-url(VENUS-URL)

But you’re welcome to find (or build) your own!

Hint: You might find the function place-image more useful than overlay-image, as it allows you to fix one image and put another somewhere on it.

Note: You must use a posn to store the positions of your spaceship and of Venus.

1.2: Random launch

Eli is getting impatient with your slow departure and has decided to randomly launch your ship!

Use num-random(n) to change your function so that your spaceship is placed at a random spot on your background.


CHECKPOINT: Call over a TA once you reach this point.


Part 2

2.1: Adding movement

Now it’s time to actually launch your ship! To do so, you will be using a Pyret reactor, which we talked about in the presentation.

Here is a basic example for reference:


my-reactor =

reactor:

init: init-posn

to-draw: outer-space

on-tick: lift-off

end

Change your code from Problem 1 so that your image is drawn as part of a reactor. To do this, you must create two functions:

To use your reactor, run your program and type interact(my-reactor) in the interactions window.


CHECKPOINT: Call over a TA once you reach this point.


If you can’t steer your ship, how will you get to Venus?

So far, our reactor uses lift-off to move the rocket every few milliseconds. In a game, however, we also want to move the rocket as a player presses keys. Brainstorm which keys would you like to use and how the posn of a rocket change based on which key was pressed. Pick only two keys to start (you can add more later).

Write a function move-ship that takes in a posn and a string and returns a new posn. The string represents a keypress, the input posn represents the initial position of an object, and the output posn represents the object’s new position after the keypress.

Below is a list of some string to key mappings:

You can also use each key’s literal character to represent that key. For example, “a” is the a key, “b” is the b key, etc.

Add the move-ship function to your reactor like this:


my-reactor =

reactor:

init: init-posn

to-draw: outer-space

on-tick: lift-off

on-key: move-ship

end


CHECKPOINT: Call over a TA once you reach this point.


2.2: Additional features

Pick and choose which feature you want to try to implement, or come up with your own!

Collisions:

Write a function collision that takes in the position of the spaceship and returns a Boolean indicating whether the spaceship has landed on Venus.

Add it to your reactor like this:


my-reactor =

reactor:

init: init-posn

to-draw: outer-space

on-tick: lift-off

on-key: move-ship

stop-when: collision

end

Next, congratulate your captain for a successful landing by modifying to-draw so that if there is a collision, the image becomes a “Congratulations!” graphic.

Wrap-around:

What happens if the spaceship misses Venus and flies off the screen? (Try this out using your current reactor).

Modify your on-tick function so that if the spaceship flies off the screen, it wraps around on the other side.