Reputation: 27
So the idea for my game is the it will open onto a menu which has four options, Easy, Medium, Hard, and Exit. The Easy option will open the first level, the Medium open the second level, the Hard will open the third level, and the exit will quit the game.
If the user selects the Easy, Medium, or Hard option then it will take them to a maze (layout of maze will be made later) where the player spawns in the middle. In each corner of the maze is a green square and one of the green squares will turn red and the player will have 60 seconds to reach it. If the player reaches it in time then the players position, random number, and timer will be reset but with 10 seconds less than the previous time (50, 40, 30, ...). If the player doesn't reach it in time then text appears saying "You lose" along with your best time (e.g. "Your best time was: 24 seconds") for a couple of seconds before returning to the main menu.
I know this is a bit of a stretch but if someone could piece together what I have now with their own knowledge to create the game with the description above I would be eternally grateful. Please help.
import pygame, random, os, sys
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
YELLOW = (255, 200, 0)
pygame.init()
screen = pygame.display.set_mode((480, 320))
font = pygame.font.SysFont("Calibri", 20)
menu_font = pygame.font.Font(None, 40)
currentLevel = 0
pygame.display.set_caption("Mazer")
screen = pygame.display.set_mode((1040, 768))
clock = pygame.time.Clock()
rand = random.randint(1, 4)
class Player(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], 15, 15)
def move(self, dx, dy):
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):
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0:
self.rect.right = wall.rect.left
if dx < 0:
self.rect.left = wall.rect.right
if dy > 0:
self.rect.bottom = wall.rect.top
if dy < 0:
self.rect.top = wall.rect.bottom
# Nice class to hold a wall rect
class Wall(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Finish1(object):
def __init__(self, pos):
Finish1.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Finish2(object):
def __init__(self, pos):
Finish2.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Finish3(object):
def __init__(self, pos):
Finish3.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Finish4(object):
def __init__(self, pos):
Finish4.rect = pygame.Rect(pos[0], pos[1], 16, 16)
class Option:
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)
def set_rend(self):
self.rend = menu_font.render(self.text, True, self.get_color())
def get_color(self):
if self.hovered:
return (255, 255, 255)
else:
return (100, 100, 100)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
options = [Option("Easy", (140, 105)), Option("Medium", (135, 155)),
Option("Hard", (145, 205)), Option("Exit", (145, 255))]
def Menu():
runnin = True
while runnin:
clock.tick(60)
screen.fill(BLACK)
mouseclick = pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit(0)
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
if mouseclick[0] == 1:
if option.text == "Easy":
global currentlevel
currentlevel = 0
main()
elif option.text == "Medium":
currentlevel = 1
main()
elif option.text == "Hard":
currentlevel = 2
main()
else:
runnin = False
else:
option.hovered = False
option.draw()
#screen.blit(, (350, 50))
pygame.display.update()
pygame.quit()
sys.exit(0)
levels = [[
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WF GW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W P W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WH IW",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WF GW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W P W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WH IW",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"WF GW",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W P W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WH IW",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
]]
def load_level(level):
walls = []
players = []
finishes = []
x = y = 0
for row in levels[level]:
for col in row:
if col == "W":
walls.append(Wall((x, y)))
if col == "P":
players.append(Player((x, y)))
if col == "F":
finishes.append(Finish1((x, y)))
if col == "G":
finishes.append(Finish2((x, y)))
if col == "H":
finishes.append(Finish3((x, y)))
if col == "I":
finishes.append(Finish4((x, y)))
x += 16
y += 16
x = 0
return walls, players, finishes
walls, players, finishes = load_level(currentLevel)
def main():
frame_count = 0
frame_rate = 60
start_time = 60
running = True
while running:
clock.tick(60)
pygame.event.pump()
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
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
else:
option.hovered = False
option.draw()
total_seconds = start_time - (frame_count // frame_rate)
if total_seconds < 1:
#Draw "you lose" text on screen along with your best time for a couple of
#seconds then return to main menu
Menu()
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
text = font.render(output_string, True, WHITE)
screen.blit(text, [250, 280])
frame_count += 1
clock.tick(frame_rate)
# Move the player if an arrow key is pressed
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)
for player in players:
if player.rect.colliderect(Finish1.rect) and rand == 1:
total_seconds = best_time
start_time -= 10
#reset the timer to start_time - 10, reset the random integer, and reset player somehow
if player.rect.colliderect(Finish2.rect) and rand == 2:
total_seconds = best_time
start_time -= 10
#reset the timer to start_time - 10, reset the random integer, and reset player somehow
if player.rect.colliderect(Finish3.rect) and rand == 3:
total_seconds = best_time
start_time -= 10
#reset the timer to start_time - 10, reset the random integer, and reset player somehow
if player.rect.colliderect(Finish4.rect) and rand == 4:
total_seconds = best_time
start_time -= 10
#reset the timer to start_time - 10, reset the random integer, and reset player somehow
screen.fill((BLACK))
for wall in walls:
pygame.draw.rect(screen, (WHITE), wall.rect)
for player in players:
pygame.draw.rect(screen, (YELLOW), player.rect)
pygame.draw.rect(screen, (GREEN), finish.rect)
if rand == 1:
pygame.draw.rect(screen, (RED), Finish1.rect)
elif rand == 2:
pygame.draw.rect(screen, (RED), Finish2.rect)
elif rand == 3:
pygame.draw.rect(screen, (RED), Finish3.rect)
else:
pygame.draw.rect(screen, (RED), Finish4.rect)
pygame.display.flip()
if __name__ == '__main__':
main()
Upvotes: 0
Views: 155
Reputation: 27
import pygame, random, os, sys
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
YELLOW = (255, 200, 0)
GREY = (100, 100, 100)
pygame.init()
screen = pygame.display.set_mode((480, 320))
font = pygame.font.SysFont("Calibri", 50)
help_font = pygame.font.SysFont("Calibri", 30)
title_font = pygame.font.SysFont("Segoe", 150)
menu_font = pygame.font.Font(None, 80)
currentLevel = 0
pygame.display.set_caption("Amazeing Race")
screen = pygame.display.set_mode((1024, 768))
clock = pygame.time.Clock()
class Player(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], 27, 27)
def move(self, dx, dy):
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):
self.rect.x += dx
self.rect.y += dy
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0:
self.rect.right = wall.rect.left
if dx < 0:
self.rect.left = wall.rect.right
if dy > 0:
self.rect.bottom = wall.rect.top
if dy < 0:
self.rect.top = wall.rect.bottom
class Wall(object):
def __init__(self, pos):
self.rect = pygame.Rect(pos[0], pos[1], 41, 31)
class Finish1(object):
def __init__(self, pos):
Finish1.rect = pygame.Rect(pos[0], pos[1], 41, 31)
class Finish2(object):
def __init__(self, pos):
Finish2.rect = pygame.Rect(pos[0], pos[1], 41, 31)
class Finish3(object):
def __init__(self, pos):
Finish3.rect = pygame.Rect(pos[0], pos[1], 41, 31)
class Finish4(object):
def __init__(self, pos):
Finish4.rect = pygame.Rect(pos[0], pos[1], 41, 31)
class Option:
hovered = False
def __init__(self, text, pos):
self.text = text
self.pos = pos
self.set_rect()
self.draw()
def draw(self):
self.set_rend()
screen.blit(self.rend, self.rect)
def set_rend(self):
self.rend = menu_font.render(self.text, True, self.get_color())
def get_color(self):
if self.hovered:
return (WHITE)
else:
return (GREY)
def set_rect(self):
self.set_rend()
self.rect = self.rend.get_rect()
self.rect.topleft = self.pos
options = [Option("Easy", (450, 205)), Option("Medium", (450, 305)),
Option("Hard", (450, 405)), Option("Help", (450, 505)), Option("Exit", (450, 605))]
levels = [[
"WWWWWWWWWWWWWWWWWWWWWWWWW",
"WFW W GW",
"W W WWW W WWW W W WWW WWW",
"W W W W W W W",
"W WWWWW WWWWW W W WWWWW W",
"W W W W W W W W",
"W WWWWW W WWWWWWW W W W W",
"W W W W W W W W",
"WWWWW WWWWW WWW W W W W W",
"W W W W W W W W W",
"W WWW W WWW W WWWWWWW W W",
"W W W W W",
"W WWWWW WWWWP WWW W WWW W",
"W W W W W",
"W WWWWWWWWW W W WWW WWWWW",
"W W W W W W",
"WWWWWWW W WWW W WWWWWWW W",
"W W W W W W W W",
"WWW W WWW W WWW W WWW W W",
"W W W W W W W",
"WWWWW W WWW W W WWW W WWW",
"W W W W W W W",
"W W WWWWW WWWWW WWWWWWW W",
"WHW W WIW",
"WWWWWWWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWWWWWWW",
"WFW W W GW",
"W W WWW W WWW W W WWW WWW",
"W W W W W W W",
"W WWWWW WWWWW W W WWWWW W",
"W W W W W W W W",
"W WWWWW W WWWWWWW W W W W",
"W W W W W W W W",
"WWWWWWWWWWW WWW W W W W W",
"W W W W W W W W",
"W WWW W WWW W WWWWWWW W W",
"W W W W W",
"WWWWWWW WWWWP WWW W WWW W",
"W W W W W",
"W WWWWWWWWW W W WWW WWWWW",
"W W W W W W",
"WWWWWWW W WWW W WWWWWWW W",
"W W W W W W W W",
"WWW W WWW W WWW W WWW W W",
"W W W W W W W W",
"WWWWW W WWWWW W WWW W WWW",
"W W W W W W W",
"W W WWWWW WWWWW WWWWWWW W",
"WHW W WIW",
"WWWWWWWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWWWWWWW",
"WFW W W W GW",
"W W WWW W WWW W W WWW WWW",
"W W W W W W W",
"W WWWWW WWWWW W W WWWWW W",
"W W W W W W W W",
"W WWWWW W WWWWWWW W W W W",
"W W W W W W W W",
"WWWWWWWWWWW WWW W W W W W",
"W W W W W W W W",
"W WWW W WWW W WWWWWWW W W",
"W W W W W",
"WWWWWWW WWWWP WWW W WWW W",
"W W W W W",
"W WWWWWWWWW W W WWW WWWWW",
"W W W W W W",
"WWWWWWW W WWW W WWWWWWW W",
"W W W W W W W W W",
"WWW W WWW W WWW W W W W W",
"W W W W W W W W",
"WWWWW W WWWWW W WWW W WWW",
"W W W W W W W",
"W W WWWWW WWWWW WWWWWWW W",
"WHW W WIW",
"WWWWWWWWWWWWWWWWWWWWWWWWW",
]]
def load_level(level):
walls = []
players = []
finishes = []
x = y = 0
for row in levels[level]:
for col in row:
if col == "W":
walls.append(Wall((x, y)))
if col == "P":
players.append(Player((x, y)))
if col == "F":
finishes.append(Finish1((x, y)))
if col == "G":
finishes.append(Finish2((x, y)))
if col == "H":
finishes.append(Finish3((x, y)))
if col == "I":
finishes.append(Finish4((x, y)))
x += 40.96
y += 30.72
x = 0
return walls, players, finishes
walls, players, finishes = load_level(currentLevel)
def Menu():
global currentlevel, walls, players, finishes
runnin = True
while runnin:
clock.tick(60)
screen.fill(BLACK)
mouseclick = pygame.mouse.get_pressed()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit(0)
for option in options:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
if mouseclick[0] == 1:
if option.text == "Easy":
walls, players, finishes = load_level(0)
currentlevel = 0
main()
elif option.text == "Medium":
walls, players, finishes = load_level(1)
currentlevel = 1
main()
elif option.text == "Hard":
walls, players, finishes = load_level(2)
currentlevel = 2
main()
elif option.text == "Help":
Help()
else:
runnin = False
else:
option.hovered = False
option.draw()
screen.blit(title_font.render("A-maze-ing Race", True, GREY), (100, 50))
pygame.display.update()
pygame.quit()
sys.exit(0)
def Help():
runnin = True
option = Option("Back", (20, 700))
while runnin:
clock.tick(60)
screen.fill(BLACK)
mouseclick = pygame.mouse.get_pressed()
for e in pygame.event.get():
if e.type == pygame.QUIT:
pygame.quit()
sys.exit(0)
if e.type == pygame.KEYDOWN:
if e.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit(0)
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered = True
if mouseclick[0] == 1:
if option.text == "Back":
Menu()
else:
option.hovered = False
option.draw()
screen.blit(help_font.render("First, select a level: Easy, Medium, or Hard", True, WHITE), (10, 20))
screen.blit(help_font.render("Easy - More manoeverable map, no sight restrictions.", True, WHITE), (10, 50))
screen.blit(help_font.render("Medium - More constricted map, vision of walls disappears after 8 seconds.", True, WHITE), (10, 80))
screen.blit(help_font.render("Hard - Slighty different and constricted map layout, vision of walls disappears after", True, WHITE), (10, 110))
screen.blit(help_font.render("3 seconds.", True, WHITE), (10, 140))
screen.blit(help_font.render("Once in the level you will have 10 seconds to prepare before one of the 4 green", True, WHITE), (10, 180))
screen.blit(help_font.render("blocks in the corner turns red and the countdown starts. Then you must navigate", True, WHITE), (10, 210))
screen.blit(help_font.render("through the maze and reach the red block before the time runs out. If you", True, WHITE), (10, 240))
screen.blit(help_font.render("reach the red block in time when the timer started at 10 seconds then you win ", True, WHITE), (10, 270))
screen.blit(help_font.render("the game.", True, WHITE), (10, 300))
pygame.display.update()
def main():
option = Option("Menu", (78, 697))
pygame.display.update()
rand = random.randint(1, 4)
frame_count = 0
frame_rate = 60
start_time = 50
prep_time = 10
best_time = 400
if currentlevel == 1:
show_time = 7 + prep_time
else:
show_time = 3 + prep_time
total_seconds = 50
show_seconds = 25
running = True
while running:
mouseclick = pygame.mouse.get_pressed()
prep_seconds = prep_time - (frame_count // frame_rate)
minutes = prep_seconds // 60
seconds = prep_seconds % 60
output_string = "Preparation time: {0:02}:{1:02}".format(minutes, seconds)
if prep_seconds < 0:
show_seconds = show_time - (frame_count // frame_rate)
total_seconds = start_time - (frame_count // frame_rate)
minutes = total_seconds // 60
seconds = total_seconds % 60
output_string = "Time left: {0:02}:{1:02}".format(minutes, seconds)
if total_seconds > 0:
frame_count += 1
clock.tick(frame_rate)
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
option.hovered = True
if option.rect.collidepoint(pygame.mouse.get_pos()):
if mouseclick[0] == 1:
if option.text == "Menu":
Menu()
key = pygame.key.get_pressed()
if total_seconds > 0:
if key[pygame.K_LEFT]:
player.move(-3, 0)
if key[pygame.K_RIGHT]:
player.move(3, 0)
if key[pygame.K_UP]:
player.move(0, -3)
if key[pygame.K_DOWN]:
player.move(0, 3)
for player in players:
if player.rect.colliderect(Finish1.rect) and rand == 1 or player.rect.colliderect(Finish2.rect) and rand == 2 or player.rect.colliderect(Finish3.rect) and rand == 3 or player.rect.colliderect(Finish4.rect) and rand == 4:
if prep_seconds < 0:
show_seconds = 25
frame_count = 0
if total_seconds < best_time:
best_time = start_time - prep_time - total_seconds
prep_seconds = prep_time - (frame_count // frame_rate)
minutes = prep_seconds // 60
seconds = prep_seconds % 60
output_string = "Preparation time: {0:02}:{1:02}".format(minutes, seconds)
start_time -= 10
rand = random.randint(1, 4)
screen.fill(BLACK)
if currentlevel == 2 or currentlevel == 1:
if show_seconds > 0:
for wall in walls:
pygame.draw.rect(screen, (GREY), wall.rect)
else:
for wall in walls:
pygame.draw.rect(screen, (GREY), wall.rect)
pygame.draw.rect(screen, (GREEN), Finish1.rect)
pygame.draw.rect(screen, (GREEN), Finish2.rect)
pygame.draw.rect(screen, (GREEN), Finish3.rect)
pygame.draw.rect(screen, (GREEN), Finish4.rect)
if prep_seconds < 0:
if rand == 1:
pygame.draw.rect(screen, (RED), Finish1.rect)
elif rand == 2:
pygame.draw.rect(screen, (RED), Finish2.rect)
elif rand == 3:
pygame.draw.rect(screen, (RED), Finish3.rect)
else:
pygame.draw.rect(screen, (RED), Finish4.rect)
for player in players:
pygame.draw.rect(screen, (YELLOW), player.rect)
screen.blit(font.render(output_string, True, GREEN), (350, 700.04))
if start_time == 10:
screen.fill(BLACK)
prep_seconds = 0
total_seconds = 0
minutes = 0
seconds = 0
screen.blit(font.render("YOU WIN", True, GREEN), (440, 500))
screen.blit(font.render("Your best time was " + str(best_time)+" seconds", True, GREEN), (260, 600))
screen.blit(font.render("Click to return to the Main Menu", True, GREEN), (250, 700))
if mouseclick[0] == 1 or mouseclick[1] == 1:
running = False
elif total_seconds < 0.1:
screen.fill(BLACK)
prep_seconds = 0
total_seconds = 0
minutes = 0
seconds = 0
screen.blit(font.render("YOU LOSE", True, GREEN), (440, 500))
if best_time == 400:
screen.blit(font.render("You did not touch a block.", True, GREEN), (280, 600))
else:
screen.blit(font.render("Your best time was " + str(best_time)+" seconds", True, GREEN), (260, 600))
screen.blit(font.render("Click to return to the Main Menu", True, GREEN), (250, 700))
if mouseclick[0] == 1 or mouseclick[1] == 1:
running = False
option.draw()
pygame.display.flip()
Menu()
Upvotes: 1