Lab 9: Asteroid Mining

        A CS151 Original Game

In this week's lab, we will start working on a multiplayer game called Asteroid Mining. The goal of each player is to reach and mine asteroids before the other player.  Each spaceship can move in any direction to reach asteroids which appear at random positions on the screen.  Colliding with the edge of the window resets the score of the player.

The goals this week are:

I recommend that you start working on this project today.  The code for the game Snake that we wrote together in class should serve as your guide to writing the code for this project.  As always, let me know if you have any questions or if you need any help.

Tasks

L1. 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.

L2. 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.

L3. 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.

L4. 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.

Project 9

P1. 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 asteroid at position 0, 0. The method also sets the attribute score to zero.

P2. Main 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)