Minor changes, I can't remember. Probably just cleaned up code and stuff.

This commit is contained in:
maxneedspats 2022-07-28 12:38:41 -05:00
parent 66b837cecd
commit e4c78d00ff
4 changed files with 19 additions and 25 deletions

View file

@ -1,3 +1,5 @@
## Memory Game
This is my final project (option 1) for ITSE-1479. Instead of using the provided
This is my final project (option 1) for ITSE-1479.
Instead of using the provided cardback.png image, I used a photo of one of my turtles as the card back.

18
card.py
View file

@ -15,26 +15,10 @@ class Card(ImageSprite):
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))
super().image = self.card_back
self.image = self.card_back
self.temp_count = 0
@property
def image(self):
return super().image
@image.setter
def image(self, image):
super().image = image
@property
def rect(self):
return super().rect
@rect.setter
def rect(self, rect):
super().rect = rect
def on_click(self):
"""
Is meant to be called when the card is clicked, but that must be implemented by the class which has-a Card. \

View file

@ -10,10 +10,12 @@ class ImageSprite(pygame.sprite.Sprite):
:param screen_height:
"""
super().__init__()
self.image = pygame.image.load(image_path)
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):
"""
@ -33,5 +35,4 @@ class ImageSprite(pygame.sprite.Sprite):
:param y: y coordinate
:return: None
"""
self.rect.move(x, y)
self.rect.move(x / self.width, y / self.height)

15
main.py
View file

@ -2,7 +2,7 @@ import os
import random
import pygame
from image_sprite import Card
from card import Card
# CONSTANTS
WIDTH = 1600
@ -19,13 +19,20 @@ image_files.extend(image_files)
random.shuffle(image_files)
# Create sprites
cards = [Card(f'images/{image_files}', WIDTH, HEIGHT) for file in image_files]
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()
# 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(i * (150 * i % 4) + 85, i * (150 * int(i / 4)) + 85)
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
@ -44,5 +51,5 @@ while game_is_running:
clicked_cards.append(card)
card.on_click()
card.flip_card()
cards[0].move(1, 0)
pygame.display.flip()