# - Imports - import os # Hide Pygame welcome message os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide" import pygame import sys import random import math # - Main Function - def main(): # - Initialize Game - pygame.init() pygame.mixer.init() clock = pygame.time.Clock() # - Screen Size - screen = pygame.display.set_mode((800, 600)) # - Title - pygame.display.set_caption("Wolf needs Watermelon!") # - Icon - icon = pygame.image.load('images/wolf_player.png') icon = pygame.transform.scale(icon, (32, 32)) iconFinal = pygame.Surface(icon.get_size()) key = (0, 255, 0) iconFinal.fill(key) iconFinal.set_colorkey(key) iconFinal.blit(icon, (0, 0)) pygame.display.set_icon(iconFinal) # - Random Position Generators - def randX(): return random.randint(0, 736) def randY(): return random.randint(0, 536) # - Player - playerImg = pygame.image.load('images/wolf_player.png') playerX = 370 playerY = 480 playerX_change = 0 playerY_change = 0 playerRect = playerImg.get_rect() def player(x, y): screen.blit(playerImg, (x, y)) # - Watermelon - melonImg = pygame.image.load('images/melon_player.png') melonX = randX() melonY = randY() melonX_change = 0 melonY_change = 0 melonRect = melonImg.get_rect() def melon(x, y): screen.blit(melonImg, (x, y)) # - Wolf eating Watermelon - wolfMelonImg = pygame.image.load('images/wolf_melon.jpg') wolfMelonX = 300 wolfMelonY = 150 def wolfMelon(x, y): screen.blit(wolfMelonImg, (x, y)) # - Collision Detection Function - def isCollision(melonX, melonY, playerX, playerY): distance = math.sqrt((math.pow(melonX - playerX, 2)) + (math.pow(melonY - playerY, 2))) if distance < 27: return True else: return False # - Background Music - pygame.mixer.init() pygame.mixer.music.load('audio/bg.mp3') pygame.mixer.music.play(-1, 0.0) # - Score - score = 0 white = (255, 255, 255) def drawText(surf, text, size, x, y): font = pygame.font.Font(None, 30) textSurface = font.render(text, True, white) textRect = textSurface.get_rect() textRect.midtop = (x, y) surf.blit(textSurface, textRect) # - Time Limit - pygame.time.set_timer(pygame.USEREVENT, 1000) timer = 30 # - Background - bg = pygame.image.load('images/bg.png') clock = pygame.time.Clock() # - Game Loop - running = True while running: # - Set FPS - clock.tick(144) # - Set Background - screen.blit(bg, (0, 0)) # - Render Player - player(playerX, playerY) # - Render Melon - melon(melonX, melonY) # - Draw Score - drawText(screen, "Score: " + str(score), 18, 50, 10) # - Draw FPS - drawText(screen, "FPS: " + str(int(clock.get_fps())), 18, 53, 35) # - Interactive Game Items - for event in pygame.event.get(): # - Quit Function - if event.type == pygame.QUIT: running = False # - Movement Functions + More - if event.type == pygame.KEYDOWN: if event.key == pygame.K_a or event.key == pygame.K_LEFT: playerX_change = -1 elif event.key == pygame.K_d or event.key == pygame.K_RIGHT: playerX_change = 1 elif event.key == pygame.K_w or event.key == pygame.K_UP: playerY_change = -1 elif event.key == pygame.K_s or event.key == pygame.K_DOWN: playerY_change = 1 # - Restart Function - elif event.key == pygame.K_r: pygame.quit() main() # - Exit Function - elif event.key == pygame.K_ESCAPE: pygame.quit() if event.type == pygame.KEYUP: if event.key == pygame.K_a or event.key == pygame.K_d or event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: playerX_change = 0 elif event.key == pygame.K_w or event.key == pygame.K_s or event.key == pygame.K_UP or event.key == pygame.K_DOWN: playerY_change = 0 # - Collision Detection Check - collision = isCollision(melonX, melonY, playerX, playerY) if collision: melonX = randX() melonY = randY() score += 1 nomSound = pygame.mixer.Sound('audio/nom.wav') nomSound.play() # - Timer Logic - if event.type == pygame.USEREVENT: timer -= 1 # - Draw Timer - drawText(screen, "Time Left: " + str(timer), 18, 73, 60) # - Score Check - if score >= 10: melonX = 1000 melonY = 1000 wolfMelon(wolfMelonX, wolfMelonY) drawText(screen, "You win! Press Escape to exit or R to try again.", 18, 400, 300) drawText(screen, "Thanks for playing!", 18, 400, 330) # - Additional Timer Check - if timer <= 0: timer = 0 # - Timer Check - elif timer <= 0: timer = 0 drawText(screen, "Woops... Press Escape to exit or R to try again.", 25, 400, 300) drawText(screen, "Thanks for playing!", 18, 400, 330) # - More Movement Functions - playerX += playerX_change playerY += playerY_change if playerX <= 0: playerX = 0 elif playerX >= 736: playerX = 736 elif playerY >= 536: playerY = 536 elif playerY <= 0: playerY = 0 # - Render and Update Screen - pygame.display.update() # - Call Main Function - main()