|
| 1 | +# implementation of card game - Memory |
| 2 | + |
| 3 | +import simplegui |
| 4 | +import random |
| 5 | + |
| 6 | +# -------------------------------------------------------- |
| 7 | +# -------------------------------------------------------- |
| 8 | + |
| 9 | +# global variables |
| 10 | + |
| 11 | +deck = [] |
| 12 | +exposed = [] |
| 13 | + |
| 14 | +card1 = 0 |
| 15 | +card2 = 0 |
| 16 | + |
| 17 | +state = 0 |
| 18 | +turns = 0 |
| 19 | + |
| 20 | +# -------------------------------------------------------- |
| 21 | +# -------------------------------------------------------- |
| 22 | + |
| 23 | +# helper function to initialize globals |
| 24 | +def new_game(): |
| 25 | + global deck, state, exposed, card1, card2, turns |
| 26 | + |
| 27 | + deck = range(8) + range(8) |
| 28 | + random.shuffle(deck) |
| 29 | + |
| 30 | + exposed = [False for x in deck] |
| 31 | + |
| 32 | + state = turns = card1 = card2 = 0 |
| 33 | + |
| 34 | + |
| 35 | +# -------------------------------------------------------- |
| 36 | +# -------------------------------------------------------- |
| 37 | + |
| 38 | + |
| 39 | +# define event handlers |
| 40 | +def mouseclick(pos): |
| 41 | + |
| 42 | + card_num = pos[0] // 50 |
| 43 | + |
| 44 | + if (exposed[card_num] != True): |
| 45 | + exposed[card_num] = True |
| 46 | + |
| 47 | + global card1, card2, state, turns |
| 48 | + |
| 49 | + if state == 0: |
| 50 | + state = 1 |
| 51 | + card1 = card_num |
| 52 | + |
| 53 | + elif state == 1: |
| 54 | + state = 2 |
| 55 | + card2 = card_num |
| 56 | + turns += 1 |
| 57 | + |
| 58 | + else: |
| 59 | + state = 1 |
| 60 | + |
| 61 | + if (deck[card1] != deck[card2]): |
| 62 | + exposed[card1] = exposed[card2] = False |
| 63 | + |
| 64 | + card1 = card_num |
| 65 | + |
| 66 | +# -------------------------------------------------------- |
| 67 | + |
| 68 | +# cards are logically 50x100 pixels in size |
| 69 | +def draw(canvas): |
| 70 | + x = 0 |
| 71 | + i = 0 |
| 72 | + for num in deck: |
| 73 | + if exposed[i] == False: |
| 74 | + canvas.draw_line((x, 0), (x, 100), 2, 'yellow') |
| 75 | + canvas.draw_line((x, 0), (x+50, 0), 200, 'green') |
| 76 | + else: |
| 77 | + canvas.draw_text(str(num), [x , 90], 115, "red") |
| 78 | + x += 50 |
| 79 | + i += 1 |
| 80 | + |
| 81 | + label.set_text("Turns = " + str(turns)) |
| 82 | + |
| 83 | + |
| 84 | +# -------------------------------------------------------- |
| 85 | +# -------------------------------------------------------- |
| 86 | + |
| 87 | + |
| 88 | +# create frame and add a button and labels |
| 89 | +frame = simplegui.create_frame("Memory", 800, 100) |
| 90 | +frame.add_button("Reset", new_game) |
| 91 | +label = frame.add_label("Turns = " + str(turns)) |
| 92 | + |
| 93 | +# -------------------------------------------------------- |
| 94 | + |
| 95 | +# register event handlers |
| 96 | +frame.set_mouseclick_handler(mouseclick) |
| 97 | +frame.set_draw_handler(draw) |
| 98 | +# -------------------------------------------------------- |
| 99 | + |
| 100 | +# get things rolling |
| 101 | +new_game() |
| 102 | +frame.start() |
| 103 | +# -------------------------------------------------------- |
| 104 | +# -------------------------------------------------------- |
0 commit comments