Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

pygame code directory help not finding image code: import pygame import sys from pygame.locals import * # Initialize Pygame pygame.init ( ) # Screen dimensions

pygame code directory help not finding image
code:
import pygame
import sys
from pygame.locals import *
# Initialize Pygame
pygame.init()
# Screen dimensions
SCREEN_WIDTH =800
SCREEN_HEIGHT =600
# Colors
WHITE =(255,255,255)
BLACK =(0,0,0)
BLUE =(0,0,255)
# Load images
maze_img = pygame.image.load("/mnt/data/map_layout.png")
player_img = pygame.image.load("trainer.png")
treasure_img = pygame.image.load("treasure.png")
# Load sounds
treasure_sound = pygame.mixer.Sound("yippee.wav")
dead_end_sound = pygame.mixer.Sound("Do_oh.wav")
# Scale images
player_img = pygame.transform.scale(player_img, (50,50))
treasure_img = pygame.transform.scale(treasure_img, (50,50))
# Create the screen object
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption('A-MAZE-ING Adventure!')
# Initial game state
player_pos =[50,50] # Starting position
treasure_collected = False
game_over = False
timer_started = False
timer =30
def draw_maze():
screen.fill(WHITE)
screen.blit(maze_img, (0,0))
screen.blit(player_img, player_pos)
if not treasure_collected:
screen.blit(treasure_img, (700,500)) # Position of the treasure
def draw_timer():
font = pygame.font.Font(None,36)
text = font.render(f'Time: {timer}', True, BLUE)
screen.blit(text,(10,10))
def move_player(key):
if key == K_w:
player_pos[1]-=5
elif key == K_s:
player_pos[1]+=5
elif key == K_a:
player_pos[0]-=5
elif key == K_d:
player_pos[0]+=5
# Collision detection
if screen.get_at(player_pos)== BLACK:
dead_end_sound.play()
# Undo the movement
if key == K_w:
player_pos[1]+=5
elif key == K_s:
player_pos[1]-=5
elif key == K_a:
player_pos[0]+=5
elif key == K_d:
player_pos[0]-=5
def check_collision():
global treasure_collected, timer_started, timer
# Check if player collides with the treasure
if not treasure_collected and player_pos[0]>=700 and player_pos[1]>=500:
treasure_collected = True
treasure_sound.play()
timer_started = True
timer =30 # Start the 30 second timer
def update_timer():
global timer, game_over
if timer_started:
pygame.time.set_timer(USEREVENT,1000)
for event in pygame.event.get():
if event.type == USEREVENT:
if timer >0:
timer -=1
else:
game_over = True
def main():
global game_over
clock = pygame.time.Clock()
while not game_over:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
move_player(event.key)
check_collision()
draw_maze()
if timer_started:
update_timer()
draw_timer()
pygame.display.flip()
clock.tick(30)
if __name__=="__main__":
main()
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions