-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbricks.py
109 lines (84 loc) · 2.63 KB
/
bricks.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from turtle import Turtle, mainloop, listen, onkeypress, tracer, Vec2D, bgcolor
from random import choice, randint
class Target(Turtle):
colors = ['green', 'orange', 'yellow']
def __init__(self, x, y):
super().__init__()
self.white = False
self.shapesize(1, 2.5)
self.color(choice(self.colors))
self.shape('square')
self.penup()
self.goto(x, y)
class Player(Turtle):
def __init__(self):
super().__init__()
self.shapesize(1, 5)
self.color('blue')
self.shape('square')
self.penup()
self.goto(0, -300)
def goleft(self):
if self.xcor() >= -240:
self.setx(self.xcor() + 15)
def goright(self):
if self.xcor() <= 240:
self.setx(self.xcor() - 5)
class Ball(Turtle):
def __init__(self):
super().__init__()
self.shapesize(1)
self.color('red')
self.shape('circle')
self.penup()
class Game:
tx, ty = -250, 300
dy = 1
dx = choice([-0.5, 0.5])
targets = []
def __init__(self):
tracer(0)
self.pl = Player()
self.ball = Ball()
for _ in range(5):
for _ in range(10):
target = Target(self.tx, self.ty)
self.targets.append(target)
self.tx += 55
self.ty -= 25
self.tx = -250
tracer(1)
def update(self):
if self.ball.ycor() < -300:
exit()
if self.ball.ycor() > 300:
self.dy *= -1
if self.ball.ycor() >= 175:
for target in self.targets:
if not target.white:
if self.ball.ycor() >= target.ycor() - 25:
if self.ball.xcor() >= target.xcor() - 25:
if self.ball.xcor() <= target.xcor() + 25:
self.dy *= -1
target.color('black')
target.white = True
if self.ball.xcor() <= -270 or self.ball.xcor() >= 260:
self.dx *= -1
if self.ball.ycor() <= self.pl.ycor() + 25:
if self.ball.xcor() >= self.pl.xcor() - 50:
if self.ball.xcor() <= self.pl.xcor() + 50:
self.dy *= -1
self.ball.setpos(self.ball.xcor() + self.dx * 3, self.ball.ycor() - self.dy * 3)
def enable_keys(pl):
onkeypress(pl.goleft, "Right")
onkeypress(pl.goright, "Left")
def start():
bgcolor(0, 0, 0)
game = Game()
enable_keys(game.pl)
listen()
while 1:
game.update()
if __name__ == '__main__':
start()
mainloop()