Montana Tech of The University of Montana
Computer Science & Software Engineering

CSCI 135
Fundamentals of Computer Science I
Fall 2017



ASSIGNMENT 6

In this assignment, you will be building a Mars Rover video game. You will learn how to draw things and get keyboard input using static methods in StdDraw.java. You will also use StdAudio.java to play sound effects.




Mars Rover
Have you seen the movie "The Martian"? You are stranded on Mars and need to use the Mars rover to leave the safety of the biodome and explore. But the rover has a limited range, and you must get back to the biodome before you run out of fuel or else the rover self-implodes. Rover controls:
  • w - moves the rover forward
  • d - moves the rover to the right
  • a - moves the rover to the left
  • s - moves the rover backward
Your program MarsRover.java should read in a game control file from a text file as described in Monday's lecture. Here is an example game control file with comments (that start with #) at the end describing the order and meaning of the values:
800 600
mars_surface.jpg
rover.png rover_forward.png rover_backward.png rover_left.png rover_right.png roverSafe.png scorch.png
biodome.png
50
600.0 100.0
25
thrust.wav yay.wav explosion.wav
0.5
40 400 100
# An easy game setup, you start fairly close to the biodome and have plenty of fuel. The
#  biodome is relatively large (and has a large radius).
# Background image is of the Mars surface.
#
# Description of values:
#  <width in pixels> <height in pixels>
#  <background image>
#  <normal rover image> <moving forward> <moving backward> <moving left> <moving right> <rover safe> <rover crashed>
#  <biodome image>
#  <rover "radius">
#  <rover starting x> <rover starting y>
#  <starting fuel>
#  <movement sound> <safe sound> <crash sound>
#  <movement amount>
#  <biodome x center> <biodome right center> <biodome radius>
You should start by downloading the file rover.zip. This file contains some example control files and the associated images/sounds. These files will need to be in the same directory where you run your program.

Rover Movement. Your program will need to track the current horizontal and vertical position of the rover (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. The rover will continue on whatever its current path is unless the player presses one of the movement keys to change directions.

If the user moves forward or backward, the movement amount from the control file is added to, or subtracted from, velY. Similarly, left and right movement should change velX. Whenever movement is applied, the movement sound should be played and the rover picture updated to the appropriate version. Using any movement key 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 rover must be changed. This is achieved by simply adding velX to posX and adding velY to posY.

Note that it is possible for the rover to go off the top, bottom, or sides of the screen. The rover can still be hopefully driven back toward the biodome. But of course an off screen rover can still run out of fuel!

Game Over. Your rover implodes when it runs out of fuel. If your rover implodes, you should display the rover crashed picture and play the crash sound. If your rover successfully reaches the biodome, you should display the rover safe picture and play the safe sound. After reaching the biodome or imploding, the game is over and no further updates are performed.

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 download them from the links at the top of the page to the source folder of your project (the one with the *.java files). In order for the classes to appear in Eclipse, you'll need to right click on the project name in Eclipse and select Refresh.

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

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 10 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.setFont(new Font("SansSerif", Font.BOLD, 18));. You'll need to put import java.awt.Font; at the top of your program (outside the class declaration) as well. That 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(0) to your program, you should see your final drawing.

How do I check that my rover has reached the biodome?? For this program, we can assume that the biodome and rover are roughly circles. We can check for the intersecion of those circles by checking to see if the distance between the centers of the "circles" is less than the radius of each added together. Recall that the distance between two points is calculated from the equation:



The radius of both the rover and the biodome are read in from the file, as is the initial center position of both. The biodome never moves, but you should keep track of the position of your rover every time it moves (not just for calculating distance, but also so that it redraws in the correct position).
Grade Item Mars Rover Points Earned
Program Compiles and Runs
1
Header Comment
1
Programming Style
2
Reads from File
4
Display Graphics Correctly
6
Key Presses Move Rovery
6
Calculates Intersection with Biodome
6
Game Ends Correctly
4



Extra credit possibilities:

Submission. Submit your program MarsRover.java using Moodle. Be sure your submitted source file has the required header with your name, and a description of the program. If you did the extra-credit, package everything needed to run your program into a single zip file and submit to the extra-credit dropbox.

Page last updated: August 15, 2018