Arts >> Umenie a zábava >  >> Divadlo >> Dramaturgia

Python Maze Runner Game Code Example Using Pygame

Tu je kompletný Pygame príklad pre jednoduchú hru Maze Runner. Inicializuje okno, kreslá mriežku bludišťa a umožňuje pohyb hráča šípkami.

import pygame
# Farby
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# Nastavenie obrazovky
size = [700, 500]
screen = pygame.display.set_mode(size)
# Herný cyklus
done = False
clock = pygame.time.Clock()
# Pozícia a rýchlosť hráča
x_speed = 0
y_speed = 0
x_coord = 10
y_coord = 10
# Rozvrhnutie bludišťa (1 = stena, 0 = cesta)
maze = [
[1,1,1,1,1,1,1,1,1,1],
[1,0,1,0,1,0,1,0,1,1],
[1,0,0,0,1,0,0,0,1,1],
[1,0,1,0,1,1,1,0,1,1],
[1,0,1,0,0,0,1,0,1,1],
[1,0,1,1,1,0,1,0,1,1],
[1,0,0,0,0,0,1,0,0,1],
[1,0,1,1,1,1,1,0,1,1],
[1,1,1,1,1,1,1,1,1,1]
]
while not done:
clock.tick(10)
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(BLACK)
# Kreslenie bludišťa
for row in range(9):
for col in range(10):
if maze[row][col] == 1:
pygame.draw.rect(screen, WHITE, [25*col, 25*row, 25, 25])
# Kreslenie hráča
pygame.draw.rect(screen, WHITE, [x_coord, y_coord, 25, 25])
# Pohyb klávesami
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
y_speed = -5
elif keys[pygame.K_DOWN]:
y_speed = 5
elif keys[pygame.K_LEFT]:
x_speed = -5
elif keys[pygame.K_RIGHT]:
x_speed = 5
x_coord += x_speed
y_coord += y_speed
# Kolízia so stenou
if maze[int(y_coord/25)][int(x_coord/25)] == 1:
x_speed = 0
y_speed = 0
pygame.display.flip()
pygame.quit()

Dramaturgia

Related Categories