Lab 9: Arcade Game

An Epic CS151 Final Project

In this week's lab, we will start working on a final game project of your choosing. The goal of this assignment is to demonstrate independent problem solving and synthesis of old projects and ideas into new ones.  Therefore, your best resource is the collection of games and projects that we have built throughout the semester.  I recommend that you start working on this project today.  As always, let me know if you have any questions or if you need any help.

Final Project Expectations:

  1. You are expected to implement a game of your choice (from the list below) based on your own observations of game features and behavior.
  2. Your report should include a list of the game features that you have included in your implementation.
  3. This project is due on May 12th, and each student will present a 2 minute video recording of their work in front of the class.  Please upload your videos by the morning of May 12th.
  4. Collaboration is encouraged, yet copying/using any amount of code from other students or any external sources is grounds for a plagiarism case.

Tasks

L1. Pick a game as your final project

Check the list of games below, and select a game to implement as your final project.  Find a video of the game to understand it better.

Game

Possible points

Resources

Asteroids*

26

Wikipedia page

Tron (Light Cycles)

26

Wikipedia page

Lunar Lander

27

Wikipedia page

Pole Position

27

Wikipedia page

Arkanoid*

28

Wikipedia page

Xevious

28

Wikipedia page

Grand Theft Auto

29

Wikipedia page

Tetris*

30

Wikipedia page

Super Mario Bros.

30

Wikipedia page

*Popular choice

Every game is expected to be implemented using classes, and inheritance should be used when needed.  Any time you find yourself repeating code in two classes you should consider using inheritance.  You can use any concept that we have covered in class, but you can NOT use lambda functions, list comprehension, or any concept that we did not cover.

L2. Work on a top-down design for your game

top-down design is the decomposition of a system into smaller parts in order to comprehend its compositional sub-systems.  In this step, you are expected to make a list of the classes and objects needed to implement your game. Then, think about how the objects will interact with each other and define the behaviour of each object.

I recommend that you talk to other students who are implementing the same game to brainstorm together.  Add your name and email address under the game that you plan to develop here: Link 

Feel free to continue to talk to each other after this lab, a mailing list or a chat group is not a bad idea.  Keep in mind that you are encouraged to talk to others, but sharing code is not allowed.

You are expected to implement the main features of the game. If some features of the game are not possible, please confirm that with me through email.

 By the end of this lab, you are expected to draft a list of objects and a list of behaviours for each object.  For example, the Asteroids game, has the following objects:

  1. Asteroid
  2. Spaceship
  3. UFO

The list of behaviour should include (but not limited to) the following:

  1. A spaceship can move and drift in different directions.
  2. Random sized asteroids appear on the screen.
  3. UFOs appear on the screen.
  4. The spaceship can shoot asteroids to gain points.
  5. Asteroids are broken into smaller pieces when shot.
  6. The screen is infinite and the spaceship can portal to the opposite side of the screen if it collides with the edge of the window.
  7. Asteroids move and can collide with the spaceship.
  8. The spaceship has limited fuel.

Extensions

You can make additions to the game other than the original implementation to gain extra extension points on top of the possible points listed in the table above.  As always, extensions that focus on functionality are worth more than extensions that focus on aesthetics.  Make sure you write about your extensions in detail in your project report.  Be creative!

Project 9

If you decide to implement a version of the game Asteroids, below you can find some a walkthrough of the main code components.  You can use the steps below as a guide for organizing your code for other games as well.

P1. The setup

Create a Lab09 folder on your computer where you store CS151 projects and labs, hopefully somewhere that is backed up (e.g. in Dropbox, Google Drive, Microsoft One Drive, etc.)

Create the file asteroid_mining.py in your Lab09 folder, and open it in VS Code.

Open up a Terminal (e.g. Control+` in VS Code) and change the directory (cd) to the Lab09 folder.

Start by writing the typical docstring at the top of the file and import the turtle, time, and random modules into your code.

P2. Make a Spaceship Class

First, let’s make a Spaceship class:

class Spaceship:

    # implements the spaceship in the game

Next, let’s write the __init__ method that initializes the Spaceship object and takes x_pos, y_pos, and color as arguments.  The method is expected to initialize the following attributes:

  1. A Python turtle object called “ship” that has a speed of 0 (fastest), classic shape, color, and an initial position on the window.
  2. A speed attribute that stores the moving speed of the Spaceship as an int with the value 5.  Note that this is different from the previous ship.speed(0) above.

Next, let’s add two methods to control the spaceship with keyboard buttons:

    def left(self):

        #your code goes here

       

    def right(self):

        #your code goes here

The two methods rotate the turtle “ship” object to the right or left by 20 degrees.

Finally, let’s build a “move” method that can be called repeatedly to move the spaceship across the window.  The method is a single line of code that moves the turtle forward by the speed attribute of the spaceship.

P3. Make an Asteroid Class

First, let’s make a Asteroid class:

class Asteroid :

    # implements the Asteroid in the game

Next, let’s write the __init__ method that initializes the Asteroid object without taking any arguments (other than self).  The method is contains the following attributes:

  1. A Python turtle object called “rock” that has a speed of 0 (fastest), circle shape, grey color, and a random initial position on the window.

When initializing the “rock” object, remember that a circle is 20 x 20 px by default, but you can change that size using the following code:

        self.rock.shapesize(0.4, 0.4)

Finally, let’s add a method called “relocate” to move the Asteroid to a random location on the screen.  You can reuse your random position code from __init__ in this method.  As a challenge, let’s see if you can write it as a single line of code.

P4. Make a main function

In your main function, add the following:

  1. A window object with a title that reflects the name of the game, black background color, width of 600 px, and height of 400 px.  Do not forget to set tracer to 0 or False.
  2. A spaceship object with color green and initial position at 0 and 0.
  3. An asteroid object.
  4. Make the window listen for key presses and bind keyboard keys to the left and write methods in your spaceship object.
  5. An infinite while loop that updates the window, moves the spaceship.  To regulate the speed of the game use time.sleep(0.1) to slow things down inside the loop.

P5. Spaceship Class Additions

You are expected to add the following to the Spaceship class for your project:

  1. In addition to the left and right movements, add two methods that move the spaceship faster or slower by updating the speed attribute in Spaceship.  The two methods are called “faster” and “slower” and they should be connected to the up/down or w/s buttons on your keyboard.
  2. Add a score attribute that starts with 0 and incremented throughout the game everytime the spaceship collides with an asteroid.
  3. Add a method to check for collisions:     def collision(self, asteroid):  The method returns true of the spaceship and the asteroid are less than 20 px away from each other.  Remember that you can use turt.distance(other_turtle) to calculate the distance between two turtle objects.  The method returns False if the objects have a distance that is more than 20 px.  Increment score when collision occurs, and use any scoring scheme that you see fit.
  4. Add the method outofscreen that returns true if the spaceship is outside the bounds of the window (returns false otherwise).
  5. Add a method to recenter the spaceship at position 0, 0. The method also sets the attribute score to zero.

P6. Main Function Additions

You are expected to add the following to the main function:

  1. A turtle object to write the scores on the window.
  2. A second spaceship with the color Yellow.
  3. Bind the new “faster” and “slower” methods and spaceship to the keyboard.
  4. Move both spaceships in the infinite while loop, and relocate asteroid in the case of a collision with one of the spaceships.
  5. Check that none of the spaceships is outside the window, and if a spaceship moves outside the bounds of the window, the recenter method is called.
  6. Write scores when the scores change.

Extensions

Here are a few ideas for project extensions:

  1. Enhance the visual elements of the game by using .gifs or turtle drawings.  You can make the spaceship look more realistic or add stars in the distance.  Also, you can enhance the shape of the asteroid by making it irregular or rotating around its center. (check this page for inspiration: https://www.jpl.nasa.gov/news/news.php?feature=7749 ) (one extension point possible)
  2. Randomise the size of the asteroid and make the score gained by mining the asteroid proportional to its size (i.e. bigger asteroid results in higher score). (two extension points possible)
  3. Give each spaceship a limited supply of fuel.  This means that a spaceship is able to go faster/slower a limited number of times.  Show remaining fuel as a percentage next to the score of each spaceship. (three extension points possible)
  4. Add a health attribute and allow spaceships to shoot at each other or asteroids.  A huge asteroid should be destroyed before a spaceship is able to mine it (inspired by Armageddon 1998). (four extension points possible)
  5. Be creative! (four extension points possible)