-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaddle.py
38 lines (30 loc) · 1.1 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
from turtle import Turtle
class Paddle(Turtle):
STRETCH_WID = 10
def __init__(self, width, height):
super().__init__()
self.penup()
self.shape("square")
self.shapesize(stretch_wid=self.STRETCH_WID, stretch_len=1)
self.color("white")
self.goto(0, -250)
self.setheading(90)
self.width = width
self.height = height
# Legacy code, move with arrow keys
def move_left(self):
if self.xcor() - self.STRETCH_WID * 11.5 > -self.width / 2:
new_x = self.xcor() - 30
self.goto(new_x, self.ycor())
# Legacy code, move with arrow keys
def move_right(self):
if self.xcor() + self.STRETCH_WID * 12 < self.width / 2:
new_x = self.xcor() + 30
self.goto(new_x, self.ycor())
# Paddle follows mouse movements
def on_motion(self, event):
x = event.x - self.width / 2
# Check for screen boundaries
if x > - self.width / 2 + self.STRETCH_WID * 10:
if x < self.width / 2 - self.STRETCH_WID * 11:
self.goto(x, self.ycor())