Now has a scoreboard
This commit is contained in:
parent
de30a74c6d
commit
67e6a83952
2 changed files with 50 additions and 13 deletions
28
card.py
28
card.py
|
@ -3,24 +3,38 @@ from tkinter import PhotoImage
|
|||
|
||||
|
||||
class Card(turtle.Turtle):
|
||||
card_count = 0
|
||||
def __init__(self, image_path):
|
||||
"""
|
||||
Initializes Card object.
|
||||
:param image_path: Path to image
|
||||
:param image_path: Path to image for card_front
|
||||
"""
|
||||
super().__init__()
|
||||
|
||||
# self.size = 150 # desired image height and width (in pixels)
|
||||
self.image_path = image_path
|
||||
self.card_id = Card.card_count
|
||||
Card.card_count += 1
|
||||
self.penup()
|
||||
self.speed(8)
|
||||
self.smaller_back = PhotoImage(file='images/turtle.png').subsample(4, 4)
|
||||
turtle.addshape('card_back', turtle.Shape('image', self.smaller_back))
|
||||
self.smaller_front = PhotoImage(file=image_path).subsample(4, 4)
|
||||
turtle.addshape('card_front', turtle.Shape('image', self.smaller_front))
|
||||
self.back = PhotoImage(file='images/turtle.png').subsample(4, 4)
|
||||
turtle.addshape('card_back', turtle.Shape('image', self.back))
|
||||
self.front = PhotoImage(file=image_path).subsample(4, 4)
|
||||
turtle.addshape(f'card_front{self.card_id}', turtle.Shape('image', self.front))
|
||||
self.shape('card_back')
|
||||
|
||||
def __eq__(self, other):
|
||||
"""
|
||||
Checks if two cards have the same front image.
|
||||
:param other: Another Card object
|
||||
:return: True if cards have the same front image, False otherwise.
|
||||
"""
|
||||
if type(other) is not Card:
|
||||
raise TypeError('Can only compare Card objects to other Card objects.')
|
||||
else:
|
||||
return self.image_path == other.image_path
|
||||
|
||||
def to_front(self):
|
||||
self.shape('card_front')
|
||||
self.shape(f'card_front{self.card_id}')
|
||||
|
||||
def to_back(self):
|
||||
self.shape('card_back')
|
||||
|
|
35
main.py
35
main.py
|
@ -14,6 +14,12 @@ try:
|
|||
screen = turtle.Screen()
|
||||
turtle.bgcolor('#46a38d')
|
||||
screen.setup(WIDTH, HEIGHT)
|
||||
score_text = turtle.Turtle()
|
||||
|
||||
score_text.hideturtle()
|
||||
score_text.penup()
|
||||
score_text.goto((WIDTH - HEIGHT) // 2, HEIGHT * .25)
|
||||
score_text.write('Score: 0', font=('Arial', 24, 'bold'))
|
||||
|
||||
|
||||
def coord_translation(x, y):
|
||||
|
@ -26,13 +32,18 @@ try:
|
|||
return x - (WIDTH / 2), y - (HEIGHT / 2)
|
||||
|
||||
|
||||
# Creates list of images, doubles it, and shuffles it
|
||||
image_files = os.listdir('images')
|
||||
|
||||
# Creates list of images, doubles it, and shuffles it
|
||||
image_files.remove('turtle.png')
|
||||
image_files.extend(image_files)
|
||||
random.shuffle(image_files)
|
||||
|
||||
cards = [Card(f'images/{file}') for file in image_files]
|
||||
cards = []
|
||||
for file in image_files:
|
||||
path = f'images/{file}'
|
||||
print(path)
|
||||
cards.append(Card(path))
|
||||
|
||||
# Move sprites
|
||||
for i in range(16):
|
||||
|
@ -47,31 +58,43 @@ try:
|
|||
"""
|
||||
:return: The card which was clicked
|
||||
"""
|
||||
global clicked_cards
|
||||
for card in cards:
|
||||
if card.is_mouse_over(x, y):
|
||||
if len(clicked_cards) == 2:
|
||||
clicked_cards = []
|
||||
print(cards.index(card))
|
||||
card.to_front()
|
||||
clicked_cards.append(card)
|
||||
|
||||
|
||||
screen.onclick(fun=clicked_card)
|
||||
score = 0
|
||||
|
||||
|
||||
def update_score():
|
||||
score_text.clear()
|
||||
score_text.write(f'Score: {score}', font=('Arial', 24, 'bold'))
|
||||
|
||||
|
||||
game_is_running = True
|
||||
clicked_cards = []
|
||||
score = 0
|
||||
|
||||
screen.onclick(fun=clicked_card)
|
||||
|
||||
while game_is_running:
|
||||
time.sleep(0.1)
|
||||
if len(clicked_cards) == 2:
|
||||
if clicked_cards[0].shape() != clicked_cards[1].shape():
|
||||
time.sleep(2)
|
||||
if clicked_cards[0] != clicked_cards[1]:
|
||||
time.sleep(1)
|
||||
clicked_cards[0].to_back()
|
||||
clicked_cards[1].to_back()
|
||||
print('Wrong!')
|
||||
score -= 1
|
||||
update_score()
|
||||
else:
|
||||
print('Correct!')
|
||||
score += 5
|
||||
update_score()
|
||||
clicked_cards = []
|
||||
screen.update()
|
||||
|
||||
|
|
Loading…
Reference in a new issue