讲解:BTH000、Python、Python、turtle r
Assignment 2 – Object oriented turtle raceCourse: BTH000 Python Programming I1 IntroductionThe purpose of the assignment is for you to demonstrate that you grasp thebasics in object oriented modeling and that you master to put together aPython program with a few user defined classes and objects.2 Task –Turtle RaceThe task is to implement a game where a couple of people can register forthe race by inputting their name and their favourite color. Thereafter eachplayer get a turtle with the chosen color, and the turtles compete against eachother in a race from a start line to a goal line. Use the turtle graphics packageto make the turtles and the race track visible on the screen. Use the objectoriented concept to design your program. You shall implement at least twodifferent classes in this assignment. One for the player and one for the racegame itself.This is what a run of the race might look like:Figure 1: Game setup1Figure 2: Game runningFigure 3: Present result2.1 class PlayerYour 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 positionYou are free to add more attributes if you need to. You could for examplehave a variable with a number keeping track of if the moving method shouldactually move the turtle forward. We do not want it to keep moving out ofthe window after it reached the goal line. If you choose to implement such anattribute it will probably be convenient to implement a getter method correspondingto the attribute. The class should also have a class variable to keeptrack of how many players that have registered to the game so far. This classvariable can be used to calculate the start position for the current turtle whencreating a new Player object.2The 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 position2.2 class GameYour Game class should at least have the following private attributes: a list with the players a lBTH000留学生作业代做、Python编程语言作业调试、Python实验作业代写、代做turtle race作业 代做ist representing the game result (can also be a list with players orderedafter 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 goalline) a method that registers the players (take name and color as interactiveinput, 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 result2.3 Race trackConstants defining the race track bounds may be defined globally and knownby all classes and methods/functions.2.4 Script to run the raceWrite a script that creates a Game object and calls its registering method, racemethod and result presenting method.32.5 Turtle graphicsThe following methods within the turtle package can be useful. There is a fulldescription of all turtle methods on python.org:https://docs.python.org/3.3/library/turtle.html?highlight=turtle.forward(distance)Move the turtle forward by the specified distance, in the direction the turtleis 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 Nonexcor()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 foundhere: http://www.science.smith.edu/dftwiki/index.php/Color Charts for TKinter4write(arg)Write text - the string representation of arg- at the current turtle positionaccording 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’5转自:http://www.7daixie.com/2019042612619853.html