Created
March 10, 2021 19:40
-
-
Save rosswf/5bb96eba19ace3f1207a7dd355800935 to your computer and use it in GitHub Desktop.
Beginner.py weekly coding prompt March 9th - Create something to determine if someone has the luck of the Irish.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import pygame | |
import sys | |
# Options | |
WIDTH = 1024 | |
HEIGHT = 768 | |
FONT = "carlito" | |
FONT_SIZE = 72 | |
FONT_COLOUR = pygame.Color("0x07a860") | |
BG_COLOUR = pygame.Color("0xdbdbdb") | |
FPS = 2 | |
WIN_TEXT = "You have the luck of the Irish!" | |
IMAGE = "assets/pot-of-gold.png" | |
# Game Setup | |
pygame.init() | |
pygame.display.set_caption("St Patrick's Day") | |
clock = pygame.time.Clock() | |
game_window = pygame.display.set_mode((WIDTH, HEIGHT)) | |
# Setup the ending text | |
font = pygame.font.SysFont(FONT, FONT_SIZE) | |
win_text = font.render(WIN_TEXT, True, FONT_COLOUR) | |
win_text_size = font.size(WIN_TEXT) | |
# Open image | |
pot_img = pygame.image.load(IMAGE) | |
pot_width, pot_height = pot_img.get_size() | |
def play(): | |
game_over = False | |
while not game_over: | |
game_window.fill(BG_COLOUR) | |
x = random.randint(0, WIDTH - pot_width) | |
y = random.randint(0, HEIGHT - pot_height) | |
game_window.blit(pot_img, (x, y)) | |
pygame.display.update() | |
clock.tick(FPS) | |
# event checking | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
elif event.type == pygame.MOUSEBUTTONDOWN: | |
if pot_img.get_rect(topleft=(x, y)).collidepoint(event.pos): | |
game_over = True | |
def win_screen(): | |
while True: | |
game_window.fill(BG_COLOUR) | |
game_window.blit( | |
pot_img, (WIDTH // 2 - pot_width // 2, HEIGHT // 2 - pot_height // 2) | |
) | |
game_window.blit(win_text, (WIDTH // 2 - win_text_size[0] // 2, HEIGHT // 3)) | |
pygame.display.update() | |
for event in pygame.event.get(): | |
if event.type == pygame.QUIT: | |
sys.exit() | |
if __name__ == "__main__": | |
play() | |
win_screen() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment