Python 游戏没有开始
因此,游戏将打开一个菜单,用户可以选择一个级别,简单,中等,硬,或退出游戏。现在,所有发生的事情是,游戏打开到一个黑屏大小的游戏(1024768),其中关闭,最大化和最小化是不可点击的。非常感谢您的帮助Python 游戏没有开始,python,pygame,Python,Pygame,因此,游戏将打开一个菜单,用户可以选择一个级别,简单,中等,硬,或退出游戏。现在,所有发生的事情是,游戏打开到一个黑屏大小的游戏(1024768),其中关闭,最大化和最小化是不可点击的。非常感谢您的帮助 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,
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()
导入pygame、random、os、sys
黑色=(0,0,0)
白色=(255,255,255)
蓝色=(0,0255)
绿色=(0,255,0)
红色=(255,0,0)
紫色=(255,0255)
黄色=(255200,0)
pygame.init()
screen=pygame.display.set_模式((480320))
font=pygame.font.SysFont(“Calibri”,20)
menu_font=pygame.font.font(无,40)
currentLevel=0
pygame.display.set_标题(“Mazer”)
screen=pygame.display.set_模式((1040768))
clock=pygame.time.clock()
类播放器(对象):
定义初始(自我,位置):
self.rect=pygame.rect(位置[0],位置[1],15,15)
def移动(自身、dx、dy):
如果dx!=0:
自移动单轴(dx,0)
如果是dy!=0:
自移动单轴(0,dy)
def move_单轴(自、dx、dy):
self.rect.x+=dx
自校正y+=dy
对于墙中墙:
如果self.rect.collide rect(wall.rect):
如果dx>0:
self.rect.right=wall.rect.left
如果dx<0:
self.rect.left=wall.rect.right
如果dy>0:
self.rect.bottom=wall.rect.top
如果dy<0:
self.rect.top=wall.rect.bottom
#这是一个很好的课堂,可以举行一次壁式辩论
类墙(对象):
定义初始(自我,位置):
self.rect=pygame.rect(位置[0],位置[1],16,16)
类完成(对象):
定义初始(自我,位置):
self.rect=pygame.rect(位置[0],位置[1],16,16)
类别选项:
悬停=错误
定义初始(自我、文本、位置):
self.text=文本
self.pos=pos
self.set_rect()
self.draw()
def牵引(自):
self.set_rend()
screen.blit(self.rend、self.rect)
def set_rend(自):
self.rend=menu\u font.render(self.text,True,self.get\u color())
def get_颜色(自身):
如果self.hover:
返回(255、255、255)
其他:
返回(100100100)
def设置(自):
self.set_rend()
self.rect=self.rend.get_rect()
self.rect.topleft=self.pos
选项=[选项(“容易”(140105)),选项(“中等”(135155)),
选项(“硬的”(145205)),选项(“退出”(145255))]
def菜单():
running2=真
运行2时:
时钟滴答(60)
屏幕填充(黑色)
mouseclick=pygame.mouse.get_pressed()
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
系统出口(0)
如果event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
pygame.quit()
系统出口(0)
对于选项中的选项:
if option.rect.collidepoint(pygame.mouse.get_pos()):
option.hovered=True
如果鼠标单击[0]==1:
如果option.text==“简单”:
全局电流级
currentlevel=0
main()
elif option.text==“中等”:
currentlevel=1
main()
elif option.text==“硬”:
currentlevel=2
main()
其他:
running2=错误
其他:
option.hovered=False
option.draw()
#屏幕光点(,(350,50))
pygame.display.update()
pygame.quit()
系统出口(0)
级别=[[
“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW,
“W”,
“W”,
“W”,
“W P W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W F W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“W”,
“WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW,
],
[
“wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww”,
“W”,
“W”,
“wwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwwww”,
“W”,
“W
if __name__ == '__main__':
main()
global running, players, finishes, walls, currentLevel