Last active
December 17, 2015 18:39
-
-
Save csaez/5655129 to your computer and use it in GitHub Desktop.
Interactive Programming in Python - Mini-project #5 - "Memory"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mini-project #5 - implementation of card game - "Memory" | |
# | |
# 'Introduction to Interactive Programming in Python' Course | |
# RICE University - coursera.org | |
# by Joe Warren, John Greiner, Stephen Wong, Scott Rixner | |
import simplegui | |
import random | |
# HELPER FUNCTIONS | |
def init(): | |
global WIDTH, HEIGHT, number_cards, cards, flipped, game_state, moves | |
global image_atlas, image_coords | |
WIDTH = 150 * 4 | |
HEIGHT = 150 * 4 | |
number_cards = 16 # 4x4=16 | |
cards = list() | |
for i in range(number_cards // 2): | |
for j in range(2): | |
card = Card() | |
card.value = i | |
cards.append(card) | |
random.shuffle(cards) # unsort card list | |
flipped = list() # list of flipped cards | |
game_state = False | |
moves = 0 # number of moves | |
image_atlas = simplegui.load_image("http://goo.gl/0Ea0R") # card image | |
image_coords = {"0": ((100, 100), (200, 200)), | |
"1": ((300, 100), (200, 200)), | |
"2": ((500, 100), (200, 200)), | |
"3": ((100, 300), (200, 200)), | |
"4": ((300, 300), (200, 200)), | |
"5": ((500, 300), (200, 200)), | |
"6": ((100, 500), (200, 200)), | |
"7": ((300, 500), (200, 200)), | |
"8": ((500, 500), (200, 200))} | |
# CLASSES | |
class Card(): | |
def __init__(self): | |
self.value = None | |
self.flipped = False | |
self.size = [150, 150] | |
self.pos = [0, 0] | |
def draw(self, canvas): | |
if self.flipped: | |
# draw face | |
data = image_coords.get(str(self.value)) | |
canvas.draw_image(image_atlas, data[0], data[1], | |
self.pos, self.size) | |
else: | |
# draw backface | |
data = image_coords.get("8") | |
canvas.draw_image(image_atlas, data[0], data[1], | |
self.pos, self.size) | |
def clicked(self, point): | |
require = [ | |
self.pos[0] - self.size[0] // 2 < point[ | |
0] < self.pos[0] + self.size[0] // 2, | |
self.pos[1] - self.size[1] // 2 < point[1] < self.pos[1] + self.size[1] // 2] | |
return all(require) | |
# GLOBAL VARIABLES | |
init() | |
# EVENT HANDLERS | |
def reset(): | |
label.set_text("Moves = 0") | |
init() | |
def mouseclick(pos): | |
# add game state logic here | |
global flipped, game_state, moves | |
label.set_text("Moves = " + str(moves)) | |
for card in cards: | |
card.flipped = card in flipped | |
for card in cards: | |
clicked = card.clicked(pos) | |
if not clicked or card.flipped: | |
continue | |
card.flipped = True | |
if not game_state: | |
flipped.append(card) | |
moves += 1 | |
else: | |
if card.value == flipped[-1].value: | |
flipped.append(card) | |
else: | |
flipped.pop() | |
game_state = not game_state | |
def draw(canvas): | |
canvas.draw_text("Loading...", ( | |
WIDTH // 3, HEIGHT // 2), 44, "White", "sans-serif") | |
index = 0 | |
for x in range(4): | |
for y in range(4): | |
card = cards[index] | |
card.pos[0] = (card.size[0] * x) + card.size[0] // 2 | |
card.pos[1] = (card.size[1] * y) + card.size[1] // 2 | |
card.draw(canvas) | |
index += 1 | |
# CREATE FRAME AND ADD A BUTTON AND LABELS | |
frame = simplegui.create_frame("Memory", WIDTH, HEIGHT) | |
frame.set_canvas_background("Silver") | |
frame.add_button("Restart", reset, 150) | |
label = frame.add_label("Moves = 0") | |
# REGISTER EVENT HANDLERS | |
frame.set_mouseclick_handler(mouseclick) | |
frame.set_draw_handler(draw) | |
# GET THINGS ROLLING | |
frame.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment