pygame菜单不起作用?

时间:2014-02-02 19:54:53

标签: python python-3.x pygame

对于以下代码,存在问题。我正在为我的游戏创建一个主菜单。有两个选项“开始游戏”和“退出”。我遇到的问题是,当我想点击开始游戏时,没有任何反应。

我收到一条错误消息:“'module'对象没有属性'start'。我不知道这意味着什么。

我有另一个名为“bs”(泡泡射手)。这是我的主要代码文件。当我点击菜单文件的开头时,如何让它继续播放bs文件来玩游戏?谢谢你的帮助!

import pygame, os, random, math

WINDOWWIDTH  = 640
WINDOWHEIGHT = 480
STARTX = WINDOWWIDTH / 2
STARTY = WINDOWHEIGHT - 27
BLUE = (0,0,205)
WHITE = (255,255,255)
BLUE = (0,0,205)
upper_border = pygame.Rect(12,44,448,20) #done this
right_border = pygame.Rect(440,60,20,448) #done this
left_border = pygame.Rect(12,60,20,648) # done this
down_border = pygame.Rect(12,280,448,20) #done this

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

pygame.init()
screen = pygame.display.set_mode((480, 320))
menu_font = pygame.font.Font(None, 40)
options = [Option("START GAME", (140, 105)), Option("QUIT", (205, 205))]
while True:
    pygame.event.pump()
    for option in options:
     if option.rect.collidepoint(pygame.mouse.get_pos()):
        option.hovered = True
    else:
        option.hovered = False
    option.draw()
    for event in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
        for option in options:
            if option.hovered and option.text == "START GAME":
                pygame.start()


pygame.display.update()
screen.fill((0,0,0))
pygame.draw.rect(screen,BLUE,upper_border)
pygame.draw.rect(screen,BLUE,right_border)
pygame.draw.rect(screen,BLUE,left_border)
pygame.draw.rect(screen,BLUE,down_border)
pygame.display.update()

1 个答案:

答案 0 :(得分:1)

错误消息引用此行:

pygame.start()

此处pygame是一个模块,没有属性start。相反,您需要调用游戏其余部分所使用的任何功能,例如:

import bs

...

if option.hovered and option.text == "START GAME":
    bs.start()