-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSocketSnakeClient
More file actions
64 lines (55 loc) · 1.59 KB
/
SocketSnakeClient
File metadata and controls
64 lines (55 loc) · 1.59 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
import socket,pygame, time, random, pickle
from pygame.locals import *
pygame.init()
window=pygame.display.set_mode((500,500))
pygame.display.set_caption("Cool Python Game")
font=pygame.font.Font('freesansbold.ttf', 32)
xmove=0
ymove=0
class Player:
def draw(self,data):
for e in range(len(data)):
pygame.draw.rect(window,(255,0,0),(data[e][0],data[e][1],20,20))
class Player2:
def __init__(self):
self.x=random.randint(20,300)
self.y=random.randint(20,300)
def moveBy(self,x_distance,y_distance):
self.x += x_distance
self.y += y_distance
def draw(self):
pygame.draw.rect(window,(0,255,0),(self.x,self.y,12,12))
player2=Player2()
player1=Player()
host='192.168.1.43'
port=12345
conn=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn.connect((host,port))
xmove=0
ymove=0
while True:
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
exit()
if event.type==KEYDOWN:
if event.key==K_UP:
ymove= -1
xmove=0
elif event.key==K_DOWN:
ymove = 1
xmove=0
elif event.key==K_LEFT:
xmove = -1
ymove = 0
elif event.key==K_RIGHT:
xmove= 1
ymove = 0
player2.moveBy(xmove,ymove)
data = pickle.loads(conn.recv(256))
conn.send(bytes("{} {}".format(player2.x,player2.y),"utf-8"))
window.fill((255,255,255))
player2.draw()
player1.draw(data)
pygame.display.update()
conn.close()