-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
97 lines (78 loc) · 2.94 KB
/
main.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
import time
from turtle import Screen
from ball import Ball
from paddle import Paddle
from brickwall import BrickWall
from scoreboard import ScoreBoard
WIDTH = 1000
HEIGHT = 800
SCREEN_COLOR = "black"
SCREEN_TITLE = "BREAK OUT GAME"
COLLISION_MARGIN_PADDLE = 100
PADDLE_HEIGHT = 20
COLLISION_MARGIN_BRICK = 20
SCREEN_REFRESH = 0.02
game_score = ScoreBoard()
play_again = True
while play_again:
screen = Screen()
screen.bgcolor(SCREEN_COLOR)
screen.title(SCREEN_TITLE)
screen.setup(width=WIDTH, height=HEIGHT)
screen.tracer(0)
game_wall = BrickWall(game_score.level)
game_paddle = Paddle()
game_ball = Ball()
game_score.refresh()
screen.listen()
screen.onkey(game_paddle.move_ship_left, "Left")
screen.onkey(game_paddle.move_ship_right, "Right")
game_on = True
while game_on:
time.sleep(SCREEN_REFRESH)
game_ball.move()
screen.update()
# check for collisions with paddle
if game_paddle.ycor() - PADDLE_HEIGHT <= game_ball.ycor() <= game_paddle.ycor() + PADDLE_HEIGHT and \
game_paddle.distance(game_ball) <= COLLISION_MARGIN_PADDLE:
game_ball.bounce(bounce_on_y=True, paddle_bounce=True)
# check for collisions with screen sides
if abs(game_ball.xcor()) >= WIDTH / 2:
game_ball.bounce(bounce_on_x=True)
# check for collisions with screen top
if game_ball.ycor() >= HEIGHT / 2:
game_ball.bounce(bounce_on_y=True)
# check for collisions with screen bottom
if game_ball.ycor() <= -HEIGHT / 2:
game_score.reduce_life()
game_paddle.reset()
game_ball.reset()
# check for collisions with bricks
for brick in game_wall.bricks:
if brick.distance(game_ball) <= COLLISION_MARGIN_BRICK:
brick.has_been_hit = True
game_score.update_score()
if game_ball.ycor() >= brick.bottom_side:
game_ball.bounce(bounce_on_y=True)
elif game_ball.ycor() <= brick.top_side:
game_ball.bounce(bounce_on_y=True)
elif game_ball.xcor() >= brick.left_side:
game_ball.bounce(bounce_on_x=True)
elif game_ball.xcor() <= brick.right_side:
game_ball.bounce(bounce_on_x=True)
# remove bricks that has been hit
game_wall.remove_has_been_hit()
if len(game_wall.bricks) <= 0:
game_score.next_level()
screen.clear()
break
if game_score.lives <= 0:
game_on = False
if not game_on:
game_score.game_over()
repeat = screen.textinput("Game Over!", "Press Y to play again. N to quit")
if repeat.upper() == "Y":
screen.clear()
else:
play_again = False
screen.mainloop()