Montana Tech of The University of Montana
Computer Science Department

CSCI 135
Fundamentals of Computer Science I
Fall 2019



ASSIGNMENT 8

In this assignment, you will be building a video game called Staying Alive. In the process, you will be gain experience using classes and objects.


Staying Alive
The object of the game is to avoid a bunch of objects that are zooming around the screen and bouncing off the walls. The game is over if the player is hit by any of the objects. The player scores a point for every time step of the game in which they stay alive. The game is played with the mouse. The player's character moves in the direction of the mouse. The further the mouse is away from the character's current location when it is clicked, the faster the character moves towards the mouse location.

Classes: You should start by downloading the file alive.zip. NOTE: You can get the correct hitchhiker3.txt here. This file contains some example control files and the associated images. These files will need to be in the same directory where you run your program. The zip file also contains stub versions for the two classes and the client code you will be developing. You will need to add the class attributes, the bodies of methods, and the body of the client (calling) program. You should also comment the top of every method (every method needs a comment describing what it does, what parameters it takes, and what it returns). Here is a description of the three classes: Control file. Your client program StayingAlive.py should read in a game control file which is specified as a command line parameter. Here is an example game control file hitchhiker.txt with comments at the end describing the order and meaning of the values. You might recognize this from an example we did in class. Remember that you need to read in values from the file so that different configuration files will have different effects in your game.
stars.jpg
dont_panic_40.png 0.5 0.5 0.035 100
6
asteroid_small.png 0.1 0.1 0.018 -0.002 -0.003
asteroid_medium.png 0.2 0.2 0.030 0.002 -0.003
asteroid_large.png 0.3 0.3 0.065 -0.002 0.003
asteroid_small.png 0.4 0.4 0.018 -0.001 -0.004
asteroid_medium.png 0.6 0.6 0.030 0.002 -0.003
asteroid_large.png 0.7 0.7 0.065 -0.0035 0.0025

# Hitchhikers Guide to the Galaxy: Avoid a bunch of asteroids
#  <background image>
#  <player image> <player x-position> <player y-position> <player radius> <player speed factor>
#  <number enemies>
#  <enemy0 image> <enemy0 x-position> <enemy0 y-position> <enemy0 radius> <enemy0 x-velocity> <enemy0 y-velocity>
#  <enemy1 image> <enemy1 x-position> <enemy1 y-position> <enemy1 radius> <enemy1 x-velocity> <enemy1 y-velocity>
#  ...
Console output. Your StayingAlive.py program should output the intial game state to the console (command) window:
% python StayingAlive.py hitchhiker.txt 
PLAYER: (0.5, 0.5) r=0.035 speed=100
ENEMY 0: (0.1, 0.1) vel (-0.0020, -0.0030) r=0.018
ENEMY 1: (0.2, 0.2) vel (0.0020, -0.0030) r=0.03
ENEMY 2: (0.3, 0.3) vel (-0.0020, 0.0030) r=0.065
ENEMY 3: (0.4, 0.4) vel (-0.0010, -0.0040) r=0.018
ENEMY 4: (0.6, 0.6) vel (0.0020, -0.0030) r=0.03
ENEMY 5: (0.7, 0.7) vel (-0.0035, 0.0025) r=0.065
Canvas size and coordinates. In this assignment, we'll leave the canvas size at the default size of 512 x 512 pixels. We will also use the default coordinate system which has the lower-left corner being (0.0, 0.0) and the upper-right corner being (1.0, 1.0). Thus in this assignment you do NOT need to call methods such as StdDraw.setCanvasSize(), StdDraw.setXscale(), or StdDraw.setYscale().

Player dynamics. The Player object updates its x-position and y-position based on the current mouse location. The current mouse location can be obtained by first checking to see if it has been pressed (StdDraw.mousePressed(), and if so, calling StdDraw.mouseX() and StdDraw.mouseY(). These methods return a float representing the mouse location in the current StdDraw coordinate system. The mouse coordinates are sent to the Player object via the updatePos(mouseX, mouseY) method. To calculate the player's new x-position, you increment the current x-position by the differece between the mouse coordinate and the player's current position divided by the player's speed factor.

For example, if the player has a speed of 100 and is currently at an x-position of 0.5 and the mouse is at the x-coordinate 0.7: (0.7 - 0.5) / 100.0 = 0.002. After the update, the player's new x-position would be 0.5002. Note that a higher speed factor actually causes slower responsiveness to the mouse input.

Enemy dynamics. The Enemy objects update their position in the updatePos() method by adding their x-velocity and y-velocity to their x-position and y-position respectively. In updatePos(), you should also check if the position has gone out of the box (0.0, 0.0) - (1.0, 1.0). If so, you should invert the x-velocity and/or y-velocity as appropriate.

Collision detection. For simplicity, we assume the player and enemies are all circles. The game is over if the circle representing the player intersects with any enemy circle. This is similar to how I checked for the intersection of Circle objects in the lecture on classes.



How do I make the text larger? You need to set the font used by StdDraw. A single call at the start of your program such as StdDraw.setFontSize(18) will do the job.

How long should I pause between steps in my animation loop? We used StdDraw.show(10).


Grade Item Staying Alive Points Earned
Header Comment
2
Program Compiles and Runs
4
File Read In Correctly
4
Background Draws
2
Player Methods Implemented
4
Player Drawn Correctly
2
Enemy Methods Implemented
4
Correct Enemies Drawn
2
Mouse Move Player Correctly
2
Enemies Move Correctly
2
Score Display
2

Extra credit. There are plenty of possibilities to improve the game. For example:
Submission. Submit your programs StayingAlive.py, Player.py, Enemy.py to the Moodle dropbox for Lab 8. Be sure each submitted source file has the required header with your name and a description of the program.

If you choose to do extra credit, submit those files to the Extra Credit drop box at the top of the Moodle webpage.

Page last updated: October 31, 2019