-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessions.py
60 lines (45 loc) · 1.4 KB
/
sessions.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
"""
Defines sessions
"""
import socket
import pygame
from pygame.locals import *
import threading
from game_thread import ServerGameThread
class Session:
def __init__(self, ID):
self.ID =ID
self.members = list()
self.started = False
self.socket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
# Define game objects
self.ball = Rect(392, 392, 16, 16)
self.player_left = Rect(16, 300, 20, 200)
self.player_right = Rect(764, 300, 20, 200)
self.velx = 9
self.vely = 8
self.thread = ServerGameThread(self)
def add_member(self, member):
self.members.append(member)
def __len__(self):
return len(self.members)
def start(self):
self.started = True
playerID = 0
for member in self.members:
self.socket.sendto("start:{}:{}".format(self.ID, playerID).encode(), member.address)
playerID += 1
self.thread.start()
def update_player(self, data):
if data[1] == "0":
self.player_left.x = int(data[2])
self.player_left.y = int(data[3])
elif data[1] == "1":
self.player_right.x = int(data[2])
self.player_right.y = int(data[3])
def game_loop(self):
while True:
pass
class Member:
def __init__(self, address):
self.address = address