commit 42d07e95ed858398eb45792e3a959e0d5ecccebb Author: maxneedspats <84929191+maxneedspats@users.noreply.github.com> Date: Wed Jul 27 22:19:04 2022 -0500 Initial commit. Also, Card and ImageSprite likely done, at least usable (I think). diff --git a/README.md b/README.md new file mode 100644 index 0000000..f7ac49f --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +## Memory Game + +This is my final project (option 1) for ITSE-1479. Instead of using the provided diff --git a/card.py b/card.py new file mode 100644 index 0000000..e69de29 diff --git a/image_sprite.py b/image_sprite.py new file mode 100644 index 0000000..b1a5ce6 --- /dev/null +++ b/image_sprite.py @@ -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) + diff --git a/images/in_class.png b/images/in_class.png new file mode 100644 index 0000000..1ea6744 Binary files /dev/null and b/images/in_class.png differ diff --git a/images/sobbing.png b/images/sobbing.png new file mode 100644 index 0000000..1a2a4b8 Binary files /dev/null and b/images/sobbing.png differ diff --git a/images/tears_of_joy.png b/images/tears_of_joy.png new file mode 100644 index 0000000..258a2db Binary files /dev/null and b/images/tears_of_joy.png differ diff --git a/images/thinking.png b/images/thinking.png new file mode 100644 index 0000000..5ef2cbd Binary files /dev/null and b/images/thinking.png differ diff --git a/images/thumbs_up.png b/images/thumbs_up.png new file mode 100644 index 0000000..13ccf3e Binary files /dev/null and b/images/thumbs_up.png differ diff --git a/images/tongue.png b/images/tongue.png new file mode 100644 index 0000000..dc3f843 Binary files /dev/null and b/images/tongue.png differ diff --git a/images/turtle.jpg b/images/turtle.jpg new file mode 100644 index 0000000..bf07d9e Binary files /dev/null and b/images/turtle.jpg differ diff --git a/images/unamused.png b/images/unamused.png new file mode 100644 index 0000000..9780fcd Binary files /dev/null and b/images/unamused.png differ diff --git a/images/wink.png b/images/wink.png new file mode 100644 index 0000000..9884ff0 Binary files /dev/null and b/images/wink.png differ diff --git a/main.py b/main.py new file mode 100644 index 0000000..3c27541 --- /dev/null +++ b/main.py @@ -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() diff --git a/test.py b/test.py new file mode 100644 index 0000000..9bef975 --- /dev/null +++ b/test.py @@ -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()