因此,想法是游戏将打开菜单,用户可以选择一个级别,简单,中等,硬或退出游戏。现在所发生的一切都是游戏打开到我的游戏大小(1024,768)的黑屏,其中关闭,最大化和最小化是不可点击的。非常感谢任何帮助。
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()
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 Finish(object):
def __init__(self, pos):
self.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():
running2 = True
while running2:
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:
running2 = False
else:
option.hovered = False
option.draw()
#screen.blit(, (350, 50))
pygame.display.update()
pygame.quit()
sys.exit(0)
levels = [[
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
"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",
"W F W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWW",
"W W",
"W W",
"W WWWWWWWWWWWWW W",
"W W",
"W P W",
"W W",
"W WWWWWWWWWWWWW W",
"W W",
"W WWWWWWWWWWWWW W",
"W W",
"W F W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWW",
],
[
"WWWWWWWWWWWWWWWWWWWW",
"W W",
"W W",
"W W",
"W W",
"W P W",
"W W",
"W W",
"W W",
"W W",
"W W",
"W F W",
"W W",
"W W",
"WWWWWWWWWWWWWWWWWWWW",
]]
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(Finish((x, y)))
x += 16
y += 16
x = 0
return walls, players, finishes
walls, players, finishes = load_level(currentLevel)
running = True
def main():
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()
# 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:
for finish in finishes:
if player.rect.colliderect(finish.rect):
currentLevel += 1
walls, players, finishes = load_level(currentLevel)
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)
for finish in finishes:
pygame.draw.rect(screen, (GREEN), finish.rect)
pygame.display.flip()
答案 0 :(得分:3)
你的程序中没有任何一点是你启动main()函数或其他任何事情。
许多python脚本底部的常见部分是:
if __name__ == '__main__':
main()
如果脚本是顶级python文件,则用于启动所有内容
在底部包含此内容,这将帮助您入门。
一旦你完成了这项工作,你会发现在函数中调用的许多变量都是本地的(我猜测整个问题是基于你编写程序的事实然后试图稍后将所有内容联系起来)所以你需要将它们声明为全局变量,但这超出了这个问题的范围。
希望这有帮助。
答案 1 :(得分:1)
要将它们声明为全局变量,请在以下行之后写下:def main():
:
global running, players, finishes, walls, currentLevel