Hi, at the end of the maze you see a red square. I was trying to make it say "you win , would you like to play again y or n". i have tried using def mes and gameover = false but nothing seems to work. would help a lot if someone could help. (sorry this is a school project and we have to use PYGAME.)

``````import os
import pygame
import random

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
brown = (205,133,63)
blue = (135,206,250)

FPS = 60
#the dimension of the game
display_width = 680
display_height = 440

# Class for the player
class Player(object):

def __init__(self):
self.rect = pygame.Rect(40, 40, 30, 30)

def move(self, dx, dy):

# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)

def move_single_axis(self, dx, dy):

# Move the rect
self.rect.x += dx
self.rect.y += dy

# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right, Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left, Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down, Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up, Hit the bottom side of the wall
self.rect.top = wall.rect.bottom

# Class to hold a wall rect
class Wall(object):

def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 40, 40)

font = pygame.font.SysFont(None, 50)

def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [680/2, display_height/2])

# Initialise pygame
os.environ["Time to play"] = "1"

# Set up the display
pygame.display.set_caption("Wrath of the gods")
gameDisplay = pygame.display.set_mode((display_width,display_height))

clock = pygame.time.Clock()
walls = [] # List to hold the walls
player = Player() # Create the player

# Holds the level layout in a list of strings.
level = [
"WWWWWWWWWWWWWWWWW",
"W    W    W     W",
"W WW W WW   WWW W",
"W W  W  W W W W W",
"W WWWWW WWW W W W",
"W  W  W   W   W W",
"WW W WW WWWWWWW W",
"W    W   W      W",
"W WWWW W WWWWW WW",
"W               W",
"WWWWWWWWWWWWWWWEW",

]

# W = wall, E = exit
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, 40, 40)
x += 40
y += 40
x = 0

running = True
while running:

clock.tick(FPS)

for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False

# Move the player
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
player.move(-2, 0)
if key[pygame.K_RIGHT]:
player.move(2, 0)
if key[pygame.K_UP]:
player.move(0, -2)
if key[pygame.K_DOWN]:
player.move(0, 2)

if player.rect.colliderect(end_rect):
raise SystemExit

# Draw the scene
gameDisplay.fill((brown))
for wall in walls:
pygame.draw.rect(gameDisplay, green, wall.rect)
pygame.draw.rect(gameDisplay, red, end_rect)
pygame.draw.rect(gameDisplay, white, player.rect)
pygame.display.flip()

pygame.quit()
quit()
``````

I've found a `play_again()` function <here>. It can be adapted to your game by returning a boolean. Also the procedure to initialize the maze and the player must be called again, so I write a `reinit()` function to do that. Here is the result. It could be a good starting point.

``````import os
import pygame
import random

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,155,0)
brown = (205,133,63)
blue = (135,206,250)

FPS = 60
#the dimension of the game
display_width = 680
display_height = 440

# Class for the player
class Player(object):

def __init__(self):
self.rect = pygame.Rect(40, 40, 30, 30)

def move(self, dx, dy):

# Move each axis separately. Note that this checks for collisions both times.
if dx != 0:
self.move_single_axis(dx, 0)
if dy != 0:
self.move_single_axis(0, dy)

def move_single_axis(self, dx, dy):

# Move the rect
self.rect.x += dx
self.rect.y += dy

# If you collide with a wall, move out based on velocity
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: # Moving right, Hit the left side of the wall
self.rect.right = wall.rect.left
if dx < 0: # Moving left, Hit the right side of the wall
self.rect.left = wall.rect.right
if dy > 0: # Moving down, Hit the top side of the wall
self.rect.bottom = wall.rect.top
if dy < 0: # Moving up, Hit the bottom side of the wall
self.rect.top = wall.rect.bottom

# Class to hold a wall rect
class Wall(object):

def __init__(self, pos):
walls.append(self)
self.rect = pygame.Rect(pos[0], pos[1], 40, 40)

font = pygame.font.SysFont(None, 50)

def message_to_screen(msg,color):
screen_text = font.render(msg, True, color)
gameDisplay.blit(screen_text, [680/2, display_height/2])

# Initialise pygame
os.environ["Time to play"] = "1"

# Set up the display
pygame.display.set_caption("Wrath of the gods")
gameDisplay = pygame.display.set_mode((display_width,display_height))

clock = pygame.time.Clock()
walls = [] # List to hold the walls
player = None
end_rect = None

def reinit():
global player, end_rect
player = Player() # Create the player

# Holds the level layout in a list of strings.
level = [
"WWWWWWWWWWWWWWWWW",
"W    W    W     W",
"W WW W WW   WWW W",
"W W  W  W W W W W",
"W WWWWW WWW W W W",
"W  W  W   W   W W",
"WW W WW WWWWWWW W",
"W    W   W      W",
"W WWWW W WWWWW WW",
"W               W",
"WWWWWWWWWWWWWWWEW",

]

# W = wall, E = exit
x = y = 0
for row in level:
for col in row:
if col == "W":
Wall((x, y))
if col == "E":
end_rect = pygame.Rect(x, y, 40, 40)
x += 40
y += 40
x = 0

reinit()

bigfont = pygame.font.Font(None, 80)
smallfont = pygame.font.Font(None, 45)

def play_again():
SCREEN_WIDTH = display_width
SCREEN_HEIGHT = display_height
screen = gameDisplay
text = bigfont.render('Play again?', 13, (0, 0, 0))
textx = SCREEN_WIDTH / 2 - text.get_width() / 2
texty = SCREEN_HEIGHT / 2 - text.get_height() / 2
textx_size = text.get_width()
texty_size = text.get_height()
pygame.draw.rect(screen, (255, 255, 255), ((textx - 5, texty - 5),
(textx_size + 10, texty_size +
10)))

screen.blit(text, (SCREEN_WIDTH / 2 - text.get_width() / 2,
SCREEN_HEIGHT / 2 - text.get_height() / 2))

#clock = pygame.time.Clock()
pygame.display.flip()
clock.tick(50)
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
elif event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
x, y = event.pos
if x >= textx - 5 and x <= textx + textx_size + 5:
if y >= texty - 5 and y <= texty + texty_size + 5:
return True

running = True
while running:

clock.tick(FPS)

for e in pygame.event.get():
if e.type == pygame.QUIT:
running = False
if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
running = False

# Move the player
key = pygame.key.get_pressed()
if key[pygame.K_LEFT]:
player.move(-2, 0)
if key[pygame.K_RIGHT]:
player.move(2, 0)
if key[pygame.K_UP]:
player.move(0, -2)
if key[pygame.K_DOWN]:
player.move(0, 2)

if player.rect.colliderect(end_rect):
again = play_again()
if again:
reinit()
else:
break

# Draw the scene
gameDisplay.fill((brown))
for wall in walls:
pygame.draw.rect(gameDisplay, green, wall.rect)
pygame.draw.rect(gameDisplay, red, end_rect)
pygame.draw.rect(gameDisplay, white, player.rect)
pygame.display.flip()

pygame.quit()
quit()
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.