-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathEX12.10.py
51 lines (38 loc) · 1.51 KB
/
EX12.10.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 12.10 (Tkinter: four cars) Write a program that simulates four cars racing, as shown in
# Figure 12.22. You should define a subclass of Canvas to display a car.
from tkinter import * # Import tkinter
class RaceCar(Canvas):
def __init__(self, master, width, height):
Canvas.__init__(self, master, width=width, height=height)
self["bg"] = "white"
self.sleepTime = 100
self.x = 10
self.y = 50
self.displayCar()
def displayCar(self):
self.delete("car")
self.create_oval(self.x + 10, self.y - 10, self.x + 20, self.y, fill="black", tags="car")
self.create_oval(self.x + 30, self.y - 10, self.x + 40, self.y, fill="black", tags="car")
self.create_rectangle(self.x, self.y - 20, self.x + 50, self.y - 10, fill="green", tags="car")
self.create_polygon(self.x + 10, self.y - 20, self.x + 20, self.y - 30,
self.x + 30, self.y - 30, self.x + 40, self.y - 20, fill="red", tags="car")
def run():
while True:
for car in cars:
if car.x < int(car["width"]):
car.displayCar()
car.x += 4
else:
car.x = 0
car.after(10) # Sleep for 100 milliseconds
car.update()
window = Tk() # Create a window
window.title("Racing Cars") # Set a title
width = 250
height = 48
cars = []
for i in range(4):
cars.append(RaceCar(window, width=width, height=height))
cars[i].pack()
run()
window.mainloop() # Create an event loop