diff --git a/card.py b/card.py index 49e252d..7caef61 100644 --- a/card.py +++ b/card.py @@ -1,26 +1,48 @@ -import pygame -from image_sprite import ImageSprite +import turtle +from tkinter import PhotoImage -class Card(ImageSprite): - def __init__(self, image_path, screen_width, screen_height): +class Card(turtle.Turtle): + def __init__(self, image_path): """ Initializes Card object. :param image_path: Path to image - :param screen_width: Width of screen - :param screen_height: Height of screen """ - super().__init__(image_path, screen_width, screen_height) + super().__init__() - self.size = 150 # image height and width (in pixels) - self.card_back = pygame.transform.scale(pygame.image.load('images/turtle.jpg'), (self.size, self.size)) - self.card_front = pygame.transform.scale(pygame.image.load(image_path), (self.size, self.size)) - self.image = self.card_back + # 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') - self.temp_count = 0 + def to_front(self): + self.shape('card_front') - def flip_card(self): - if self.image == self.card_back: - self.image = self.card_front - else: - self.image = self.card_back + 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. + 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 diff --git a/image_sprite.py b/image_sprite.py deleted file mode 100644 index 999102d..0000000 --- a/image_sprite.py +++ /dev/null @@ -1,38 +0,0 @@ -import pygame - - -class ImageSprite(pygame.sprite.Sprite): - def __init__(self, image_path, screen_width, screen_height): - """ - Initializes Card object. - :param image_path: Path of - :param screen_width: - :param screen_height: - """ - super().__init__() - - self.image = pygame.image.load(image_path) - self.rect = self.image.get_rect() - self.rect.move(screen_width / 2, screen_height / 2) - self.width = screen_width - self.height = screen_height - - def is_clicked(self): - """ - Tests if the sprite is clicked - :return: Returns True if clicked, False otherwise - """ - mouse_pos = pygame.mouse.get_pos() - if self.rect.collidepoint(mouse_pos) and pygame.mouse.get_pressed()[0]: - return True - else: - return False - - def move(self, x, y): - """ - Moves to the given x and y coordinates - :param x: x coordinate - :param y: y coordinate - :return: None - """ - self.rect.move(x / self.width, y / self.height) diff --git a/images/turtle.jpg b/images/turtle.jpg deleted file mode 100644 index bf07d9e..0000000 Binary files a/images/turtle.jpg and /dev/null differ diff --git a/images/turtle.png b/images/turtle.png new file mode 100644 index 0000000..342aa1a Binary files /dev/null and b/images/turtle.png differ diff --git a/main.py b/main.py index c62833c..133452c 100644 --- a/main.py +++ b/main.py @@ -1,65 +1,80 @@ -import os -import random -import pygame +try: + import os + import random + import time + import turtle -from card import Card + from card import Card -# CONSTANTS -WIDTH = 1600 -HEIGHT = 900 -BACKGROUND_COLOR = (66, 135, 245) + # CONSTANTS + WIDTH = 1600 + HEIGHT = 840 + BACKGROUND_COLOR = (66, 135, 245) -screen = pygame.display.set_mode((WIDTH, HEIGHT)) -pygame.display.set_caption('Turtle Cards') + screen = turtle.Screen() + turtle.bgcolor('#46a38d') + screen.setup(WIDTH, HEIGHT) -# Creates list of images, doubles it, and shuffles it -image_files = os.listdir('images') -image_files.remove('turtle.jpg') -image_files.extend(image_files) -random.shuffle(image_files) -# Create sprites -cards = [Card(f'images/{file}', WIDTH, HEIGHT) for file in image_files] -for card in cards: - screen.blit(card.image, card.rect) -pygame.display.flip() + 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) -# Move sprites -# TODO: Fix initial movement of 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. - cards[i].move((150 * (i % 4)) + 85, (150 * int(i / 4)) + 85) - print(str((150 * (i % 4)) + 85), str((150 * int(i / 4)) + 85)) - print(cards[i].rect) -game_is_running = True -clicked_cards = [] + # Creates list of images, doubles it, and shuffles it + image_files = os.listdir('images') + image_files.remove('turtle.png') + image_files.extend(image_files) + random.shuffle(image_files) -while game_is_running: - # Draw things - screen.fill(BACKGROUND_COLOR) - for card in cards: - screen.blit(card.image, card.rect) + cards = [Card(f'images/{file}') for file in image_files] - if clicked_cards[0].image == clicked_cards[1].image: - # Code for if cards match - print('Cards match!') - clicked_cards = [] - else: - # Code for if cards don't match - print('Cards don\'t match!') - clicked_cards[0].flip_card() - clicked_cards[1].flip_card() - clicked_cards = [] + # 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) - for event in pygame.event.get(): - if event.type == pygame.QUIT: - game_is_running = False + + def clicked_card(x, y): + """ + :return: The card which was clicked + """ for card in cards: - if card.is_clicked(): + if card.is_mouse_over(x, y): + print(cards.index(card)) + card.to_front() clicked_cards.append(card) - card.flip_card() - cards[0].move(1, 0) - pygame.display.flip() + + + screen.onclick(fun=clicked_card) + + game_is_running = True + clicked_cards = [] + score = 0 + + 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) + clicked_cards[0].to_back() + clicked_cards[1].to_back() + print('Wrong!') + score -= 1 + else: + print('Correct!') + score += 5 + clicked_cards = [] + screen.update() + + screen.mainloop() +except turtle.Terminator: + exit(0)