-
Notifications
You must be signed in to change notification settings - Fork 198
/
game.py
271 lines (218 loc) · 7.68 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
'''
the main game
author:@techwithtim
requirements:see requirements.txt
'''
import subprocess
import sys
import get_pip
def install(package):
subprocess.call([sys.executable, "-m", "pip", "install", package])
try:
print("[GAME] Trying to import pygame")
import pygame
except:
print("[EXCEPTION] Pygame not installed")
try:
print("[GAME] Trying to install pygame via pip")
import pip
install("pygame")
print("[GAME] Pygame has been installed")
except:
print("[EXCEPTION] Pip not installed on system")
print("[GAME] Trying to install pip")
get_pip.main()
print("[GAME] Pip has been installed")
try:
print("[GAME] Trying to install pygame")
import pip
install("pygame")
print("[GAME] Pygame has been installed")
except:
print("[ERROR 1] Pygame could not be installed")
import pygame
import os
import time
from client import Network
import pickle
pygame.font.init()
board = pygame.transform.scale(pygame.image.load(os.path.join("img","board_alt.png")), (750, 750))
chessbg = pygame.image.load(os.path.join("img", "chessbg.png"))
rect = (113,113,525,525)
turn = "w"
def menu_screen(win, name):
global bo, chessbg
run = True
offline = False
while run:
win.blit(chessbg, (0,0))
small_font = pygame.font.SysFont("comicsans", 50)
if offline:
off = small_font.render("Server Offline, Try Again Later...", 1, (255, 0, 0))
win.blit(off, (width / 2 - off.get_width() / 2, 500))
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
offline = False
try:
bo = connect()
run = False
main()
break
except:
print("Server Offline")
offline = True
def redraw_gameWindow(win, bo, p1, p2, color, ready):
win.blit(board, (0, 0))
bo.draw(win, color)
formatTime1 = str(int(p1//60)) + ":" + str(int(p1%60))
formatTime2 = str(int(p2 // 60)) + ":" + str(int(p2 % 60))
if int(p1%60) < 10:
formatTime1 = formatTime1[:-1] + "0" + formatTime1[-1]
if int(p2%60) < 10:
formatTime2 = formatTime2[:-1] + "0" + formatTime2[-1]
font = pygame.font.SysFont("comicsans", 30)
try:
txt = font.render(bo.p1Name + "\'s Time: " + str(formatTime2), 1, (255, 255, 255))
txt2 = font.render(bo.p2Name + "\'s Time: " + str(formatTime1), 1, (255,255,255))
except Exception as e:
print(e)
win.blit(txt, (520,10))
win.blit(txt2, (520, 700))
txt = font.render("Press q to Quit", 1, (255, 255, 255))
win.blit(txt, (10, 20))
if color == "s":
txt3 = font.render("SPECTATOR MODE", 1, (255, 0, 0))
win.blit(txt3, (width/2-txt3.get_width()/2, 10))
if not ready:
show = "Waiting for Player"
if color == "s":
show = "Waiting for Players"
font = pygame.font.SysFont("comicsans", 80)
txt = font.render(show, 1, (255, 0, 0))
win.blit(txt, (width/2 - txt.get_width()/2, 300))
if not color == "s":
font = pygame.font.SysFont("comicsans", 30)
if color == "w":
txt3 = font.render("YOU ARE WHITE", 1, (255, 0, 0))
win.blit(txt3, (width / 2 - txt3.get_width() / 2, 10))
else:
txt3 = font.render("YOU ARE BLACK", 1, (255, 0, 0))
win.blit(txt3, (width / 2 - txt3.get_width() / 2, 10))
if bo.turn == color:
txt3 = font.render("YOUR TURN", 1, (255, 0, 0))
win.blit(txt3, (width / 2 - txt3.get_width() / 2, 700))
else:
txt3 = font.render("THEIR TURN", 1, (255, 0, 0))
win.blit(txt3, (width / 2 - txt3.get_width() / 2, 700))
pygame.display.update()
def end_screen(win, text):
pygame.font.init()
font = pygame.font.SysFont("comicsans", 80)
txt = font.render(text,1, (255,0,0))
win.blit(txt, (width / 2 - txt.get_width() / 2, 300))
pygame.display.update()
pygame.time.set_timer(pygame.USEREVENT+1, 3000)
run = True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
run = False
elif event.type == pygame.KEYDOWN:
run = False
elif event.type == pygame.USEREVENT+1:
run = False
def click(pos):
"""
:return: pos (x, y) in range 0-7 0-7
"""
x = pos[0]
y = pos[1]
if rect[0] < x < rect[0] + rect[2]:
if rect[1] < y < rect[1] + rect[3]:
divX = x - rect[0]
divY = y - rect[1]
i = int(divX / (rect[2]/8))
j = int(divY / (rect[3]/8))
return i, j
return -1, -1
def connect():
global n
n = Network()
return n.board
def main():
global turn, bo, name
color = bo.start_user
count = 0
bo = n.send("update_moves")
bo = n.send("name " + name)
clock = pygame.time.Clock()
run = True
while run:
if not color == "s":
p1Time = bo.time1
p2Time = bo.time2
if count == 60:
bo = n.send("get")
count = 0
else:
count += 1
clock.tick(30)
try:
redraw_gameWindow(win, bo, p1Time, p2Time, color, bo.ready)
except Exception as e:
print(e)
end_screen(win, "Other player left")
run = False
break
if not color == "s":
if p1Time <= 0:
bo = n.send("winner b")
elif p2Time <= 0:
bo = n.send("winner w")
if bo.check_mate("b"):
bo = n.send("winner b")
elif bo.check_mate("w"):
bo = n.send("winner w")
if bo.winner == "w":
end_screen(win, "White is the Winner!")
run = False
elif bo.winner == "b":
end_screen(win, "Black is the winner")
run = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
quit()
pygame.quit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q and color != "s":
# quit game
if color == "w":
bo = n.send("winner b")
else:
bo = n.send("winner w")
if event.key == pygame.K_RIGHT:
bo = n.send("forward")
if event.key == pygame.K_LEFT:
bo = n.send("back")
if event.type == pygame.MOUSEBUTTONUP and color != "s":
if color == bo.turn and bo.ready:
pos = pygame.mouse.get_pos()
bo = n.send("update moves")
i, j = click(pos)
bo = n.send("select " + str(i) + " " + str(j) + " " + color)
n.disconnect()
bo = 0
menu_screen(win)
name = input("Please type your name: ")
width = 750
height = 750
win = pygame.display.set_mode((width, height))
pygame.display.set_caption("Chess Game")
menu_screen(win, name)