Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The goals of this assignment are to: Write a small class Write a program that uses that class and the Dice class from lab Experiment

The goals of this assignment are to:

Write a small class

Write a program that uses that class and the Dice class from lab

Experiment with using random numbers in a program

You will do this by making a horse race simulator, with two horses moving with random-sized steps to the finish line.

Getting Started

Make a PyCharm project called A2. Drag into that project these two files: graphics.py Links to an external site.and Dice.py Links to an external site.. Each of those files has been slightly modified from past versions, so make sure to download the linked files and use those instead of old ones. Add a Race.py file to the project. You can also use these two .gif files (Knight.gif Links to an external site., Wizard.gif Links to an external site.) although I would urge you to find your own images.

Assignment Requirements

In the Race.py, start by importing graphics and Dice. You can decide exactly how to import from these files, but recall that how you import changes how you use them and you will need to be consistent.

Then, define a Horse class. A Horse object will have data and actions. The data a Horse should have is:

An x position. The x position determines how far left and right on the screen the horse is. This variable will change as the horse races.

A y position. The y position determines how far up and down on the screen the horse is. The y position will not change during the race, but it determines which row or lane the horse is running in.

An image as defined by the graphics.py module. The main function will load an image and send it to the horse constructor, but the object should store what image it is using. See the slides from lecture on using the Image class. You should probably write a small test program based on the slides that just tries to get an image on the screen so you have some confidence in how that works.

A dice object. The horse will move by rolling this dice object (as done in lab2), but we want to store the dice for repeated use.

A graphics window to draw on. The window will be built in the main function and each Horse object will store the window so the image can be drawn.

The __init__ method for Horse is what should set up all those different pieces of data in a Horse object. The __init__ method should have parameters that provide the initial data to be stored. The parameters for the Horse __init__ (along with the initial self), should be (in this order):

A speed number. This number determines how far the horse can move each time. But we want the movement to be random, so use speed to build a Dice object with speed number of sides and store the Dice object, as indicated in the data section above.

A y_position. This determines how far up and down the horse is. The horse y position should be initialized with this parameter. The horse x position should be initialized to some small value that represents the starting line for the horse, but that starting x value does not need to be sent as a parameter - just use a number in the code.

An image. This image parameter should be stored in the horse object as indicated in the data section above.

A graphics window. This window parameter should be stored in the horse object as indicated in the data section above.

The __init__ method should initialize the needed stored values from the data section above using these passed in parameters.

Along with a __init__ method, the Horse class must have the following methods:

A move method. The move method should only have a self parameter. Move should roll the stored Dice object, and add the roll value to the stored x position.

A draw method. The draw method should only have a self parameter. Draw should use the stored image to call the draw_at_pos method. An example of calling this method is an_image.draw_at_pos(a_window, an_x_value, a_y_value) and this will draw the image on a_window located at the x and y values. You should use the stored window, x, and y from the object in your call to draw_at_pos.

A crossed_finish_line method. This method should have a self parameter, but also a parameter that indicates the x value of the finish line. This method should return true if the stored horse x position is greater or equal to the finish line parameter and false otherwise. Essentially, this is reporting if the horse has crossed the finish. (The will be true when the middle of the image crosses the finish, but that works fine.)

Then, below the Horse class make a main method and add the special lines to run the main method when the file is run.

The main method will simulate running two horses (each a Horse object) in a race. The steps the main method should follow are:

Set up the window. I made the window 700 x 350 to give some length to the race.

Load an image for the first horse following the example from Lec 5 slides.

Load an image for the second horse.

Make two Horse objects, providing the speed, the y position, the image, and the window for each Horse.

Get ready for the race

Draw the two horses by calling the Horse draw method for each.

Draw a finish line. Look at making a Line from graphics.py and draw it.

Wait for a mouse click by using the graphics.py getMouse.

Race once the mouse has been clicked in the window.

Loop until the race is over. The race is over when any of the horses has crossed the finish line.

Move each horse.

Draw each horse and the finish line. You will want to clear the window at the start of each loop and update the window at the end of the loop.

Report on the race

Once the race is over, print "Tie" if both horses crossed the finish line, "Horse 1 is the winner" if only horse 1 has crossed the finish line, and "Horse 2 is the winner" if only horse 2 crossed the finish line.

Wait for a mouse click before ending the program.

Style

Make sure your variable names are meaningful. Add a few comments describing what is happening in the code at key steps. Be careful with indenting and white space.

Submitting

Submit your Race.py and any .gif images you use. Drag each file into the submission window in Gradescope (not in a folder). You do not need to submit the Dice.py and graphics.py used in this assignment.

This tool needs to be loaded in a new browser window

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Logic In Databases International Workshop Lid 96 San Miniato Italy July 1 2 1996 Proceedings Lncs 1154

Authors: Dino Pedreschi ,Carlo Zaniolo

1st Edition

3540618147, 978-3540618140

More Books

Students also viewed these Databases questions

Question

Discuss the history of human resource management (HRM).

Answered: 1 week ago