Montana Tech of The University of Montana
Computer Science Department

CSCI 135
Fundamentals of Computer Science I
Fall 2021



LAB 4

In this assignment, you will be building a Mars Lander video game. You will practice using iteration (loops) by doing a game play and animation loop. You will learn how to draw things and get keyboard input using functions in StdDraw.py. You will also use StdAudio.py to play sound effects.




Mars Lander
You are an astronaut on the first ever manned mission to Mars. Your auto-pilot computer (running Windows 2037 Hyper Mega Ultimate) has just given you the blue screen of death. Rebooting is going to take awhile, so it's up to you to pilot your ship to a soft landing in the only safe spot around. You have a limited amount of fuel and once that runs out, you are at the mercy of gravity! Spaceship controls:
  • w - bottom thrusters (slows the ship's descent)
  • d - left thrusters (makes the ship go more to the right)
  • a - right thrusters (makes the ship go more to the left)
Your program, MarsLander.py, should read in a game control file from a text file as described in Graphics Lecture. Below is an example game control file with comments at the end describing the order and meaning of the values. Be careful! There are different configuration files for the game, so you should not use these values directly - you must read them from the file. They will be different in different files.
1000 500
mars_sky.jpg
ship.png ship_bottom.png ship_left.png ship_right.png ship_landed.png ship_crashed.png
20 50
500.0 400.0
100
thrust yay explosion
-0.1
2.0
0.5
500 50
# An easy game setup, you start directly above a large pad and have plenty of fuel.
# Background image is from one of the NASA Mars rovers.
#
# Description of values:
#  <width in pixels> <height in pixels>
#  <background image>
#  <normal ship image> <bottom thrusters> <left thrusters> <right thrusters> <ship landed> <ship crashed>
#  <horizontal distance, ship center to edge of thrusters> <vertical distance, ship center to bottom edge of thrusters>
#  <ship starting x> <ship starting y>
#  <starting fuel>
#  <thruster sound> <good landing sound> <bad landing sound>
#  <gravity term>
#  <maximum survivable velocity> 
#  <thrust amount>
#  <landing pad x center> <distance from center of landing pad to edge>
You should start by downloading the file Lander.zip. This file contains some example control files, the associated images/sounds, and all the files you will need for the graphics and sound libraries to work. These files will need to be in the same directory where you run your program.

Gravity and thrusting. Your program will need to track the current horizontal and vertical position of the ship (let's call the positions posX and posY). You also need to store a velocity composed of an horizontal component velX and a vertical component velY. Both velocities start at 0. At every time step of the game, the gravity term from the control file is added to velY.

If the user uses activates the bottom thrusters, the thrust amount from the control file is added to velY. Similarly, the left and right thrusters should change velX. Whenever thrust is applied, the thruster sound should be played and the ship picture updated to the appropriate version. Using any thruster causes the amount of fuel to be reduced by one. The current fuel remaining should be display in the upper-left corner. After updating the velocities, the current position of the ship must be changed. This is achieved by simply adding velX to posX and adding velY to posY.

Note that it is possible for the ship to go off the top or sides of the screen. The ship can still be hopefully piloted back toward the landing pad. But of course an off screen ship can still hit the ground!

Landing and crashing. Your ship has landed or crashed when the ship bottom reaches (or goes below) the y-coordinate of 0. If your ship has reached the bottom and either velX or velY is greater than the maximum survivable velocity, your ship has crashed. Your ship has also crashed if its bottom thrusters are not completely inside the x-coordinates of the landing pad. If you land successfully, you should display the ship landed picture and play the good landing sound. If you crash and burn, you should display the ship crashed picture and play the bad landing sound. After landing or crashing, the game is over and no further updates are performed. You may want to do a final StdDraw.show(1000) so that the screen doesn't immediately disappear, though.

Window setup. The width and height of the game window are given as the start of the control file. We will use these values both for the size of the drawing window (in pixels) and for the coordinate system used by the drawing methods. To set the window size, call StdDraw.setCanvasSize(width, height). To set the x-coordinate range, call StdDraw.setXscale(0, width). To set the y-coordinate range, call StdDraw.setYscale(0, height).

My program complains it can't find StdDraw or StdAudio. What is wrong? Make sure you have extracted them from the downloaded zip file and that they are in the same folder as your code (the one with the *.py files). You will need to run your program from the command line, and you will need all of the .py libraries, images, and sound files in the same folder. Don't forget to use the python MarsLander.py <config_file> command to run your program.

How exactly do I draw images or get keyboard input? Check out BoxCar.py (you'll also need explosion.wav to make this program work).

How tall should the landing pad be? We used a height of 10 pixels.

How do I play a sound from inside the program? Use the StdAudio.playFile(filename) function. Note that when you pass it a filename, you don't give it the .wav extension. For example, to play the yay.wav sound, you would use the command StdAudio.playFile("yay").

My game goes way to fast. How do I slow it down? Be sure to call StdDraw.show(ms) at the end of your animation loop. We used a delay value of 100 milliseconds.

How do I make the drawn text bold like yours? You need to set the font used by StdDraw. A single call at the start of your program such as
StdDraw.setFontFamily("SansSerif")
StdDraw.setFontSize(18)

will do the job.

After I crash, the final things I drew are not displayed. What is going on? When StdDraw is in animation mode (as soon as your program calls StdDraw.show()), the graphics window is only updated on the next call to StdDRaw.show(). If you add a final call StdDraw.show(1000) to your program, you should see your final drawing, but it will only last 1 second. If you want it to display for longer, make this number larger.

I check for a crash if the bottom of my ship hits y = 0.0, but my ship just cruises on past without crashing. What is wrong? Be sure you are not testing a float variable against a literal value with ==. It is very unlikely that your position will be exactly equal to the literal value. You probably should use an inequality instead.
Grade Item Mars Lander Points Earned
Header Comment
2
Program Compiles and Runs
4
File Read In Correctly
4
Background Draws
2
Fuel Display
2
Correct Spacehip Draws
(based on action)
4
Keys Move Spaceship Correctly
4
Correct Audio
2
Correct Gravity / Descent
2
Correct Final Images
2
Correct Final Sounds
2

Extra credit possibilities:
  • Create a new game control file along with a new set of images and sounds. Describe your creation in the comments at the end of the control file. Submit all the files needed to run your new level.
  • Create a new program that loads a more advanced control file like this one. The end of the control file contains a list of rectangular obstacles that should be drawn. Your ship should crash if it hits any of the obstacles. You can pretend the ship is a rectangle for purposes of collision detection.
  • Dream up other ways to make the game even more exciting.

Submission. Submit your program MarsLander.py using the Moodle dropbox for Lab 07. Be sure each submitted source file has the required header with your name and a description of the program. Submit the solution to this assignment to our regular lab dropbox. If you did the extra-credit, package everything needed to run your program into a single zip file and submit to the Lab 07 Extra dropbox at the top of the Moodle page.

Page last updated: October 27, 2021