BTH000

【BTH000】Assignment 2 – Object oriented turtle race
Course: BTH000 Python Programming I
1 Introduction
The purpose of the assignment is for you to demonstrate that you grasp the
basics in object oriented modeling and that you master to put together a
Python program with a few user defined classes and objects.
2 Task –Turtle Race
The task is to implement a game where a couple of people can register for
the race by inputting their name and their favourite color. Thereafter each
player get a turtle with the chosen color, and the turtles compete against each
other in a race from a start line to a goal line. Use the turtle graphics package
to make the turtles and the race track visible on the screen. Use the object
oriented concept to design your program. You shall implement at least two
different classes in this assignment. One for the player and one for the race
game itself.
This is what a run of the race might look like:
Figure 1: Game setup
1
Figure 2: Game running
Figure 3: Present result
2.1 class Player
Your Player class should at least have the following private attributes:
a name
a color
a start position
a Turtle object initiated to the chosen color and correct start position
You are free to add more attributes if you need to. You could for example
have a variable with a number keeping track of if the moving method should
actually move the turtle forward. We do not want it to keep moving out of
the window after it reached the goal line. If you choose to implement such an
attribute it will probably be convenient to implement a getter method corresponding
to the attribute. The class should also have a class variable to keep
track of how many players that have registered to the game so far. This class
variable can be used to calculate the start position for the current turtle when
creating a new Player object.
2
The constructor shall take two input parameters, the name and the color.
At least methods for the following shall be implemented within the class:
a method to move the turtle forward a random amount of pixels
a getter method that returns the name
a getter method that returns the color
a method writing the name in text beside the start position
2.2 class Game
Your Game class should at least have the following private attributes:
a list with the players
a list representing the game result (can also be a list with players ordered
after race outcome)
At least methods for the following shall be implemented within the class:
a method that draws the race track on the screen (a start line and a goal
line)
a method that registers the players (take name and color as interactive
input, create player object and add to the player list )
a method checking if the race is over (all turtles have stopped)
the race itself, i.e. moving the players repeatedly until the race is over
a method presenting a list of the race result
2.3 Race track
Constants defining the race track bounds may be defined globally and known
by all classes and methods/functions.
2.4 Script to run the race
Write a script that creates a Game object and calls its registering method, race
method and result presenting method.
3
2.5 Turtle graphics
The following methods within the turtle package can be useful. There is a full
description of all turtle methods on python.org:
https://docs.python.org/3.3/l...
forward(distance)
Move the turtle forward by the specified distance, in the direction the turtle
is headed.
Parameters:
distance – a number (integer or float)
left(angle)
Turn turtle left by angle degrees.
right(angle)
Turn turtle right by angle degrees.
setpos(x, y=None)
Move turtle to an absolute position.
Parameters:
x - a number or a pair/vector of numbers,
y - a number or None
xcor()
Return the turtle’s x coordinate.
pendown()
Pull the pen down – drawing when moving.
penup()
Pull the pen up – no drawing when moving.
color(name)
Among other things it sets the turtle color to the one specified by name.
Parameters:
name – a string which is a valid color name. Valid color names can be found
here: http://www.science.smith.edu/... Charts for TKinter
4
write(arg)
Write text - the string representation of arg- at the current turtle position
according to align (’left’, ’center’ or ’right’)
hideturtle()
Make the turtle invisible.
showturtle()
Make the turtle visible.
shape(name=None)
Set turtle shape to shape with given name.
Parameters:
name – a string which is a valid shape name. Valid shape names are: ’turtle’,
’arrow’, ’circle’, ’square’, ’triangle’, ’classic’

    推荐阅读