OBJECT ORIENTED DESIGN: MARS LANDER EXAMPLE Fundamentals of Computer Science I
Outline • Approach – Find: • Objects • Instance variables • Methods – Refine: • Instance variables • Methods – Implementation (Code) 2
What are the Nouns? 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) You should start by downloading the file lander.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. 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 bottom landing pads reaches (or goes below) the y-coordinate of 0. If your landing pads have reached the bottom and either velX or velY is greater than the maximum survivable velocity. 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. The location of the landing pad is read in from the file with a center and a "radius". The height of the pad should be 10 pixels. 3
Objects • Spaceship • Planet • Obstacle • Landing Pad • Game
Instance Variables? Your program MarsLander.java should read in a game control file from a text file as described in lecture 11. Here is an example game control file with comments at the end describing the order and meaning of the values: 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.wav yay.wav explosion.wav -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>
Instance Variables • Spaceship • rate of fuel expenditure • velocity x • velocity y • fuel • dimension - horizontal • dimension - vertical • position x • position y • maximum survivable velocity • crashed? • sounds • images • thrust
Instance Variables • Planet • gravity • Obstacle • dimensions • LandingPad • image • windFactor/weather (?)
Instance Variables • Obstacle • location • dimensions • image? • Landing Pad (extends Obstacle) • Game • gameOver
Methods • Spaceship • controls (move) • draw • collisionDetection • useFuel • constructor • getters • setters • toString • equals
Methods • Planet • constructor • getters • setters • toString • equals • draw • LandingPad • draw • the 5 general methods
Methods • Obstacle • constructor • getters • setters • toString • equals • draw • Game • readFile
Starting Implementation: Spaceship Constructor public void Spaceship(String file) { // read in all instance variable information from file }
Summary • Approach • Find: • Objects • Instance variables • Methods • Refine: • Instance variables • Methods • Implementation (Code)
Recommend
More recommend