-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpingPong.py
107 lines (86 loc) · 2.05 KB
/
pingPong.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
# pInG PoNg
# Started on 9|4|20
import turtle
# Game Window Properties
gameWindow = turtle.Screen()
gameWindow.title("`*pInG PoNg*")
gameWindow.bgcolor("blue")
gameWindow.setup(width = 800, height = 600)
gameWindow.tracer(0)
# Paddle A
paddleA = turtle.Turtle()
paddleA.speed("slowest")
paddleA.shape("square")
paddleA.color("brown")
paddleA.shapesize(stretch_wid = 5, stretch_len = 1)
paddleA.penup()
paddleA.goto(-350, 0)
# Paddle B
paddleB = turtle.Turtle()
paddleB.speed("slowest")
paddleB.shape("square")
paddleB.color("brown")
paddleB.shapesize(stretch_wid = 5, stretch_len = 1)
paddleB.penup()
paddleB.goto(350, 0)
# Ball
ball = turtle.Turtle()
ball.speed("slowest")
ball.shape("circle")
ball.color("red")
ball.penup()
ball.goto(0, 0)
ball.mvX = 0.3
ball.mvY = 0.3
# Paddle A Functions
def paddleAUp():
ycor = paddleA.ycor()
ycor += 20
paddleA.sety(ycor)
def paddleADown():
ycor = paddleA.ycor()
ycor -= 20
paddleA.sety(ycor)
# Paddle B Functions
def paddleBUp():
ycor = paddleB.ycor()
ycor += 20
paddleB.sety(ycor)
def paddleBDown():
ycor = paddleB.ycor()
ycor -= 20
paddleB.sety(ycor)
# Keyboard Binding
gameWindow.listen()
gameWindow.onkeypress(paddleAUp, "w")
gameWindow.onkeypress(paddleADown, "s")
gameWindow.onkeypress(paddleBUp, "Up")
gameWindow.onkeypress(paddleBDown, "Down")
# Game loop
gameOver = False
while not gameOver:
gameWindow.update()
# Mving the ball
ball.setx(ball.xcor() + ball.mvX)
ball.sety(ball.ycor() + ball.mvY)
# Borders
if ball.ycor() > 290:
ball.sety(290)
ball.mvY *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.mvY *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.mvX *= -1
if ball.xcor() < -390:
ball.goto(0, 0)
ball.mvX *= -1
# Collisions
if (ball.xcor() > 340 and ball.xcor() < 350) and (ball.ycor() < paddleB.ycor() + 50 and ball.ycor() > paddleB.ycor() - 50):
ball.setx(340)
ball.mvX *= -1
if (ball.xcor() < -340 and ball.xcor() > -350) and (ball.ycor() < paddleA.ycor() + 50 and ball.ycor() > paddleA.ycor() - 50):
ball.setx(-340)
ball.mvX *= -1
print("GAME OVER")