February 16th, 2014, 07:28 PM
Join Date: Feb 2014
Time spent in forums: 16 m 31 sec
Reputation Power: 0
Does anybody know how to write the code for the following assignment?
1. [username_3_1.py]. This assignment combines pygame and classes
a. Create the following classes
i. class Circle(location, size, color)
ii. class Rect(location, length, width, color)
iii. class Picture(location, filename)
b. I should be able to create instances of your classes like this
i. my_circle = Circle((100,100), 23, RED)
ii. my_rect = Rect((100,100), 150,100, RED)
iii. my_pic = Picture((100,100),”picure.png”)
c. I should be able to have those instances draw themselves using the commands below:
d. Doing so should have the following effect, i.e., it all occurs within each class’s draw():
i. Pygame gets initialized
ii. The specified object should be created
iii. Pygame should show the object in a game loop
iv. When the user closes the window or presses the ‘q’ key, the draw loop should be
exited and pygame should be shut down.
e. For each class, I should be able to query any property of instances like this:
i. print my_circle.color [result might be (255,0,0)]
ii. print my_circle.radius [result might be 23]
iii. print my_circle.position [result might be (100,100)]
2. [username_3_2.py ]. Specify which key events get monitored.
a. Tweak the code in your previous code file so that each class takes an additional parameter,
a list (regular list, not a tuple) of allowable keys, like this:
i. My_circle =
Circle((100,100), 23, RED, [pygame.K_y,pygame.K_n])
b. The draw() method keeps looping until any one of these keys are pressed, but only those
keys specified when the class instance was created.
3. [username_3_3.py ]. Start with code from your first program username_3_1.py. Write a
new class just like the others but instead it’s going to display text on the pygame surface (see book
or pygame_test_v7_text.py for help). Should work like this:
a. my_text = Text((100,100), “What is your name?”, Green)
b. …and then I should be able to call my_text.draw()
c. In the game loop, the prompt (in this case “What is your name?”) should be printed to the
surface so the user can see it.
d. Then, like the other classes draw() method, wait for a response in the game loop, but:
i. The only key that should shut down pygame is the ENTER key, i.e., use
ii. Every other key the user presses should be appended to a string so that it builds up
over each keypress.
iii. When the user does finally press the ENTER key, shut down pygame as before, but
also print (the normal print function) the string.
iv. We’re basically making a graphical version of raw_input() except it draws the
prompt using pygame doesn’t return the response, just prints it.