OO Programming and Data Structures | CS 241

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:

Ship
x : int
y : int
dx : int
dy : 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.

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.

To run the test script, log into the CS Department Linux system and run the testBed command, providing the class and test script to run as well as the Python program to use, for example:


testBed cs241/check05a check05a.py

Submission

Submit your program by logging into the CS Department Linux system, and running the submit command with your filename, for example:


submit check05a.py

You should then select the appropriate course (CS 241), instructor, and assignment from the list.