-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverPong.py
More file actions
159 lines (131 loc) · 4.54 KB
/
serverPong.py
File metadata and controls
159 lines (131 loc) · 4.54 KB
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
import socket
import pygame
# import numpy
from _thread import *
from classes import Player
from classes import Ball
from classes import Settings
import pickle
black = (0, 0, 0)
white = (255, 255, 255)
red_bright = (255, 0, 0)
green_bright = (0, 255, 0)
blue_bright = (0, 0, 255)
server = ""
port = 5555
pygame.init()
clock = pygame.time.Clock()
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ball = Ball(284, 184, 16, 16, white)
gameRunning = False
try:
s.bind((server, port))
except socket.error as e:
str(e)
print(e)
s.listen()
print("Waiting for a connection, server started.")
connected = set()
games = {}
idCount = 0
setting_vals = Settings(50, 50, 5, 5, 3, 7)
score = [0, 0]
def ball_collision_check():
while True:
# Check for collision of ball with either player
if pygame.Rect(players[0].rect).collidepoint(ball.x, ball.y + 8):
if ball.y + 8 >= players[0].y + (players[0].height / 2):
ball.yvel = round(abs((ball.y + 8) - (players[0].y + players[0].height / 2)) / (players[0].height / 2) * ball.yvelmax, 0)
elif ball.y + 8 <= players[0].y + (players[0].height / 2):
ball.yvel = round(abs((ball.y + 8) - (players[0].y + players[0].height / 2)) / (players[0].height / 2) * ball.yvelmax, 0) * -1
ball.yvel = int(ball.yvel)
ball.xvel *= -1
ball.x += 5
print("collision with p1")
print(ball.yvel)
elif pygame.Rect(players[1].rect).collidepoint(ball.x + 16, ball.y + 8):
if ball.y + 8 > players[1].y + (players[1].height / 2):
ball.yvel = round(abs((ball.y + 8) - (players[1].y + players[1].height / 2)) / (players[1].height / 2) * ball.yvelmax, 0)
elif ball.y + 8 < players[1].y + (players[1].height / 2):
ball.yvel = round(abs((ball.y + 8) - (players[1].y + players[1].height / 2)) / (players[1].height / 2) * ball.yvelmax, 0) * -1
ball.yvel = int(ball.yvel)
ball.xvel *= -1
ball.x -= 5
print("collision with p2")
print(ball.yvel)
# Check if a player wall is hit
elif ball.x <= 0:
# reset ball location
ball.x = 284
ball.y = 184
score[1] += 1
elif ball.x >= 574:
# reset ball location
ball.x = 284
ball.y = 184
score[0] += 1
if ball.y <= 0 or ball.y >= 384:
ball.yvel *= -1
ball.update()
clock.tick(60)
# start position players
players = [Player(0, 0, int(200-(50/2)), 10, 50, white),
Player(1, 590, int(200-(50/2)), 10, 50, white)]
def threaded_client(conn, player, ball):
global setting_vals
initial_send = {
"player": players[player],
"ball": ball,
"score": score
}
conn.send(pickle.dumps(initial_send["player"]))
setup = False
gameRunning = False
while True:
data = pickle.loads(conn.recv(2048))
players[player].y = data["player"].y
players[player].update()
players[player].ready = data["player"].ready
if player == 0:
setting_vals = data["settings"]
# once both players connected we need to start the gameloop
if not setup and gameRunning and player == 0:
players[0].height = setting_vals.p1size
players[1].height = setting_vals.p2size
players[0].vel = setting_vals.p1vel
players[1].vel = setting_vals.p2vel
ball.xvel = setting_vals.ballxvel
ball.yvelmax = setting_vals.ballyvel
players[0].update()
players[1].update()
start_new_thread(ball_collision_check, ())
setup = True
if not data:
print("disconnected")
break
else:
reply = {
"ball": ball,
"score": score
}
if players[0].ready and players[1].ready:
reply["ready"] = True
gameRunning = True
else:
reply["ready"] = False
if player == 1:
reply["player"] = players[0]
reply["settings"] = setting_vals
else:
reply["player"] = players[1]
conn.sendall(pickle.dumps(reply))
print("Lost connection")
conn.close()
while True:
conn, addr = s.accept()
print("Connected to:", addr)
idCount += 1
p = 0
if idCount % 2 == 0:
p = 1
start_new_thread(threaded_client, (conn, p, ball))