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):
|
|
|
|
def __init__(self, image_path):
|
2022-07-27 22:26:41 -05:00
|
|
|
"""
|
|
|
|
Initializes Card object.
|
|
|
|
:param image_path: Path to image
|
|
|
|
"""
|
2022-07-31 23:14:09 -05:00
|
|
|
super().__init__()
|
2022-07-27 22:26:41 -05:00
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
# self.size = 150 # desired image height and width (in pixels)
|
|
|
|
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.shape('card_back')
|
2022-07-27 22:26:41 -05:00
|
|
|
|
2022-07-31 23:14:09 -05:00
|
|
|
def to_front(self):
|
|
|
|
self.shape('card_front')
|
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-07-31 23:18:36 -05:00
|
|
|
# https://www.youtube.com/watch?v=dQw4w9WgXcQ
|
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
|