2022-07-31 23:14:09 -05:00
|
|
|
import turtle
|
|
|
|
from tkinter import PhotoImage
|
2022-07-27 22:26:41 -05:00
|
|
|
|
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
class Card(turtle.Turtle):
|
2022-08-01 00:06:36 -05:00
|
|
|
card_count = 0
|
2022-08-01 00:16:47 -05:00
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
def __init__(self, image_path):
|
2022-07-27 22:26:41 -05:00
|
|
|
"""
|
|
|
|
Initializes Card object.
|
2022-08-01 00:06:36 -05:00
|
|
|
:param image_path: Path to image for card_front
|
2022-07-27 22:26:41 -05:00
|
|
|
"""
|
2022-07-31 23:14:09 -05:00
|
|
|
super().__init__()
|
2022-07-27 22:26:41 -05:00
|
|
|
|
2022-08-01 00:06:36 -05:00
|
|
|
self.image_path = image_path
|
|
|
|
self.card_id = Card.card_count
|
|
|
|
Card.card_count += 1
|
2022-07-31 23:14:09 -05:00
|
|
|
self.penup()
|
|
|
|
self.speed(8)
|
2022-08-01 00:06:36 -05:00
|
|
|
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))
|
2022-07-31 23:14:09 -05:00
|
|
|
self.shape('card_back')
|
2022-07-27 22:26:41 -05:00
|
|
|
|
2022-08-01 00:06:36 -05:00
|
|
|
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
|
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
def to_front(self):
|
2022-08-01 00:06:36 -05:00
|
|
|
self.shape(f'card_front{self.card_id}')
|
2022-07-27 22:26:41 -05:00
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
def to_back(self):
|
|
|
|
self.shape('card_back')
|
|
|
|
|
|
|
|
def is_mouse_over(self, x, y):
|
|
|
|
# Collision code reused from D. Atkinson's Turtle Crossing program, with some minor modifications.
|
2022-08-01 00:16:47 -05:00
|
|
|
# http://tiny.cc/ShortCodeLink
|
2022-07-31 23:14:09 -05:00
|
|
|
top_edge = self.ycor() + 103
|
|
|
|
bottom_edge = self.ycor() - 103
|
|
|
|
car_left_edge = self.xcor() - 103
|
|
|
|
car_right_edge = self.xcor() + 103
|
|
|
|
if (
|
|
|
|
(
|
|
|
|
(y - bottom_edge > 0 and top_edge - y > 0)
|
|
|
|
or
|
|
|
|
(top_edge - y > 0 and y - bottom_edge > 0)
|
|
|
|
)
|
|
|
|
and
|
|
|
|
(
|
|
|
|
(x - car_left_edge > 0 and car_right_edge - x > 0)
|
|
|
|
or
|
|
|
|
(x - car_left_edge > 0 and car_right_edge - x > 0)
|
|
|
|
)
|
|
|
|
):
|
|
|
|
return True
|
|
|
|
return False
|