![](/img/trans.png)
[英]Pygame game doesn't work after exporting to .exe with pyinstaller
[英]Pygame game doesn't work
因此,對於我的游戲,其主意是它將打開一個包含四個選項的菜單,分別為Easy,Medium,Hard和Exit。 “簡單”選項將打開第一級,“中”將打開第二級,“硬”將打開第三級,退出將退出游戲。
如果用戶選擇“簡單”,“中”或“硬”選項,則會將它們帶入迷宮(稍后會進行迷宮布局),玩家會在中間出現。 迷宮的每個角落都有一個綠色方塊,其中一個綠色方塊會變成紅色,玩家將有60秒的時間到達它。 如果玩家及時到達,則玩家位置,隨機數和計時器將被重置,但比之前的時間(50、40、30,...)短10秒。 如果玩家沒有及時到達,則出現幾秒鍾的文字“您輸了”以及您的最佳時間(例如,“您的最佳時間是:24秒”),然后返回主菜單。
我知道這有點困難,但是如果有人可以用自己的知識將我現在擁有的東西拼湊起來,用上面的描述來創建游戲,我將永遠感激不已。 請幫忙。
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()
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.