CS
010 - Introduction to Computer Science I
Assignment 7:
DUE: May 27th before 2:00pm
Collaboration Policy
Limited collaboration is OK. You may do
the following while writing a programming assignment: discuss general
solution ideas with your study group members or have members observe a
run of your program, and offer their ideas on its behavior. You should
*never* look at someone else's code for the assignment to figure out how
to do your own program. It is very hard to write your own unique
solution once you have seen someone else's. If you need someone to look
at your code to give advice, you should see your TA or instructor. Of
course, copying code from ANY source (any book, current or past student,
past solutions, or the web) is STRICTLY FORBIDDEN.
Code that is turned in, must be contained in a .cpp file named
main.cpp. Files of any other format will not be
graded (e.g. main.doc, main.txt, etc…).
You must turn your work in from a lab computer on campus.
Turn in online to as7 folder. If you turn
your assignment in to the wrong folder, your assignment may not be
graded. If it is graded, you will lose 20 pts
(out of 100).
Programs that do not compile will receive a 0 out of 100!
Programs that do not have the readme file turned in with
the main.cpp file will not
be graded.
Remember to include the following header
information at the top of your program:
// Course: CS 10
//
// Lecture Section: ... 001 or 002
// Lab Section: ... 021, 022, etc)
//
// Assignment #: ... assignment 2, 3, etc.
//
// Last Name: Enter your LAST (family) name here (eg, Doe)
// First Name: Enter your FIRST (given) name here (eg, John)
//
// ID Number: Enter your ID number here (eg, 860-00-0000)
// lab login id: Enter your cs10 login here (eg, jdoe)
//
// Email address: Enter your UCR email address here (eg,
jdoe@cs.ucr.edu)
//
=======================================================================
Problem Definition:
For this assignment you will impliment the final moon lander game
!
To do this, you will need to add code to, and modify
your work from programing assignment 6. Specifically, you will add a
function that will draw your ship instead of a circle,
add
some code to ccc_win_main, create a collision detection function, add a
function that draws a simple landscape, and add a function that draws
an explosion based on the position of the ship. I know, it seems like
a lot, but many of the above additions are trivial (e.g. using a
function to draw your ship--you already did that in assignment 5, the
explosion drawing function works just like the draw ship function,
etc...).
The draw landscape function is passed
nothing, and returns void, the draw explosion function is passed a
point that represents the bottom center of your ship, and returns void.
Keep your landscape simple, have no more than THREE places to land as
in the example main below! This means there should be no more than 5
lines drawn in your landscape, and they should only be horizontal, and
vertical.
For the collision detection function, you will pass it a SINGLE
Point object that represents the bottom center of the ship. The
function will then check to see if that point is inside your landscape,
if it is, the function will return true,
otherwise it will return false.
The easiest way to do this is to break up your landscape into squares
(as mentioned only draw your landscape as a sequance of horizontal
lines, followed by vertical lines), the area under each horizontal line
would constitute a square. Then, all you need to do is check if your
point lies within any of the squares.
For example; if the upper left corner of the square was at point
(0,3500), and the lower right point of the square was at point
(2000,0), to check if the point representing your ship was inside the
box you would use the statement:
if(x >= 0 && x <= 2000 && y <= 3500)
{
//inside the square
}
else
{
//outside the square
}
This would need to be done for each square, so you will need to use a
switch statement or an if-else if structure. Because this is a
relatively simple way to check for collisions, it is OK if your ship
*lands* on a wall (vertical line). Also, since the point representing
your ship will be in the at the ship's center, it is OK if half your
ship passes through a vertical line/wall before the collision is
detected. You do not need to worry about
collision with the window's edge.
You will then need to add more code your ccc_win_main
function. The program should continue until the ship either lands, or
explodes. At the point in ccc_win_main where
you were previously drawing the circle, you
will now need to call your collision detection function to decide
weither you will draw the ship, or draw an explosion. If there is no
collision you can just draw the ship, otherwise you need to check the
ship's current velocity. If the ship is over some acceptable negative
velocity, it has landed safely, otherwise KABOOM! Using the code that
was given for assignment 6, I found that any velocity greater than or
equal to -17.5 was acceptable but difficult to land. For an easier game
(and testing my code), a velocity greater than or equal to -35 was
reasonable.
After the ship lands safely or explodes, you will use a break
statement (page 137) to exit the main animation loop, this should cause
the program to end.
Lastly, you must provide in the header area of your
code, a list of commands that will cause the ship to land safely on your
landscape. Write the commands in the format below:
/*
other header info as specified above, name, etc...
1) Main: <value>, Right: <value>, Left: <value>
2) Main: <value>, Right: <value>, Left: <value>
.
.
.
n) Main: <value>, Right: <value>, Left: <value>
*/
To receive full credit, the ship must be able to land in greater
than or equal to 5 commands, and less than or equal to 10 commands (5
<= n <= 10). Each set of three thruster values
input by the user is
considered one command.
Hints:
Program incrementaly! If you try to do
all the above in
one pass, and then compile your
program, you will have a mess on your hands! Break the above into
smaller steps, and get each smaller step to work first. Below is what I
would do...
1) Write the draw ship function first, and get the program to get the
ship to move around instead of a circle.
2) Write the collision detection function, and your landscape
function. Write down on paper what Points will constitue your landscape,
you will need the same points in your draw landscape, and collision
detection function.
3) Write the draw explosion function.
4) Modify the code in ccc_win_main to not just draw the ship at line
9, but detect if there is a collision. Don't worry about checking the
velocity, just have the program either draw the ship (no collision), or
draw the explosion (collision).
5) Once it is correctly detecting collision, add the code to check the
ships velocity. If the velocity is OK, break out of the loop, else draw
an explosion.
Solution to as5
main.cpp
Solution to as6
main.cpp
Example Program Download
main
Rubric: (100 pts total)
5 pt: Ship must land in >= 5 commands and <= 10 commands (set of main, left, and right = one command)
20 pt: Collision function detects collision with landscape and returns true
5 pt: Collision function returns false when there is no collision
5 pt: Landscape has no more than three landing pads
5 pt: Point that represents ship is located at the bottom center of ship (don't draw this point though)
5 pt: Correctly breaks out of animation loop when ship lands or explodes, otherwise continues
10 pt: On landing, draws ship when velocity is greater than or equal to -17.5 or -35
10 pt: Draws explosion when ship lands too fast
10 pt: Just draws ship when no collision is detected
15 pt: Program uses correct functions to draw ship, landscape, and explosion
10 pt: Style
- Good variable names
- Proper indentation and spacing
- Good comments
- No line wraps
- No magic numbers
Note that you can also receive penalties for this assignment. See the top of this page for details.
Fill out the
following form, and turn it in with your program:
readme.txt
For coding style requirements see the following link:
http://www.cs.ucr.edu/cs10/cs10_05win/requirements/coding_std.html