Initial commit. Also, Card and ImageSprite likely done, at least usable (I think).
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
## Memory Game
|
||||||
|
|
||||||
|
This is my final project (option 1) for ITSE-1479. Instead of using the provided
|
0
card.py
Normal file
37
image_sprite.py
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
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)
|
||||||
|
|
||||||
|
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, y)
|
||||||
|
|
BIN
images/in_class.png
Normal file
After Width: | Height: | Size: 101 KiB |
BIN
images/sobbing.png
Normal file
After Width: | Height: | Size: 109 KiB |
BIN
images/tears_of_joy.png
Normal file
After Width: | Height: | Size: 169 KiB |
BIN
images/thinking.png
Normal file
After Width: | Height: | Size: 70 KiB |
BIN
images/thumbs_up.png
Normal file
After Width: | Height: | Size: 119 KiB |
BIN
images/tongue.png
Normal file
After Width: | Height: | Size: 108 KiB |
BIN
images/turtle.jpg
Normal file
After Width: | Height: | Size: 189 KiB |
BIN
images/unamused.png
Normal file
After Width: | Height: | Size: 78 KiB |
BIN
images/wink.png
Normal file
After Width: | Height: | Size: 98 KiB |
48
main.py
Normal file
|
@ -0,0 +1,48 @@
|
||||||
|
import os
|
||||||
|
import random
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
from image_sprite import Card
|
||||||
|
|
||||||
|
# CONSTANTS
|
||||||
|
WIDTH = 1600
|
||||||
|
HEIGHT = 900
|
||||||
|
BACKGROUND_COLOR = (66, 135, 245)
|
||||||
|
|
||||||
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
pygame.display.set_caption('Turtle Cards')
|
||||||
|
|
||||||
|
# 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/{image_files}', WIDTH, HEIGHT) for file in image_files]
|
||||||
|
# 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.
|
||||||
|
cards[i].move(i * (150 * i % 4) + 85, i * (150 * int(i / 4)) + 85)
|
||||||
|
|
||||||
|
game_is_running = True
|
||||||
|
|
||||||
|
while game_is_running:
|
||||||
|
# Draw things
|
||||||
|
screen.fill(BACKGROUND_COLOR)
|
||||||
|
for card in cards:
|
||||||
|
screen.blit(card.image, card.rect)
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
game_is_running = False
|
||||||
|
clicked_cards = []
|
||||||
|
for card in cards:
|
||||||
|
if card.is_clicked():
|
||||||
|
clicked_cards.append(card)
|
||||||
|
card.on_click()
|
||||||
|
card.flip_card()
|
||||||
|
|
||||||
|
pygame.display.flip()
|
65
test.py
Normal file
|
@ -0,0 +1,65 @@
|
||||||
|
# import the pygame module
|
||||||
|
import pygame
|
||||||
|
|
||||||
|
# import pygame.locals for easier
|
||||||
|
# access to key coordinates
|
||||||
|
from pygame.locals import *
|
||||||
|
|
||||||
|
|
||||||
|
# Define our square object and call super to
|
||||||
|
# give it all the properties and methods of pygame.sprite.Sprite
|
||||||
|
# Define the class for our square objects
|
||||||
|
class Square(pygame.sprite.Sprite):
|
||||||
|
def __init__(self):
|
||||||
|
super(Square, self).__init__()
|
||||||
|
|
||||||
|
# Define the dimension of the surface
|
||||||
|
# Here we are making squares of side 25px
|
||||||
|
self.surf = pygame.Surface((25, 25))
|
||||||
|
|
||||||
|
# Define the color of the surface using RGB color coding.
|
||||||
|
self.surf.fill((0, 200, 255))
|
||||||
|
self.rect = self.surf.get_rect()
|
||||||
|
|
||||||
|
|
||||||
|
# initialize pygame
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
# Define the dimensions of screen object
|
||||||
|
screen = pygame.display.set_mode((800, 600))
|
||||||
|
|
||||||
|
# instantiate all square objects
|
||||||
|
square1 = Square()
|
||||||
|
square2 = Square()
|
||||||
|
square3 = Square()
|
||||||
|
square4 = Square()
|
||||||
|
|
||||||
|
# Variable to keep our game loop running
|
||||||
|
gameOn = True
|
||||||
|
|
||||||
|
# Our game loop
|
||||||
|
while gameOn:
|
||||||
|
# for loop through the event queue
|
||||||
|
for event in pygame.event.get():
|
||||||
|
|
||||||
|
# Check for KEYDOWN event
|
||||||
|
if event.type == KEYDOWN:
|
||||||
|
|
||||||
|
# If the Backspace key has been pressed set
|
||||||
|
# running to false to exit the main loop
|
||||||
|
if event.key == K_BACKSPACE:
|
||||||
|
gameOn = False
|
||||||
|
|
||||||
|
# Check for QUIT event
|
||||||
|
elif event.type == QUIT:
|
||||||
|
gameOn = False
|
||||||
|
|
||||||
|
# Define where the squares will appear on the screen
|
||||||
|
# Use blit to draw them on the screen surface
|
||||||
|
screen.blit(square1.surf, (40, 40))
|
||||||
|
screen.blit(square2.surf, (40, 530))
|
||||||
|
screen.blit(square3.surf, (730, 40))
|
||||||
|
screen.blit(square4.surf, (730, 530))
|
||||||
|
|
||||||
|
# Update the display using flip
|
||||||
|
pygame.display.flip()
|