forked from Saber307/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdx_ball_main.py
178 lines (146 loc) · 4.25 KB
/
dx_ball_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import turtle
import time
wn = turtle.Screen()
wn.title("Dx Ball")
wn.bgcolor("black")
wn.setup(width=400, height=400)
wn.tracer(0)
# Score and Lives
score = 0
lives = 3
bricks = 0
# Paddle
paddle = turtle.Turtle()
paddle.speed(0)
paddle.shape("square")
paddle.color("white")
paddle.shapesize(stretch_wid=1, stretch_len=5)
paddle.penup()
paddle.goto(0, -150)
# Ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("white")
ball.penup()
ball.goto(0, -130)
ball.dx = 0.3
ball.dy = 0.3
# Pen for score
pen_score = turtle.Turtle()
pen_score.speed(0)
pen_score.color("white")
pen_score.penup()
pen_score.hideturtle()
pen_score.setposition(130, 170)
pen_score.write("Score: 0", align="center", font=("Courier", 12, "normal"))
# Pen for lives
pen_lives = turtle.Turtle()
pen_lives.speed(0)
pen_lives.color("white")
pen_lives.penup()
pen_lives.hideturtle()
pen_lives.setposition(-140, 170)
pen_lives.write("Lives: 3", align="center", font=("Courier", 12, "normal"))
# Pen for other
pen_other = turtle.Turtle()
pen_other.speed(0)
pen_other.color("white")
pen_other.penup()
pen_other.hideturtle()
# Brick class
class Brick:
def __init__(self, x_cor, y_cor, point, color):
self.x_cor = x_cor
self.y_cor = y_cor
self.point = point
self.exist = True
self.turtle_object = turtle.Turtle()
self.turtle_object.speed(0)
self.turtle_object.shape("square")
self.turtle_object.color("black", color)
self.turtle_object.shapesize(stretch_wid=1, stretch_len=3)
self.turtle_object.penup()
self.turtle_object.goto(x_cor, y_cor)
# Bricks
bricks_obj = []
for y_cors in range(70,151,20):
for x_cors in range(-120,121,60):
bricks_obj.append(Brick(x_cors, y_cors, 10, "white"))
red_bricks = [3,11,22]
for brick in red_bricks:
bricks_obj[brick].point = 20
bricks_obj[brick].turtle_object.color("black", "red")
# Function
def paddle_left():
x = paddle.xcor()
x -= 20
paddle.setx(x)
def paddle_right():
x = paddle.xcor()
x += 20
paddle.setx(x)
# Keyboard binding
wn.listen()
wn.onkeypress(paddle_left, "Left")
wn.onkeypress(paddle_right, "Right")
# Main game loop
while True:
wn.update()
# Move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
# Border checking for ball
if ball.xcor() > 175:
ball.setx(175)
ball.dx *= -1
if ball.xcor() < -175:
ball.setx(-175)
ball.dx *= -1
if ball.ycor() > 175:
ball.sety(175)
ball.dy *= -1
if ball.ycor() < -175:
ball.setposition(paddle.xcor(), paddle.ycor())
ball.dy *= -1
lives -= 1
pen_lives.clear()
pen_lives.write(f"Lives: {lives}", align="center", font=("Courier", 12, "normal"))
# Border checking for paddle
if paddle.xcor() > 140:
paddle.setx(140)
#ball.dx *= -1
if paddle.xcor() < -140:
paddle.setx(-140)
#ball.dx *= -1
# Paddle and ball collision
if paddle.ycor() < ball.ycor() < paddle.ycor() + 20 and paddle.xcor() - 50 < ball.xcor() < paddle.xcor() + 50:
ball.sety(-130)
ball.dy *= -1
# Bricks and ball collision
for brick in bricks_obj:
if brick.exist:
if brick.y_cor - 20 < ball.ycor() < brick.y_cor + 20 and brick.x_cor - 30 < ball.xcor() < brick.x_cor + 30:
brick.turtle_object.hideturtle()
ball.dx *= -1
ball.dy *= -1
brick.exist = False
bricks += 1
score += brick.point
pen_score.clear()
pen_score.write(f"Score: {score}", align="center", font=("Courier", 12, "normal"))
# Game wining
if lives > 0 and bricks == 25:
ball.hideturtle()
paddle.hideturtle()
pen_score.clear()
pen_lives.clear()
pen_other.setposition(0,0)
pen_other.write("You win", align="center", font=("Courier", 24, "normal"))
pen_other.setposition(0,-50)
pen_other.write(f"Score = {score}", align="center", font=("Courier", 12, "normal"))
if lives == 0:
ball.hideturtle()
paddle.hideturtle()
pen_other.setposition(0, 0)
pen_other.write("You Lose", align="center", font=("Courier", 24, "normal"))