In: Computer Science
05 Prepare : Checkpoint A
Objective
This assignment will give you practice writing event
handling methods to advance a ship in two dimensional
space.
Overview
After completing (or while completing) the
preparation material for this week, complete the following
exercise.
For this exercise, you will create a class that models
a ship flying around in a 2-D plain (containing an x and y
coordinate). A game class is provided to you that will help you
call methods to advance and "draw" it.
Instructions
Write a class for a ship that has integer x and y
coordinates, and also stores the components of its velocity, dx and
dy. (Please note that will will have similar elements in our weekly
assignment, where we will break them into separate classes, but for
this assignment, you can store all four as member variables of your
ship class.
The ship should have two methods, one for advance, and
one for draw. The advance method should move the ship forward one
unit in time. This is accomplished by adding dx onto x, and dy onto
y. This draw method should output, "Drawing the ship at (x, y)"
where x and y represent the current location of the ship. The
__init__ method for the ship should initialize x, y, dx, and dy to
0.
For this assignment, the provided main function will
set the velocity of the ship when it is created, and the velocity
will remain constant for the rest of that run.
The following UML class diagram defines your ship
class:
Shipx : inty : intdx : intdy :
int__init__()advance()draw()
To go with your ship class, you will be provided with
a Game class and a main function. The Game class has a ship, and
two important functions, on_draw() and update(). In your the
on_draw function, you simply need to call the ship's draw function.
In the update function, you simply need to call the ship's advance
function. Nothing else in the Game class is required of
you.
In addition, nothing is required from you in the main
function. It can be left as provided. You can obtain the provided
code from: /home/cs241/check05a.py with the following
command:
cp /home/cs241/check05a.py .
Finally, for simplicity, for this assignment you may
include all of your classes in the same file if you would
like.
INSTRUCTIONS RECAP
Thus, in short, for this checkpoint, you need to do
the following.
Create a Ship class
Create an __init__ method that sets x, y, dx, and dy
to 0.
Create a draw method that prints the text, "Drawing
ship at (x, y)".
Create an advance method that adds dx to x, and dy to
y.
In the Game class on_draw method, call your ship's
draw method.
In the Game class update method, call your ship's
advance method.
Sample Output
The following is an example of output for this program:
Enter a random seed: cat Starting the ship with velocity (-1, 3)
Drawing ship at (-1, 3) Drawing ship at (-2, 6) Drawing ship at
(-3, 9) Drawing ship at (-4, 12) Drawing ship at (-5, 15) Drawing
ship at (-6, 18) Drawing ship at (-7, 21) Drawing ship at (-8, 24)
Drawing ship at (-9, 27) Drawing ship at (-10, 30) Drawing ship at
(-11, 33) Drawing ship at (-12, 36) Drawing ship at (-13, 39)
Drawing ship at (-14, 42) Drawing ship at (-15, 45) Drawing ship at
(-16, 48) Drawing ship at (-17, 51) Drawing ship at (-18, 54)
Drawing ship at (-19, 57) Drawing ship at (-20, 60)
Automatic Grading Script (TestBed)
This assignment is pass/fail. In order to receive
credit, it must pass the auto-grading test script (TestBed). You
can run this script as many times as you like while you are working
on the assignment. The same test script will be run by the
instructor for credit, so you should not be surprised at the
result.
In addition, because the point of this exercise is to
help you practice the use of classes. You must use classes
to receive credit for this assignment.
# Python version : 3.6
class Ship:
def __init__(self):
''' Constructor that initializes x,
y, dx, dy to 0 '''
self.x = 0
self.y = 0
self.dx = 0
self.dy = 0
def draw(self):
'''
Function that prints the text,
"Drawing ship at (x, y)".
Call this function within Game's
on_draw function, by using the Ship class object and dot
operator
i.e ship.draw() where ship is the
object of class Ship in class Game
'''
print('Drawing ship at (',x,',
',y,')')
def advance(self):
'''
Function that adds dx to x, and dy
to y.
Call this function within Game's
update function, by using the Ship class object and dot
operator
i.e ship.update() where ship is the
object of class Ship in class Game
'''
x += dx
y += dy
#end of Ship class
Code Screenshot: