-
Notifications
You must be signed in to change notification settings - Fork 0
/
Normal_Pong.py
67 lines (55 loc) · 2.24 KB
/
Normal_Pong.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
import turtle
import time
import random as rand
from constants import vw2, vh2, vh
from window import wn
from funktions import score_a, score_b, ball, paddle_a, paddle_b, Timer, pen, paddle_a_up, paddle_a_down, paddle_b_up, paddle_b_down
def Normal_Pong():
start = time.time()
StartSeq()
#main loop
while True:
score_a = 0
score_b = 0
timeBegin = time.time()
Now = timeBegin - start
Now = (int(Now*10))/10
wn.update()
Timer.clear()
Timer.write("{}".format(Now) , align="center", font=("Courier", 24, "normal"))
#move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#border check
if ball.ycor() > (vh2-10):
ball.sety((vh2-10))
ball.dy *= -1
if ball.ycor() < -(vh2-10):
ball.sety(-(vh2-10))
ball.dy *= -1
if ball.xcor() > (vw2-10):
ball.goto(0, rand.randint(-(vh-50), (vh-50)))
ball.dx = (rand.randint(2, 10)/10)
ball.dy = 1 - ball.dx
ball.dx *= -1
score_a += 1
pen.clear()
pen.write("Player A: {} Player B: {}".format(score_a, score_b) , align="center", font=("Courier", 24, "normal"))
if ball.xcor() < -(vw2-10):
ball.goto(0, rand.randint(-(vh-50), (vh-50)))
ball.dx = (rand.randint(2, 2))
ball.dy = 1 - ball.dx
ball.dx *= -1
score_b += 1
pen.clear()
pen.write("Player A: {} Player B: {}".format(score_a, score_b) , align="center", font=("Courier", 24, "normal"))
#paddle hit ball
if (ball.xcor() < -(vw2-60)) and (ball.xcor() > -(vw2-50)) and (ball.ycor() < paddle_a.ycor() + 40) and (ball.ycor() > paddle_a.ycor() - 40):
ball.setx(-(vw2-60))
ball.dx *= -1
if (ball.xcor() > (vw2-60)) and (ball.xcor() < (vw2-50)) and (ball.ycor() < paddle_b.ycor() + 40) and (ball.ycor() > paddle_b.ycor() - 40):
ball.setx((vw2-60))
ball.dx *= -1
timeElapsed = time.time() - timeBegin
if timeElapsed < 0.05:
time.sleep(0.05 - timeElapsed)