Arts >> Theater >> Playwriting

What is the code for maze on runner book?

```python

import pygame

Define some colors

BLACK = (0, 0, 0)

WHITE = (255, 255, 255)

Set the height and width of the screen

size = [700, 500]

screen = pygame.display.set_mode(size)

Loop until the user clicks the close button.

done = False

clock = pygame.time.Clock()

Speed in pixels per second

x_speed = 0

y_speed = 0

Current position

x_coord = 10

y_coord = 10

Create a maze

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]

]

Loop as long as done == False

while not done:

# This limits the while loop to a max of 10 times per second.

# Leave this out and we will use all CPU we can.

clock.tick(10)

for event in pygame.event.get(): # User did something

if event.type == pygame.QUIT: # If user clicked close

done = True # Flag that we are done so we exit this loop

# Clear the screen and set the screen background

screen.fill(BLACK)

# Draw the maze

for row in range(9):

for column in range(10):

if maze[row][column] == 1:

pygame.draw.rect(screen, WHITE, [(25 * column), (25 * row), 25, 25])

# Draw the runner

pygame.draw.rect(screen, WHITE, [x_coord, y_coord, 25, 25])

# Move the runner based on the key pressed

pressed = pygame.key.get_pressed()

if pressed[pygame.K_UP]:

y_speed = -5

elif pressed[pygame.K_DOWN]:

y_speed = 5

elif pressed[pygame.K_LEFT]:

x_speed = -5

elif pressed[pygame.K_RIGHT]:

x_speed = 5

# Update the position of the runner

x_coord += x_speed

y_coord += y_speed

# If the runner hits the edge of the screen, bounce it back

if x_coord > 675:

x_speed = -5

elif x_coord < 0:

x_speed = 5

if y_coord > 475:

y_speed = -5

elif y_coord < 0:

y_speed = 5

# If the runner hits a wall, stop it

if maze[int(y_coord / 25)][int(x_coord / 25)] == 1:

x_speed = 0

y_speed = 0

#Update the screen

pygame.display.flip()

Quit pygame

pygame.quit()

```

Playwriting

Related Categories