-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path.gitattributes
More file actions
259 lines (224 loc) · 10.3 KB
/
Copy path.gitattributes
File metadata and controls
259 lines (224 loc) · 10.3 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
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
import pygame, sys, random, time
pygame.init()
# ---------------- Settings ----------------
screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
WIDTH, HEIGHT = screen.get_size()
pygame.display.set_caption("✨ Memory Puzzle Game ✨")
clock = pygame.time.Clock()
FPS = 60
# Fonts (emoji friendly)
def get_font(size):
return pygame.font.SysFont("segoeuiemoji", size) or pygame.font.SysFont(None, size)
TITLE_FONT = get_font(84)
SUBTITLE_FONT = get_font(44)
BTN_FONT = get_font(36)
HUD_FONT = get_font(28)
SMALL_FONT = get_font(22)
# Colors
BG_TOP = (80, 40, 120)
BG_BOTTOM = (30, 100, 180)
BTN_BASE = (70, 90, 160)
BTN_HOVER = (120, 160, 220)
CARD_BACK = (50, 70, 140)
CARD_FACE = (255, 255, 255)
OUTLINE = (220, 220, 230)
HUD_TEXT = (245, 245, 245)
# Themes with emojis
THEMES = {
"🏎Cars": ["🚗","🚙","🚕","🚓","🚑","🚒","🚐","🚌","🚎","🚚","🚛","🏎️","🚔","🚖"],
"🍉Fruits": ["🍎","🍌","🍇","🍉","🍊","🍍","🥭","🍓","🍒","🥝","🍐","🍋","🍈","🥥"],
"🍴Foods": ["🍔","🍕","🍟","🌭","🍿","🥪","🍣","🍱","🍩","🍪","🍰","🍫","🍝","🍜"],
"📘Books": ["📕","📗","📘","📙","📓","📔","📒","📚","📖","📑","🔖","📎","🧾","📌"]
}
# ---------------- Utility ----------------
def draw_vertical_gradient(surface, top_color, bottom_color):
h = surface.get_height()
w = surface.get_width()
for y in range(h):
ratio = y / max(h-1,1)
r = int(top_color[0]*(1-ratio) + bottom_color[0]*ratio)
g = int(top_color[1]*(1-ratio) + bottom_color[1]*ratio)
b = int(top_color[2]*(1-ratio) + bottom_color[2]*ratio)
pygame.draw.line(surface, (r,g,b), (0,y), (w,y))
def center_text(surface, text, font, color, y):
surf = font.render(text, True, color)
rect = surf.get_rect(center=(WIDTH//2, y))
surface.blit(surf, rect)
return rect
# ---------------- Menus ----------------
def first_choice_screen():
options = [("👤 Single Player", 0), ("👥 Multiplayer", 1)]
while True:
draw_vertical_gradient(screen, BG_TOP, BG_BOTTOM)
center_text(screen, "Memory Puzzle", TITLE_FONT, (255,255,255), HEIGHT*0.2)
mouse = pygame.mouse.get_pos()
click = False
for e in pygame.event.get():
if e.type==pygame.QUIT: pygame.quit(); sys.exit()
if e.type==pygame.KEYDOWN and e.key==pygame.K_ESCAPE: pygame.quit(); sys.exit()
if e.type==pygame.MOUSEBUTTONDOWN and e.button==1: click=True
tile_w, tile_h = 400, 140
gap = 60
start_x = (WIDTH - (tile_w*2+gap))//2
y = int(HEIGHT*0.45)
for i,(label,val) in enumerate(options):
rect = pygame.Rect(start_x+i*(tile_w+gap), y, tile_w, tile_h)
hover = rect.collidepoint(mouse)
pygame.draw.rect(screen, BTN_HOVER if hover else BTN_BASE, rect, border_radius=18)
pygame.draw.rect(screen, OUTLINE, rect, 2, border_radius=18)
t = BTN_FONT.render(label, True, (255,255,255))
screen.blit(t, t.get_rect(center=rect.center))
if hover and click: return val
pygame.display.flip(); clock.tick(FPS)
def theme_choice_screen():
keys = list(THEMES.keys())
while True:
draw_vertical_gradient(screen, (120,30,150), (50,140,200))
center_text(screen, "Choose Theme", TITLE_FONT, (255,255,255), HEIGHT*0.2)
mouse = pygame.mouse.get_pos()
click=False
for e in pygame.event.get():
if e.type==pygame.QUIT: pygame.quit(); sys.exit()
if e.type==pygame.KEYDOWN and e.key==pygame.K_ESCAPE: return None
if e.type==pygame.MOUSEBUTTONDOWN and e.button==1: click=True
tile_w,tile_h=250,100; gap=40
total=len(keys)*tile_w+(len(keys)-1)*gap
start_x=(WIDTH-total)//2; y=int(HEIGHT*0.45)
for i,k in enumerate(keys):
rect=pygame.Rect(start_x+i*(tile_w+gap), y, tile_w, tile_h)
hover=rect.collidepoint(mouse)
pygame.draw.rect(screen, BTN_HOVER if hover else BTN_BASE, rect, border_radius=14)
pygame.draw.rect(screen, OUTLINE, rect, 2, border_radius=14)
screen.blit(BTN_FONT.render(k,True,(255,255,255)), rect.move(70,25))
if hover and click: return k
pygame.display.flip(); clock.tick(FPS)
def grid_choice_screen(theme):
options=[("4×4",4),("6×6",6),("8×8",8)]
while True:
draw_vertical_gradient(screen,(20,120,150),(40,180,190))
center_text(screen, f"Theme: {theme}", SUBTITLE_FONT, (255,255,255), HEIGHT*0.2)
center_text(screen, "Choose Grid Size", TITLE_FONT, (255,255,255), HEIGHT*0.3)
mouse=pygame.mouse.get_pos(); click=False
for e in pygame.event.get():
if e.type==pygame.QUIT: pygame.quit(); sys.exit()
if e.type==pygame.KEYDOWN and e.key==pygame.K_ESCAPE: return None
if e.type==pygame.MOUSEBUTTONDOWN and e.button==1: click=True
btn_w,btn_h=250,90; gap=60
total=len(options)*btn_w+(len(options)-1)*gap
start_x=(WIDTH-total)//2; y=int(HEIGHT*0.5)
for i,(label,size) in enumerate(options):
rect=pygame.Rect(start_x+i*(btn_w+gap), y, btn_w, btn_h)
hover=rect.collidepoint(mouse)
pygame.draw.rect(screen, BTN_HOVER if hover else BTN_BASE, rect, border_radius=16)
pygame.draw.rect(screen, OUTLINE, rect, 2, border_radius=16)
screen.blit(BTN_FONT.render(label,True,(255,255,255)), rect.move(80,25))
if hover and click: return (size,size)
pygame.display.flip(); clock.tick(FPS)
# ---------------- Board ----------------
def create_board(rows, cols, theme):
num_pairs=(rows*cols)//2
deck=(THEMES[theme]*((num_pairs//len(THEMES[theme]))+1))[:num_pairs]
deck=deck*2; random.shuffle(deck)
pad=10
max_w=(WIDTH-200)//cols
max_h=(HEIGHT-300)//rows
size=min(max_w,max_h)
start_x=(WIDTH-(cols*size+(cols-1)*pad))//2
start_y=(HEIGHT-(rows*size+(rows-1)*pad))//2
cards=[]
for r in range(rows):
row=[]
for c in range(cols):
x=start_x+c*(size+pad)
y=start_y+r*(size+pad)
row.append({"rect":pygame.Rect(x,y,size,size),"emoji":deck.pop(),"revealed":False,"matched":False})
cards.append(row)
font=get_font(int(size*0.6))
return cards,font
# ---------------- Game ----------------
def play_game(mode, rows, cols, theme):
cards,font=create_board(rows,cols,theme)
first=second=None; matches=0; moves=0; players=[0,0]; turn=0
start_time=time.time()
# bottom control buttons
btn_w,btn_h=200,60; gap=40
total=btn_w*3+gap*2; start_x=(WIDTH-total)//2
y=HEIGHT-80
prev_rect=pygame.Rect(start_x,y,btn_w,btn_h)
menu_rect=pygame.Rect(start_x+btn_w+gap,y,btn_w,btn_h)
restart_rect=pygame.Rect(start_x+(btn_w+gap)*2,y,btn_w,btn_h)
while True:
draw_vertical_gradient(screen,(90,30,140),(20,120,190))
elapsed=int(time.time()-start_time)
mins,secs=divmod(elapsed,60)
timer=f"{mins:02}:{secs:02}"
# HUD
if mode==0:
hud=f"Pairs: {matches}/{(rows*cols)//2} Moves: {moves} Time: {timer}"
else:
hud=f"P1:{players[0]} | P2:{players[1]} | Turn:P{turn+1} | Time:{timer}"
screen.blit(HUD_FONT.render(hud,True,HUD_TEXT),(40,40))
# Draw cards
for row in cards:
for card in row:
rect=card["rect"]
pygame.draw.rect(screen,CARD_FACE if card["revealed"] or card["matched"] else CARD_BACK,rect,border_radius=12)
pygame.draw.rect(screen,OUTLINE,rect,2,border_radius=12)
if card["revealed"] or card["matched"]:
t=font.render(card["emoji"],True,(0,0,0))
screen.blit(t,t.get_rect(center=rect.center))
# Controls
mouse=pygame.mouse.get_pos()
for (rect,label) in [(prev_rect,"⬅ Prev"),(menu_rect,"🏠 Menu"),(restart_rect,"🔄 Restart")]:
hover=rect.collidepoint(mouse)
pygame.draw.rect(screen,BTN_HOVER if hover else BTN_BASE,rect,border_radius=12)
pygame.draw.rect(screen,OUTLINE,rect,2,border_radius=12)
txt=SMALL_FONT.render(label,True,(255,255,255))
screen.blit(txt,txt.get_rect(center=rect.center))
click=False
for e in pygame.event.get():
if e.type==pygame.QUIT: pygame.quit(); sys.exit()
if e.type==pygame.KEYDOWN and e.key==pygame.K_ESCAPE: return "menu"
if e.type==pygame.MOUSEBUTTONDOWN and e.button==1: click=True; mpos=e.pos
if click:
if prev_rect.collidepoint(mpos): return "prev"
if menu_rect.collidepoint(mpos): return "menu"
if restart_rect.collidepoint(mpos): return "restart"
for r,row in enumerate(cards):
for c,card in enumerate(row):
if card["rect"].collidepoint(mpos) and not card["revealed"] and not card["matched"]:
card["revealed"]=True
if first is None: first=(r,c)
elif second is None: second=(r,c); moves+=1
break
if first and second:
a=cards[first[0]][first[1]]; b=cards[second[0]][second[1]]
if a["emoji"]==b["emoji"]:
a["matched"]=b["matched"]=True; matches+=1
if mode==1: players[turn]+=1
else:
pygame.display.flip(); pygame.time.wait(800)
a["revealed"]=b["revealed"]=False
if mode==1: turn=1-turn
first=second=None
if matches==(rows*cols)//2:
center_text(screen,"🎉 You Win!",TITLE_FONT,(255,255,0),HEIGHT//2)
pygame.display.flip(); pygame.time.wait(2000)
return "menu"
pygame.display.flip(); clock.tick(FPS)
# ---------------- Main Flow ----------------
def main():
while True:
mode=first_choice_screen()
theme=theme_choice_screen()
if theme is None: continue
while True:
grid=grid_choice_screen(theme)
if grid is None: break
rows,cols=grid
action=play_game(mode,rows,cols,theme)
if action=="prev": continue # back to grid selection
elif action=="menu": break
elif action=="restart": play_game(mode,rows,cols,theme)
if __name__=="__main__":
main()