Copy Template and Rename

Copy the file gameTemplate.rkt and then rename the file. The new name should start with p2_ or p4_ and include the name of your game. For example: p2_rabbitHunt.rkt.

Title, Color and Authors

At the beginning of the code, two variables define strings for the title and title color. Change "My Game" to the title of your game. Choose a color for the title and change "white" to the string for the new color.

Also add a comment to the Authors section to show the members of your team.

Place-holder Images

The game template contains place-holder images for the target, danger, and player, which you eventually will replace with new images from the graphics guru. Use the place-holder images to get your code working while the graphics guru acquires and edits the real images your game will use.

Section 1 - Making Target and Danger Move

We create game animation with a sequence of frames, just like the rocket program you worked with in unit 3. In each frame, the target and danger will move to a new position. Your code can make the target or danger move left or right by changing its x-coordinate. Vertical movement is created by changing the y-coordinate.

Each frame is 640 pixels wide, and 480 pixels tall. Look at page 16 (unit 1) of your workbook for an example of to locate images on a frame.

Two functions, update-target and update-danger cause the target and danger to move. Each of these functions consume the current x-coordinate and y-coordinate, and call the make-posn function with the new coordinates.

Try making the target move left to right across the screen, and the danger move right to left. Then try some up and down movement. Finally, work as a team to decide how you want the target and danger actually to move in your game, and edit the code to create the movement you want.

Section 2 - Making Target and Danger Come Back After Leaving the Screen

The onscreen? function consumes an x-coordinate of an image and produces a boolean. It must produce true if any part of the image is on screen, and false if the image is off the screen. First write some examples of x-coordinates that are on-screen, and off-screen. then, write a math inequality for the values of x that are on-screen. Finally, write a compound boolean function that consumes x and produces true if the image is on-screen.

You probably will not need the safe-left? and safe-right? functions. However, if you are having difficulty getting the on-screen? function working, you may find it easer first to write the safe-left? and safe-right? functions.

Section 3 - Making the Player Move

Player movement is controlled with the keyboard. The update-player consumes the x and y coordinates of the play, and a string for a key that was pressed. You will need to write a cond function with a clause for each key that can move the player. For each clause, call the make-posn function with the updated coordinates of the player. You can decide what keys control player movement, and how the player moves.

Here is a simple example. If you want the player to move right one pixel when the left arrow key is pressed, the clause would be:
[(string=? key "left") (make-posn (- x 1) y)]

Section 4 - Detecting Collisions

The final step in creating the basic game is to determine when collisions occur between the player and other images.

Collisions Part 1 - Finding Distance

You can find the distance between any two points using the distance formula, which is derived from the Pythagorean Theorem. $$d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}$$

Follow these steps to get the distance function working in Section 4.

  1. Turn the last line of the program into a comment by placing two semicolons at the beginning of the line. This will allow you to test your distance function without running the game.
    ;; (play g)
  2. Write the body of the distance function by creating a legal expression for the distance formula which uses the parameters px, py, cx, cy
  3. Use the interactions window to test this function to ensure it works. Use examples provided in the code.

Collisions Part 2 - Making the Collide Function Work

A collision occurs when the player gets close to the target or danger. You will need to decide how close the two images need to be in order for a collision to occur. Follow these steps:

  1. Decide how close two images need to be to cause a collision.
  2. Complete the collide? function. This function will need to call the distance function you completed in part 1.
  3. Test your collide? function using the interactions window.
  4. Finally remove the two semicolons which you added to the last line in Part 1. Your game should now run!.
Computer Programming Unit 5 - Video Game Coding Instructions