-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworld.py
60 lines (41 loc) · 1.42 KB
/
world.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
import settings
from utilities.vector import *
from utilities.sprites import Sprite
class World:
def __init__(self):
self.me = my = Paddle()
self.you = your = Paddle()
self.ball = ball = Ball()
self.field = field = settings.field_shape
self.winner = None
# Place the paddles in the right spots.
my.shape.align_left(field)
my.shape.align_vertical(field)
your.shape.align_right(field)
your.shape.align_vertical(field)
# Place the ball in the middle of the screen.
ball.shape.align_horizontal(field)
ball.shape.align_vertical(field)
class Paddle(Sprite):
def __init__(self):
Sprite.__init__(self)
self.shape = settings.paddle_shape.copy()
self.velocity = 0
self.score = 0
self.target = Vector(0, 0)
def update(self, time):
direction = Vector(0, 1)
displacement = self.velocity * direction * time
self.shape.displace(displacement)
class Ball(Sprite):
def __init__(self):
Sprite.__init__(self)
speed = settings.ball_speed
direction = Vector.random()
x, y = abs(direction)
if y > x: direction = direction.orthogonal
self.shape = settings.ball_shape
self.velocity = speed * direction
def update(self, time):
displacement = self.velocity * time
self.shape.displace(displacement)