-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
72 lines (61 loc) · 1.77 KB
/
main.py
File metadata and controls
72 lines (61 loc) · 1.77 KB
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
import turtle
from turtle import Screen
from paddle import Paddle
from ball import Ball
from bricks import Bricks
from scoreboard import Scoreboard
import time
# Setup screen
screen = Screen()
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("Breakout Classic")
screen.tracer(0)
# Game objects
paddle = Paddle()
ball = Ball()
bricks = Bricks()
scoreboard = Scoreboard()
# Controls
screen.listen()
screen.onkeypress(paddle.move_left, "Left")
screen.onkeypress(paddle.move_right, "Right")
# Main game loop
game_on = True
while game_on:
# Check if the screen is still open
try:
time.sleep(ball.move_speed)
screen.update()
ball.move()
# Wall bounce
if ball.xcor() > 380 or ball.xcor() < -380:
ball.bounce_x()
if ball.ycor() > 280:
ball.bounce_y()
# Paddle bounce
if ball.ycor() < -230 and paddle.xcor() - 60 < ball.xcor() < paddle.xcor() + 60:
ball.bounce_y()
# Brick collision
for brick in bricks.all_bricks[:]:
if brick.distance(ball) < 40:
brick.hideturtle()
bricks.all_bricks.remove(brick)
scoreboard.increase_score()
ball.bounce_y()
break
# Missed ball
if ball.ycor() < -290:
scoreboard.lose_life()
ball.reset_position()
if scoreboard.lives == 0:
scoreboard.game_over()
game_on = False
# Win
if not bricks.all_bricks:
scoreboard.show_win_message()
game_on = False
except turtle.Terminator:
# Exit the game loop if the screen is closed
game_on = False
screen.exitonclick()