forked from arsh939/Python-Projects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPing_Pong_Game.py
142 lines (127 loc) · 4.28 KB
/
Ping_Pong_Game.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
from tkinter import *
import random
WIDTH = 900
HEIGHT = 300
PAD_W = 10
PAD_H = 100
BALL_SPEED_UP = 1.05
BALL_MAX_SPEED = 40
BALL_RADIUS = 30
INITIAL_SPEED = 20
BALL_X_SPEED = INITIAL_SPEED
BALL_Y_SPEED = INITIAL_SPEED
PLAYER_1_SCORE = 0
PLAYER_2_SCORE = 0
right_line_distance = WIDTH - PAD_W
def update_score(player):
global PLAYER_1_SCORE, PLAYER_2_SCORE
if player == "right":
PLAYER_1_SCORE += 1
c.itemconfig(p_1_text, text=PLAYER_1_SCORE)
else:
PLAYER_2_SCORE += 1
c.itemconfig(p_2_text, text=PLAYER_2_SCORE)
def spawn_ball():
global BALL_X_SPEED
c.coords(BALL, WIDTH/2-BALL_RADIUS/2,
HEIGHT/2-BALL_RADIUS/2,
WIDTH/2+BALL_RADIUS/2,
HEIGHT/2+BALL_RADIUS/2)
BALL_X_SPEED = -(BALL_X_SPEED * -INITIAL_SPEED) / abs(BALL_X_SPEED)
def bounce(action):
global BALL_X_SPEED, BALL_Y_SPEED
if action == "strike":
BALL_Y_SPEED = random.randrange(-10, 10)
if abs(BALL_X_SPEED) < BALL_MAX_SPEED:
BALL_X_SPEED *= -BALL_SPEED_UP
else:
BALL_X_SPEED = -BALL_X_SPEED
else:
BALL_Y_SPEED = -BALL_Y_SPEED
root = Tk()
root.title("Pong")
c = Canvas(root, width=WIDTH, height=HEIGHT, background="#003300")
c.pack()
c.create_line(PAD_W, 0, PAD_W, HEIGHT, fill="white")
c.create_line(WIDTH-PAD_W, 0, WIDTH-PAD_W, HEIGHT, fill="white")
c.create_line(WIDTH/2, 0, WIDTH/2, HEIGHT, fill="white")
BALL = c.create_oval(WIDTH/2-BALL_RADIUS/2,
HEIGHT/2-BALL_RADIUS/2,
WIDTH/2+BALL_RADIUS/2,
HEIGHT/2+BALL_RADIUS/2, fill="white")
LEFT_PAD = c.create_line(PAD_W/2, 0, PAD_W/2, PAD_H, width=PAD_W, fill="yellow")
RIGHT_PAD = c.create_line(WIDTH-PAD_W/2, 0, WIDTH-PAD_W/2,
PAD_H, width=PAD_W, fill="yellow")
p_1_text = c.create_text(WIDTH-WIDTH/6, PAD_H/4,
text=PLAYER_1_SCORE,
font="Arial 20",
fill="white")
p_2_text = c.create_text(WIDTH/6, PAD_H/4,
text=PLAYER_2_SCORE,
font="Arial 20",
fill="white")
BALL_X_CHANGE = 20
BALL_Y_CHANGE = 0
def move_ball():
ball_left, ball_top, ball_right, ball_bot = c.coords(BALL)
ball_center = (ball_top + ball_bot) / 2
if ball_right + BALL_X_SPEED < right_line_distance and \
ball_left + BALL_X_SPEED > PAD_W:
c.move(BALL, BALL_X_SPEED, BALL_Y_SPEED)
elif ball_right == right_line_distance or ball_left == PAD_W:
if ball_right > WIDTH / 2:
if c.coords(RIGHT_PAD)[1] < ball_center < c.coords(RIGHT_PAD)[3]:
bounce("strike")
else:
update_score("left")
spawn_ball()
else:
if c.coords(LEFT_PAD)[1] < ball_center < c.coords(LEFT_PAD)[3]:
bounce("strike")
else:
update_score("right")
spawn_ball()
else:
if ball_right > WIDTH / 2:
c.move(BALL, right_line_distance-ball_right, BALL_Y_SPEED)
else:
c.move(BALL, -ball_left+PAD_W, BALL_Y_SPEED)
if ball_top + BALL_Y_SPEED < 0 or ball_bot + BALL_Y_SPEED > HEIGHT:
bounce("ricochet")
PAD_SPEED = 20
LEFT_PAD_SPEED = 0
RIGHT_PAD_SPEED = 0
def move_pads():
PADS = {LEFT_PAD: LEFT_PAD_SPEED,
RIGHT_PAD: RIGHT_PAD_SPEED}
for pad in PADS:
c.move(pad, 0, PADS[pad])
if c.coords(pad)[1] < 0:
c.move(pad, 0, -c.coords(pad)[1])
elif c.coords(pad)[3] > HEIGHT:
c.move(pad, 0, HEIGHT - c.coords(pad)[3])
def main():
move_ball()
move_pads()
root.after(30, main)
c.focus_set()
def movement_handler(event):
global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
if event.keysym == "w":
LEFT_PAD_SPEED = -PAD_SPEED
elif event.keysym == "s":
LEFT_PAD_SPEED = PAD_SPEED
elif event.keysym == "Up":
RIGHT_PAD_SPEED = -PAD_SPEED
elif event.keysym == "Down":
RIGHT_PAD_SPEED = PAD_SPEED
c.bind("<KeyPress>", movement_handler)
def stop_pad(event):
global LEFT_PAD_SPEED, RIGHT_PAD_SPEED
if event.keysym in "ws":
LEFT_PAD_SPEED = 0
elif event.keysym in ("Up", "Down"):
RIGHT_PAD_SPEED = 0
c.bind("<KeyRelease>", stop_pad)
main()
root.mainloop()