-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtetris_networking.py
80 lines (58 loc) · 1.8 KB
/
tetris_networking.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
import socket
import threading
UDP_IP = "127.0.0.1"
UDP_PORT = 3715
def main():
import sys
print(sys.argv)
import tetris
if __name__ == "__main__":
main()
def encode_blocks(self, blocks):
msg += "blocks:"
for block in blocks:
msg += str(block.x) + "," + str(block.y) + ";"
return msg
def decode_blocks(self):
msgtype, msg = rcvd.split(":")
if msgtype == "blocks":
coords = msgtype.strip(";").split(";")
blocks = []
for coord in coords:
x,y = coord.split(",")
blocks.append(Block(x, y, pygame.Color("grey")))
return blocks
class Server(threading.Thread):
def __init__(self, logic):
Thread.__init__(self)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
self.logic = logic
self.lock = threading.Lock()
def run(self):
data, addr = sock.recvfrom(1024)
while data != "disconnect":
blocks = decode_blocks(data)
class Client(threading.Thread):
def __init__(self, logic):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind((UDP_IP, UDP_PORT))
self.logic = logic
self.lock = threading.Lock()
class Peer(threading.Thread):
def __init__(self):
Thread.__init__(self)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
self.msg = ""
self.rcvd = ""
def start_server(self, udp_port = UDP_PORT):
sock.bind((UDP_IP, udp_port))
data, addr = self.sock.recvfrom(buf_size)
self.start()
def run(self):
pass
def encode_shape(self, shape):
pass
def send(self, blocks):
encoded = self.encode_blocks(blocks)
sock.sendto(self.msg, (UDP_IP, UDP_PORT))