-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassy_memo.py
More file actions
90 lines (76 loc) · 2.8 KB
/
classy_memo.py
File metadata and controls
90 lines (76 loc) · 2.8 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
#! /usr/bin/env python3
import time
import random
import pygame
from pygame.locals import QUIT, MOUSEBUTTONDOWN
class Monster():
def __init__(self, face):
self.face = face
self.found = False
class Game():
def __init__(self, screen):
self.blank = pygame.image.load("img/blank.png")
self.bgimg = pygame.image.load("img/background.jpg")
self.screen = screen
self.monsters = []
self.m2check = set()
self.running = True
self.over = False
self.warmup = True
self.hold = time.time()
self.populate()
def draw_background(self):
self.screen.blit(self.bgimg, (0, 0))
def update_monsters(self):
for m in self.monsters:
if self.over or m in self.m2check:
rx = random.randint(-1, 1)
ry = random.randint(-1, 1)
self.screen.blit(m.face, (m.x + rx, m.y + ry))
elif self.warmup or m.found:
self.screen.blit(m.face, (m.x, m.y))
else:
self.screen.blit(self.blank, (m.x, m.y))
def populate(self):
for n in range(6 * 6 // 2):
face = pygame.image.load(f"img/{n}.png")
self.monsters.append(Monster(face))
self.monsters.append(Monster(face))
random.shuffle(self.monsters)
for n, m in enumerate(self.monsters):
m.x = (n % 6) * 98 + 6
m.y = (n // 6) * 98 + 6
m.r = pygame.Rect(m.x + 18, m.y + 18, 60, 60)
def handle_events(self):
for event in pygame.event.get():
if event.type == QUIT:
self.running = False
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1:
if not self.warmup and len(self.m2check) < 2:
x, y = pygame.mouse.get_pos()
for m in self.monsters:
if not m.found or m not in self.m2check:
if m.r.collidepoint(x, y):
self.m2check.add(m)
self.hold = time.time()
def handle_state(self):
if self.warmup:
if time.time() - self.hold > 9:
self.warmup = False
if len(self.m2check) > 1 and time.time() - self.hold > 0.7:
m1, m2 = self.m2check.pop(), self.m2check.pop()
if m1.face == m2.face:
m1.found = m2.found = True
if all([m.found for m in self.monsters]):
self.over = True
pygame.init()
clock = pygame.time.Clock()
game = Game(pygame.display.set_mode((600, 600)))
while game.running:
clock.tick(50)
pygame.display.flip()
game.draw_background()
game.update_monsters()
game.handle_events()
game.handle_state()