-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddle.py
64 lines (57 loc) · 2.25 KB
/
paddle.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
52
53
54
55
56
57
58
59
60
61
62
63
64
from colour import Colour
class Paddle:
def __init__(self, root, canvas, x, y):
self.root = root
self.canvas = canvas
self.speed = 0
self.width = 80
self.height = 20
self.coords = [x, y, x + self.width, y + self.height]
self.paused = False
self.paddle = self.canvas.canvas.create_rectangle(
self.coords[0], self.coords[1], self.coords[2], self.coords[3],
fill=Colour.paddle)
self.root.bind("<Control-l>", self.long)
def long(self, event):
"""Toggles the paddle between
normal and long widths"""
self.width = 160 if self.width == 80 else 80
if self.width == 160:
self.canvas.canvas.itemconfigure(
self.paddle, fill=Colour.paddle_long)
self.coords = [
self.coords[0] - 40, self.coords[1],
self.coords[2] + 40, self.coords[3]]
if self.coords[0] < 0:
self.coords[0] = 0
self.coords[2] = 160
elif self.coords[2] > 720:
self.coords[0] = 560
self.coords[2] = 720
else:
self.canvas.canvas.itemconfigure(self.paddle, fill=Colour.paddle)
self.coords = [
self.coords[0] + 40, self.coords[1],
self.coords[2] - 40, self.coords[3]]
self.canvas.canvas.coords(
self.paddle, self.coords[0], self.coords[1], self.coords[2],
self.coords[3])
def load(self, data):
self.canvas.paddle_speed = data["speed"]
self.coords = data["coords"]
self.canvas.canvas.coords(
self.paddle, self.coords[0], self.coords[1], self.coords[2],
self.coords[3])
def pause(self, paused):
self.paused = paused
if not paused:
self.move()
def move(self):
if self.paused:
return
if (self.coords[0] + self.canvas.paddle_speed < 720 - self.width +
8 and self.coords[0] + self.canvas.paddle_speed > 4):
self.canvas.canvas.move(self.paddle, self.canvas.paddle_speed, 0)
self.coords[0] += self.canvas.paddle_speed
self.coords[2] += self.canvas.paddle_speed
self.root.after(15, self.move)