2022-08-01 11:57:14 -05:00
|
|
|
from audio import Audio
|
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
try:
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import time
|
|
|
|
import turtle
|
|
|
|
|
|
|
|
from card import Card
|
|
|
|
|
|
|
|
# CONSTANTS
|
|
|
|
WIDTH = 1600
|
|
|
|
HEIGHT = 840
|
|
|
|
BACKGROUND_COLOR = (66, 135, 245)
|
|
|
|
|
|
|
|
screen = turtle.Screen()
|
|
|
|
turtle.bgcolor('#46a38d')
|
|
|
|
screen.setup(WIDTH, HEIGHT)
|
2022-08-01 12:35:59 -05:00
|
|
|
screen.title('Turtle Match - Benjamin Zimmerman')
|
2022-08-01 00:06:36 -05:00
|
|
|
|
2022-08-01 11:57:14 -05:00
|
|
|
Audio.start_audio()
|
|
|
|
Audio.play_background_music()
|
|
|
|
|
|
|
|
# Scoreboard
|
|
|
|
score_text = turtle.Turtle()
|
2022-08-01 00:06:36 -05:00
|
|
|
score_text.hideturtle()
|
|
|
|
score_text.penup()
|
|
|
|
score_text.goto((WIDTH - HEIGHT) // 2, HEIGHT * .25)
|
|
|
|
score_text.write('Score: 0', font=('Arial', 24, 'bold'))
|
2022-07-31 23:14:09 -05:00
|
|
|
|
|
|
|
|
|
|
|
def coord_translation(x, y):
|
|
|
|
"""
|
|
|
|
Translates coordinates from the screen to turtle.
|
|
|
|
:param x: x coordinate
|
|
|
|
:param y: y coordinate
|
|
|
|
:return: Translated coordinates
|
|
|
|
"""
|
|
|
|
return x - (WIDTH / 2), y - (HEIGHT / 2)
|
|
|
|
|
|
|
|
|
|
|
|
image_files = os.listdir('images')
|
2022-08-01 00:06:36 -05:00
|
|
|
|
|
|
|
# Creates list of images, doubles it, and shuffles it
|
2022-07-31 23:14:09 -05:00
|
|
|
image_files.remove('turtle.png')
|
|
|
|
image_files.extend(image_files)
|
|
|
|
random.shuffle(image_files)
|
|
|
|
|
2022-08-01 00:06:36 -05:00
|
|
|
cards = []
|
|
|
|
for file in image_files:
|
|
|
|
path = f'images/{file}'
|
|
|
|
cards.append(Card(path))
|
2022-07-31 23:14:09 -05:00
|
|
|
|
|
|
|
# Move sprites
|
|
|
|
for i in range(16):
|
|
|
|
# Note: Coordinates start from (0, 0) at top left of screen.
|
|
|
|
# Arrange cards in a grid, 4x4, with a margin of 20 pixels between each card.
|
|
|
|
# Array from left to right, then top to bottom.
|
|
|
|
x, y = coord_translation((210 * (i % 4)) + 105, (210 * int(i / 4)) + 105)
|
|
|
|
cards[i].goto(x, y)
|
|
|
|
|
2022-08-01 11:57:14 -05:00
|
|
|
score = 0
|
2022-08-01 16:29:45 -05:00
|
|
|
matches = 0
|
2022-08-01 11:57:14 -05:00
|
|
|
game_is_running = True
|
2022-08-01 16:29:45 -05:00
|
|
|
end_routine_done = False
|
2022-08-01 11:57:14 -05:00
|
|
|
clicked_cards = []
|
2022-07-31 23:14:09 -05:00
|
|
|
|
|
|
|
def clicked_card(x, y):
|
|
|
|
"""
|
|
|
|
:return: The card which was clicked
|
|
|
|
"""
|
2022-08-01 11:57:14 -05:00
|
|
|
Audio.click()
|
2022-08-01 00:06:36 -05:00
|
|
|
global clicked_cards
|
2022-07-27 22:19:04 -05:00
|
|
|
for card in cards:
|
2022-08-01 16:29:45 -05:00
|
|
|
if card.is_mouse_over(x, y) and card.shape()[:10] != 'card_front':
|
2022-08-01 00:06:36 -05:00
|
|
|
if len(clicked_cards) == 2:
|
|
|
|
clicked_cards = []
|
2022-07-31 23:14:09 -05:00
|
|
|
card.to_front()
|
2022-07-27 22:19:04 -05:00
|
|
|
clicked_cards.append(card)
|
2022-07-31 23:14:09 -05:00
|
|
|
|
|
|
|
|
2022-08-01 00:06:36 -05:00
|
|
|
def update_score():
|
|
|
|
score_text.clear()
|
|
|
|
score_text.write(f'Score: {score}', font=('Arial', 24, 'bold'))
|
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
|
2022-08-01 00:06:36 -05:00
|
|
|
screen.onclick(fun=clicked_card)
|
2022-07-31 23:14:09 -05:00
|
|
|
|
|
|
|
while game_is_running:
|
|
|
|
time.sleep(0.1)
|
2022-08-01 16:29:45 -05:00
|
|
|
if not end_routine_done:
|
|
|
|
if matches >= 8:
|
|
|
|
Audio.pause_music()
|
|
|
|
Audio.game_success()
|
|
|
|
time.sleep(3.1)
|
|
|
|
Audio.unpause_background_music()
|
|
|
|
end_routine_done = True
|
2022-07-31 23:14:09 -05:00
|
|
|
if len(clicked_cards) == 2:
|
2022-08-01 20:02:15 -05:00
|
|
|
if clicked_cards[0] == clicked_cards[1]:
|
|
|
|
score += 5
|
|
|
|
update_score()
|
|
|
|
Audio.match_made()
|
|
|
|
matches += 1
|
|
|
|
else:
|
2022-08-01 00:06:36 -05:00
|
|
|
time.sleep(1)
|
2022-07-31 23:14:09 -05:00
|
|
|
clicked_cards[0].to_back()
|
|
|
|
clicked_cards[1].to_back()
|
|
|
|
score -= 1
|
2022-08-01 00:06:36 -05:00
|
|
|
update_score()
|
2022-08-01 11:57:14 -05:00
|
|
|
Audio.no_match()
|
2022-07-31 23:14:09 -05:00
|
|
|
clicked_cards = []
|
|
|
|
screen.update()
|
|
|
|
|
|
|
|
screen.mainloop()
|
|
|
|
except turtle.Terminator:
|
|
|
|
exit(0)
|