python - Pygame 游戏未启动

标签 python pygame

所以我们的想法是,游戏将打开菜单,用户可以在其中选择一个级别,简单、中等、困难,或者退出游戏。现在所有发生的事情是游戏打开时出现黑屏,大小与我的游戏大小 (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() 

最佳答案

在您的程序中,您绝不会启动 main() 函数或与此相关的任何其他内容。

许多 python 脚本底部的常见部分是:

if __name__ == '__main__':
    main()

如果脚本是顶级 python 文件,这用于启动所有内容

将其包含在底部,这将帮助您入门。

完成此操作后,您会发现在函数中调用的许多变量都是本地变量(我猜整个问题是基于您编写程序然后尝试将所有内容绑定(bind)到函数中这一事实) 所以你需要将它们声明为全局变量,但这超出了这个问题的范围。

希望这对您有所帮助。

关于python - Pygame 游戏未启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37592626/

相关文章:

python - 为什么 Python 中可能有一个 except 子句来处理未定义的异常?

python - 计算与端点在线上的点

python - 如何将文本输出到屏幕,同时输出面向对象的按钮 - Pygame

python - 如何解决pygame闪烁问题

python - Pygame:按住键时玩家不会移动

python - 生成器字典内的迭代变量

python - 如何在 Python 中创建从特定日期到另一个相隔 6 个月的日期范围?

python - Pygame 如何使用 LayeredUpdates() 更改图像层

python - Python字典的推荐用法,函数作为值

python - 使用python批量上传到谷歌云