-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
293 lines (235 loc) · 10.6 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import json
import sys
import pygame
from pygame.locals import *
from game import playGame
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
class Button():
"""
Class to create a new button in pygame window.
"""
# initialise the button
def __init__(self, colour, x, y, width, height, text=""):
self.colour = colour
self.x = x
self.y = y
self.width = width
self.height = height
self.text = text
# draw the button on the screen
def draw(self, win, text_col, font):
drawRoundRect(win, self.colour, (self.x, self.y,
self.width, self.height))
if self.text != "":
text = font.render(self.text, 1, text_col)
win.blit(text, (self.x + (self.width/2 - text.get_width()/2),
self.y + (self.height/2 - text.get_height()/2)))
# check if the mouse is positioned over the button
def isOver(self, pos):
# pos is the mouse position or a tuple of (x,y) coordinates
if pos[0] > self.x and pos[0] < self.x + self.width:
if pos[1] > self.y and pos[1] < self.y + self.height:
return True
return False
def drawRoundRect(surface, colour, rect, radius=0.4):
"""
Draw an antialiased rounded filled rectangle on screen
Parameters:
surface (pygame.Surface): destination
colour (tuple): RGB values for rectangle fill colour
radius (float): 0 <= radius <= 1
"""
rect = Rect(rect)
colour = Color(*colour)
alpha = colour.a
colour.a = 0
pos = rect.topleft
rect.topleft = 0, 0
rectangle = pygame.Surface(rect.size, SRCALPHA)
circle = pygame.Surface([min(rect.size) * 3] * 2, SRCALPHA)
pygame.draw.ellipse(circle, BLACK, circle.get_rect(), 0)
circle = pygame.transform.smoothscale(
circle, [int(min(rect.size)*radius)]*2)
radius = rectangle.blit(circle, (0, 0))
radius.bottomright = rect.bottomright
rectangle.blit(circle, radius)
radius.topright = rect.topright
rectangle.blit(circle, radius)
radius.bottomleft = rect.bottomleft
rectangle.blit(circle, radius)
rectangle.fill(BLACK, rect.inflate(-radius.w, 0))
rectangle.fill(BLACK, rect.inflate(0, -radius.h))
rectangle.fill(colour, special_flags=BLEND_RGBA_MAX)
rectangle.fill((255, 255, 255, alpha), special_flags=BLEND_RGBA_MIN)
surface.blit(rectangle, pos)
def showMenu():
"""
Display the start screen
"""
# create light theme button
light_theme = Button(
tuple(c["colour"]["light"]["2048"]), 200-70, 275, 45, 45, "light")
# create dark theme button
dark_theme = Button(
tuple(c["colour"]["dark"]["2048"]), 270-70, 275, 45, 45, "dark")
# initialise theme
theme = ""
theme_selected = False
# create difficulty buttons
_2048 = Button(tuple(c["colour"]["light"]["64"]),
130, 330, 45, 45, "2048")
_1024 = Button(tuple(c["colour"]["light"]["2048"]),
200, 330, 45, 45, "1024")
_512 = Button(tuple(c["colour"]["light"]["2048"]),
270, 330, 45, 45, "512")
_256 = Button(tuple(c["colour"]["light"]["2048"]),
340, 330, 45, 45, "256")
# default difficulty
difficulty = 0
diff_selected = False
# create play button
play = Button(tuple(c["colour"]["light"]["2048"]),
235, 400, 45, 45, "play")
# pygame loop for start screen
while True:
screen.fill(BLACK)
screen.blit(pygame.transform.scale(
pygame.image.load("images/icon.ico"), (200, 200)), (155, 50))
font = pygame.font.SysFont(c["font"], 15, bold=True)
theme_text = font.render("Theme: ", 1, WHITE)
screen.blit(theme_text, (55, 285))
diff_text = font.render("Difficulty: ", 1, WHITE)
screen.blit(diff_text, (40, 345))
# set fonts for buttons
font1 = pygame.font.SysFont(c["font"], 15, bold=True)
font2 = pygame.font.SysFont(c["font"], 14, bold=True)
# draw all buttons on the screen
light_theme.draw(screen, BLACK, font1)
dark_theme.draw(screen, (197, 255, 215), font1)
_2048.draw(screen, BLACK, font2)
_1024.draw(screen, BLACK, font2)
_512.draw(screen, BLACK, font2)
_256.draw(screen, BLACK, font2)
play.draw(screen, BLACK, font1)
pygame.display.update()
for event in pygame.event.get():
# store mouse position (coordinates)
pos = pygame.mouse.get_pos()
if event.type == QUIT or \
(event.type == pygame.KEYDOWN and event.key == K_q):
# exit if q is pressed
pygame.quit()
sys.exit()
# check if a button is clicked
if event.type == pygame.MOUSEBUTTONDOWN:
# select light theme
if light_theme.isOver(pos):
dark_theme.colour = tuple(c["colour"]["dark"]["2048"])
light_theme.colour = tuple(c["colour"]["light"]["64"])
theme = "light"
theme_selected = True
# select dark theme
if dark_theme.isOver(pos):
dark_theme.colour = tuple(c["colour"]["dark"]["background"])
light_theme.colour = tuple(c["colour"]["light"]["2048"])
theme = "dark"
theme_selected = True
if _2048.isOver(pos):
_2048.colour = tuple(c["colour"]["light"]["64"])
_1024.colour = tuple(c["colour"]["light"]["2048"])
_512.colour = tuple(c["colour"]["light"]["2048"])
_256.colour = tuple(c["colour"]["light"]["2048"])
difficulty = 2048
diff_selected = True
if _1024.isOver(pos):
_1024.colour = tuple(c["colour"]["light"]["64"])
_2048.colour = tuple(c["colour"]["light"]["2048"])
_512.colour = tuple(c["colour"]["light"]["2048"])
_256.colour = tuple(c["colour"]["light"]["2048"])
difficulty = 1024
diff_selected = True
if _512.isOver(pos):
_512.colour = tuple(c["colour"]["light"]["64"])
_1024.colour = tuple(c["colour"]["light"]["2048"])
_2048.colour = tuple(c["colour"]["light"]["2048"])
_256.colour = tuple(c["colour"]["light"]["2048"])
difficulty = 512
diff_selected = True
if _256.isOver(pos):
_256.colour = tuple(c["colour"]["light"]["64"])
_1024.colour = tuple(c["colour"]["light"]["2048"])
_512.colour = tuple(c["colour"]["light"]["2048"])
_2048.colour = tuple(c["colour"]["light"]["2048"])
difficulty = 256
diff_selected = True
# play game with selected theme
if play.isOver(pos):
if theme != "" and difficulty != 0:
playGame(theme, difficulty)
# reset theme & diff choice if area outside buttons is clicked
if not play.isOver(pos) and \
not dark_theme.isOver(pos) and \
not light_theme.isOver(pos) and \
not _2048.isOver(pos) and \
not _1024.isOver(pos) and \
not _512.isOver(pos) and \
not _256.isOver(pos):
theme = ""
theme_selected = False
diff_selected = False
light_theme.colour = tuple(c["colour"]["light"]["2048"])
dark_theme.colour = tuple(c["colour"]["dark"]["2048"])
_2048.colour = tuple(c["colour"]["light"]["2048"])
_1024.colour = tuple(c["colour"]["light"]["2048"])
_512.colour = tuple(c["colour"]["light"]["2048"])
_256.colour = tuple(c["colour"]["light"]["2048"])
# change colour on hovering over buttons
if event.type == pygame.MOUSEMOTION:
if not theme_selected:
if light_theme.isOver(pos):
light_theme.colour = tuple(c["colour"]["light"]["64"])
else:
light_theme.colour = tuple(c["colour"]["light"]["2048"])
if dark_theme.isOver(pos):
dark_theme.colour = tuple(c["colour"]["dark"]["background"])
else:
dark_theme.colour = tuple(c["colour"]["dark"]["2048"])
if not diff_selected:
if _2048.isOver(pos):
_2048.colour = tuple(c["colour"]["light"]["64"])
else:
_2048.colour = tuple(c["colour"]["light"]["2048"])
if _1024.isOver(pos):
_1024.colour = tuple(c["colour"]["light"]["64"])
else:
_1024.colour = tuple(c["colour"]["light"]["2048"])
if _512.isOver(pos):
_512.colour = tuple(c["colour"]["light"]["64"])
else:
_512.colour = tuple(c["colour"]["light"]["2048"])
if _256.isOver(pos):
_256.colour = tuple(c["colour"]["light"]["64"])
else:
_256.colour = tuple(c["colour"]["light"]["2048"])
if play.isOver(pos):
play.colour = tuple(c["colour"]["light"]["64"])
else:
play.colour = tuple(c["colour"]["light"]["2048"])
if __name__ == "__main__":
# load json data
c = json.load(open("constants.json", "r"))
# set up pygame
pygame.init()
# set up screen
screen = pygame.display.set_mode(
(c["size"], c["size"]))
pygame.display.set_caption("2048 by Rajit Banerjee")
# display game icon in window
icon = pygame.transform.scale(
pygame.image.load("images/icon.ico"), (32, 32))
pygame.display.set_icon(icon)
# set font according to json data specifications
my_font = pygame.font.SysFont(c["font"], c["font_size"], bold=True)
# display the start screen
showMenu()