This repository was archived by the owner on Jun 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
170 lines (141 loc) · 4.29 KB
/
client.py
File metadata and controls
170 lines (141 loc) · 4.29 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
160
161
162
163
164
165
166
167
168
169
170
# References:
# https://github.com/KyrosDigital/SnakeGame
# https://github.com/engineer-man/youtube/tree/master/015
import socket
import argparse
import curses
import pickle
import random
import time
# Global variables
positions = []
snakes = []
SNAKE_LENGTH = 5
BUFFER_SIZE = 1024
def create_board(s, player_num):
data = s.recv(BUFFER_SIZE)
winsize = pickle.loads(data)
s.send('SIZE_RECEIVED'.encode('utf-8'))
sh, sw = winsize[0], winsize[1]
curses.initscr()
curses.curs_set(0)
window = curses.newwin(sh, sw, 0, 0)
window.keypad(1)
window.timeout(1)
window.border(0)
data = s.recv(BUFFER_SIZE)
global positions
positions = pickle.loads(data)
for i in range(len(positions)):
temp_snake = []
for j in range(0, -1*SNAKE_LENGTH, -1):
temp_snake.append((positions[i][1], positions[i][0]-j))
global snakes
snakes.append(temp_snake)
if i == player_num:
for j in range(0, SNAKE_LENGTH):
window.addch(snakes[i][j][0], snakes[i][j][1], curses.ACS_BLOCK)
else:
for j in range(0, SNAKE_LENGTH):
window.addch(snakes[i][j][0], snakes[i][j][1], curses.ACS_CKBOARD)
return window, positions, sh, sw
def create_socket(ip_adress, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip_adress, port))
print('Connected to ', ip_adress, port, '\nWaiting for other players...\n')
return s
def update_board(new_positions, player_num, window):
window.clear()
window.border(0)
global snakes
for i in range(len(new_positions)):
snakes[i].pop()
new_head = (new_positions[i][1], new_positions[i][0])
snakes[i].insert(0, new_head)
if i == player_num:
for j in range(0, SNAKE_LENGTH):
window.addch(snakes[i][j][0], snakes[i][j][1], curses.ACS_BLOCK)
else:
if (new_positions[i][1] != -1) and (new_positions[i][0] != -1):
for j in range(0, SNAKE_LENGTH):
window.addch(snakes[i][j][0], snakes[i][j][1], curses.ACS_CKBOARD)
head_x = snakes[player_num][0][1]
head_y = snakes[player_num][0][0]
global positions
for i in range(len(positions)):
if i != player_num:
for j in range(SNAKE_LENGTH):
if snakes[i][j][0] == head_y and snakes[i][j][1] == head_x:
msg = 'Head to body collision detected'
return msg
positions = new_positions
return None
def main():
parser = argparse.ArgumentParser(description='Starts the client. ')
parser.add_argument('ip_adress', nargs=1, default='192.168.10.4')
parser.add_argument('port', type=int, nargs=1, default=9999)
args = parser.parse_args()
player_num = -1
window = None
max_x = 0
max_y = 0
key_list = [curses.KEY_UP, curses.KEY_DOWN, curses.KEY_LEFT, curses.KEY_RIGHT]
key = -1
s = create_socket(args.ip_adress[0], args.port[0])
data = s.recv(BUFFER_SIZE)
data = data.decode('utf-8')
player_num = int(data)
data = s.recv(BUFFER_SIZE)
data = data.decode('utf-8')
if data == 'CREATE_BOARD':
s.send('STARTED_MAKING_BOARD'.encode('utf-8'))
global positions
window, positions, max_y, max_x = create_board(s, player_num)
y_ratio = float(positions[player_num][1]) / max_y
x_ratio = float(positions[player_num][0]) / max_x
if (y_ratio < 0.5) and (x_ratio < 0.5):
key = random.choice([key_list[1], key_list[3]])
elif (y_ratio < 0.5) and (x_ratio >= 0.5):
key = random.choice([key_list[1], key_list[2]])
elif (y_ratio >= 0.5) and (x_ratio < 0.5):
key = random.choice([key_list[0], key_list[3]])
else:
key = random.choice([key_list[0], key_list[2]])
temp = None
while True:
next_key = window.getch()
time.sleep(0.005)
if next_key != -1:
key = next_key
try:
if key in key_list:
if temp != None:
s.send(str(temp).encode('utf-8'))
else:
s.send(str(key).encode('utf-8'))
data = s.recv(BUFFER_SIZE)
response = pickle.loads(data)
if response == 'You won!':
print(response)
window.clear()
curses.endwin()
break
if response == 'Head to Head collision detected.':
window.clear()
print(response, '\nGAME OVER.')
curses.endwin()
break
if response == 'Head to body collision detected':
print(response, '\nGAME OVER.')
curses.endwin()
break
if response == 'Out of bounds. ':
print(response, '\nGAME OVER.')
curses.endwin()
break
temp = update_board(response, player_num, window)
except socket.error:
print ('Error. GAME OVER')
s.close()
if __name__ == '__main__':
main()