-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpong.py
119 lines (96 loc) · 2.83 KB
/
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
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
import turtle
# window se refaire a ecran
window = turtle.Screen() #S doit etre en majusccule
window.title("Pong game by Christa")
window.bgcolor("white") # bg = background color
window.setup(width = 800, height = 600)
window.tracer(0) # stop the window from updating
#score
score_A =0
score_B =0
#premeiere raquette
pad_A = turtle.Turtle()
pad_A.speed(0)
pad_A.shape("square")
pad_A.color("black")
pad_A.shapesize(stretch_wid= 5, stretch_len= 1)
pad_A.penup()
pad_A.goto(-350, 0)
# deuxieume raquette
pad_B = turtle.Turtle()
pad_B.speed(0)
pad_B.shape("square")
pad_B.color("black")
pad_B.shapesize(stretch_wid= 5, stretch_len= 1)
pad_B.penup()
pad_B.goto(350, 0)
# ball
ball = turtle.Turtle()
ball.speed(0)
ball.shape("circle")
ball.color("black")
ball.penup()
ball.goto(0, 0)
ball.dx = 0.1
ball.dy = 0.1
#pen
pen = turtle.Turtle()
pen.speed(0)
pen.color("Black")
pen.penup()
pen.goto(0, 260)
pen.write("Joueur 1: 0 Joueur 2: 0", align = "center", font=("courier", 24, "normal"))
#Function
def pad_A_up():
y =pad_A.ycor() #ycol will return the value of the y cordinate
y += 20 #add 20 pixel to the pad
pad_A.sety(y)
def pad_A_down():
y =pad_A.ycor() #ycol will return the value of the y cordinate
y -= 20 #add 20 pixel to the pad
pad_A.sety(y)
def pad_B_up():
y =pad_B.ycor() #ycol will return the value of the y cordinate
y += 20 #add 20 pixel to the pad
pad_B.sety(y)
def pad_B_down():
y =pad_B.ycor() #ycol will return the value of the y cordinate
y -= 20 #add 20 pixel to the pad
pad_B.sety(y)
# keyboard biding
window.listen() # listen for keyboard input
window.onkeypress(pad_A_up, "w") #if the user press w
window.onkeypress(pad_A_down, "s")
window.onkeypress(pad_B_up, "Up")
window.onkeypress(pad_B_down, "Down")
#Main game loop
while True:
window.update()
#move the ball
ball.setx(ball.xcor() + ball.dx)
ball.sety(ball.ycor() + ball.dy)
#border checking
if ball.ycor() > 290:
ball.sety(290)
ball.dy *= -1
if ball.ycor() < -290:
ball.sety(-290)
ball.dy *= -1
if ball.xcor() > 390:
ball.goto(0, 0)
ball.dx *= -1
score_A += 1
pen.clear()
pen.write("Joueur 1: {} Joueur 2: {}".format(score_A, score_B), align = "center", font=("sans serif", 24, "normal"))
if ball.xcor() < -390:
ball.goto(0, 0)
ball.dx *= -1
score_B += 1
pen.clear()
# collision avec la ball
if ball.xcor() > 340 and (ball.ycor() < pad_B.ycor() + 40 and ball.ycor() > pad_B.ycor() - 40):
ball.setx(340)
ball.dx *= -1
if ball.xcor() < -340 and (ball.ycor() < pad_A.ycor() + 40 and ball.ycor() > pad_A.ycor() - 40):
ball.setx(-340)
ball.dx *= -1