-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
95 lines (76 loc) · 2.2 KB
/
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
import pygame
from network import Network
from random import randint
width = 500
height = 500
w = pygame.display.set_mode((width, height))
pygame.display.set_caption("Pong Game")
w.fill((0, 0, 0))
pygame.display.flip()
class Player():
def __init__(self, x, y, width, height, color):
self.x = x
self.y = y
self.width = width
self.height = height
self.color = color
self.rect = (x,y,width,height)
self.vel = 2
def draw(self, w):
pygame.draw.rect(w, self.color, self.rect)
def move(self):
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
if self.x>0: self.x -= self.vel
else: self.x = self.x
if keys[pygame.K_RIGHT]:
if self.x<400: self.x += self.vel
else: self.x = self.x
self.update()
def update(self):
self.rect = (self.x, self.y, self.width, self.height)
class Ball:
def __init__(self, cx, cy):
self.cx = cx
self.cy = cy
self.cir = ((0,0,255),(cx,cy),5)
self.m = self.n = 1
def draw(self, w):
pygame.draw.circle(w, *self.cir)
def update(self):
self.cir = ((0,0,255),(self.cx,self.cy),5)
def read_pos(s):
s = s.split(",")
return int(s[0]), int(s[1]), int(s[2]), int(s[3])
def make_pos(tup):
return str(tup[0]) + "," + str(tup[1]) + "," + str(tup[2]) + "," + str(tup[3])
def redrawwdow(w,player, player2,b):
w.fill((0,0,0))
player.draw(w)
player2.draw(w)
b.draw(w)
pygame.display.update()
def main():
run = True
n = Network()
startPos = read_pos(n.getPos())
p = Player(startPos[0],startPos[1],100,2,(0,255,0))
p2 = Player(100,50,100,2,(255,0,0))
b = Ball(startPos[2], startPos[3])
clock = pygame.time.Clock()
while run:
clock.tick(60)
p2Pos = read_pos(n.send(make_pos((p.x, p.y, b.cx, b.cy))))
p2.x = p2Pos[0]
p2.y = p2Pos[1]
b.cx = p2Pos[2]
b.cy = p2Pos[3]
p2.update()
b.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()
p.move()
redrawwdow(w, p, p2,b)
main()