-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
223 lines (193 loc) · 6.8 KB
/
main.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
import pygame
from time import sleep
from MAP.map import Map
from copy import deepcopy
from time import time
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
pygame.init()
width = 400
height = 500
time_limit = 30
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption('Link point')
# icon = pygame.image.load('IMAGE\imagination.png')
# pygame.display.set_icon(icon)
def clear_screen():
screen.fill(black)
print_on_game("CREATOR: HQH", 190, 470, white)
def Get_Control():
for event in pygame.event.get():
if event.type == pygame.QUIT:
return "EXIT GAME"
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
return "END GAME"
if event.key == pygame.K_r:
return "RE_GAME"
if event.key == pygame.K_SPACE:
return 'PICK'
if event.key == pygame.K_LEFT:
return 'LEFT'
if event.key == pygame.K_RIGHT:
return 'RIGHT'
if event.key == pygame.K_UP:
return 'UP'
if event.key == pygame.K_DOWN:
return 'DOWN'
if event.key == pygame.K_y:
return 'YES'
if event.key == pygame.K_n:
return 'NO'
return 'NUL'
def print_on_game(TEXT, x, y, color):
font = pygame.font.Font(None, 36)
text = font.render(TEXT, True, color)
# Vẽ chữ lên màn hình
screen.blit(text, (x, y))
pygame.display.update()
class Link_points:
def __init__(self, n_point):
self.fps = 30
self.clock = pygame.time.Clock()
self.game_map = Map(10, 10, n_point)
self.game_backup = deepcopy(self.game_map)
def get_statement(self, is_checked):
if is_checked and self.game_map.is_lost():
return "YOU LOST"
if self.game_map.n_point_rem == 0:
return "YOU WIN"
return "NUL"
def roll_back(self):
self.game_map = deepcopy(self.game_backup)
def start_game(self):
clear_screen()
print_on_game("Time remain:", 10, 60, green)
start = time()
while True:
rem = time_limit - (time() - start)
for i in range(time_limit):
if i < rem: pygame.draw.rect(screen, green, (50 + 10 * i + 1, 89, 9, 10))
else: pygame.draw.rect(screen, black, (50 + 10 * i + 1, 89, 9, 10))
if rem <= 0:
print_on_game("TIME'S UP..", 10, 10, red)
return True
Command = Get_Control()
if (Command == "EXIT GAME"):
exit()
if (Command == 'END GAME'):
return False
if (Command == 'PICK' and self.game_map.is_ok_pick()):
self.game_map.is_picked = True
self.game_map.done_pos[self.game_map.cur_x, self.game_map.cur_y] = True
self.game_map.n_pos_need_mask = self.game_map.n_pos_need_mask - 1
save_is_picked = self.game_map.is_picked
self.game_map.move_point(Command)
self.game_map.print(pygame, screen)
FINISH_GAME = self.get_statement(save_is_picked)
if FINISH_GAME != "NUL":
if FINISH_GAME == "YOU LOST": print_on_game(FINISH_GAME, 10, 10, red)
if FINISH_GAME == "YOU WIN": print_on_game(FINISH_GAME, 10, 10, green)
if (FINISH_GAME == "YOU LOST"):
return True
sleep(2.0)
return False
pygame.display.update()
# self.clock.tick(self.fps)
NUM_POINT_GAME = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
TEXT_NUM_POINT_GAME = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve"]
clear_screen()
print_on_game("TRAIN HOW TO PLAY?(Y/N)", 10, 10, white)
train = False
while True:
T = Get_Control()
if T == "YES":
train = True
break
elif T == "NO":
break
elif T == "EXIT GAME":
exit()
if train:
clear_screen()
print_on_game("* space", 10, 10, green)
print_on_game(": to choose", 100, 10, white)
print_on_game("* arrow", 10, 40, green)
print_on_game(": to move", 100, 40, white)
print_on_game("* ESC", 10, 70, green)
print_on_game(": to escape game", 100, 70, white)
print_on_game("cannot undo step", 10, 110, red)
print_on_game("your target:", 10, 150, red)
print_on_game("fill every cell in map.", 150, 150, white)
print_on_game("by linking points of same color", 10, 180, white)
print_on_game("press space to start game :D", 10, 300, blue)
while True:
T = Get_Control()
if T == "EXIT GAME":
exit()
if T == "PICK":
break
while True:
clear_screen()
print_on_game("CHOSE YOUR 'BRAIN':", 10, 10, green)
pos = [0, 0]
start_game = True
while True:
dir = Get_Control()
if (dir == "EXIT GAME"):
exit()
if (dir == "END GAME" or dir == "PICK"):
if (dir == "END GAME"):
start_game = False
break
if (dir == "LEFT"):
pos[0] -= 1
if (dir == "RIGHT"):
pos[0] += 1
if (dir == "UP"):
pos[1] -= 1
if (dir == "DOWN"):
pos[1] += 1
if (pos[0] < 0): pos[0] += 1
if (pos[1] < 0): pos[1] += 1
if (pos[0] >= 3): pos[0] -= 1
if (pos[1] >= 4): pos[1] -= 1
for i in range(3):
for j in range(4):
color = white
if (pos == [i, j]): color = red
print_on_game(TEXT_NUM_POINT_GAME[NUM_POINT_GAME[i][j]], 50 + i * 100, 100 + j * 100, color)
if (start_game):
game = Link_points(NUM_POINT_GAME[pos[0]][pos[1]])
while True:
T = game.start_game()
again = False
if (T == True):
while True:
print_on_game("REPLAY? (Y/N)", 130, 410, white)
T = Get_Control()
if T == "YES":
again = True
break
elif T == "NO":
break
elif T == "EXIT GAME":
exit()
if (again):
# print(game_backup.game_map)
game.roll_back()
else: break
clear_screen()
print_on_game("PRESS ESC TO EXIT", 10, 10, white)
print_on_game("R TO REGAME", 10, 40, white)
re_game = Get_Control()
while True:
if (re_game == "RE_GAME" or re_game == "END GAME" or re_game == "EXIT GAME"):
break
re_game = Get_Control()
if (re_game == "RE_GAME"):
continue
break