File: memory.py

package info (click to toggle)
pgzero 1.2.post4%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 2,980 kB
  • sloc: python: 4,273; makefile: 166
file content (87 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (3)
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
#
# TODO: Add sound effects
#       Ignore clicks after displaying second card until
#       either hit or miss is reported.
#       Consider re-casting the data structure as a dict
#       with two-element tuple keys.
#       Configure window size according to COLS and ROWS
#

checkmark = Actor('checkmark')
steve = Actor('card_back', (50, 50))
steve.topleft = (0, 0)

import random
import time

COLS = 4
ROWS= 3
IMSIZE = 200
STATUS = []        # cells that have been clicked on
ignore = []        # cells that have been matches and are no longer in play

# Create two of each card image, then randomize before creating the board
START_IMAGES= [ "im"+str(i+1) for i in range(COLS*ROWS//2)]*2
random.shuffle(START_IMAGES)

STATUS=[]

board = []                    # initialize the board
for row in range(ROWS):
    new_row=[]
    for col in range(COLS):
        image_name = START_IMAGES.pop()
        temp=Actor(image_name, (col*IMSIZE, row*IMSIZE))
        temp.image_name = image_name # used to verify matches
        temp.topleft=(col*IMSIZE, row*IMSIZE)
        new_row.append(temp)
    board.append(new_row)

def draw():                    # draw the board when pygame-zero says to
    screen.clear()
    for row in range(ROWS):
        for col in range(COLS):
            if (row, col) in ignore:    # already matched
                checkmark.topleft = IMSIZE*col, IMSIZE*row
                checkmark.draw()
            elif (row, col) in STATUS:    # clicked this move: show face
                board[row][col].draw()
            else:                        # regular clickable card
                steve.topleft = IMSIZE*col, IMSIZE*row
                steve.draw()

def findTile(pos):
    y, x = pos
    result = x // IMSIZE , y // IMSIZE
    return result

def showTile():
    pass


def on_mouse_down(pos, button):
    if len(STATUS) == 2: # ignore until timeout redisplays
        return
    if pos in ignore: # has already been matched
        return
    if button == mouse.LEFT and (pos):
    # not sure why "and (pos)" - especially the parens!
        coords = findTile(pos)
        if coords not in STATUS:
            STATUS.append(coords) # now they are
            if len(STATUS) == 1:  # 1st click - turn not yet over
                pass
            elif len(STATUS) == 2: # 2nd click - check for match
                (x1, y1), (x2, y2) = STATUS # an "unpacking assignment"
                if board[x1][y1].image_name == board[x2][y2].image_name:
                    print("Success sound")
                    # add cards to list of non-clickable positions
                    for pos in STATUS:
                        ignore.append(pos)
                else:
                    print("Failure sound")
                clock.schedule_unique(next_turn, 2.0)

def next_turn():
    del STATUS[:]