0

我正在用 python 和 pygame 编写游戏,我试图创建一个主菜单,但我的标签不会显示????我也收到这个错误:第 22 行,在 x,y = pygame.mouse.get_pos( ) 错误:视频系统未初始化???我不明白这里发生了什么,因为我对 python 很陌生,这是我的代码:

bif="menubg.jpg"
load_check_x = "null"
load_check_y = "null"
import pygame, sys
from pygame.locals import *
x = 0
y = 0


pygame.init()
screen=pygame.display.set_mode((640,360),0,32)
background=pygame.image.load(bif).convert()
pygame.display.set_caption("Some random Title")
pygame.display.flip()
#screen.fill((0,0,0))

while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
x,y = pygame.mouse.get_pos()
#Setup mouse pos!!!
if x >= 340 and x <= 465:
    load_check_x = "True"
if x < 340 or x > 465:
    load_check_x = "False"

if y >= 425 and y <= 445:
    load_check_y = "True"
if y < 425 or y > 445:
    load_check_y = "False"

if load_check_x == "True" and load_check_y == "True":
   for event in evc:
       if event.type ==pygame.MOUSEBUTTONUP:
           clear()
labelfont = pygame.font.SysFont("Comic Sans MS", 12)
new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
screen.blit(new_text, (340, 425))

谁来帮帮我!!!

4

1 回答 1

0

看来你的缩进是错误的。python 解释器如何知道 if 语句主体何时结束?它查看制表符或空格的数量,以查看后面的代码是否属于正文。

如果您查看您的代码,您会看到您有一些执行初始化的代码,然后是一个while循环和另一个处理您的事件的循环。之后你有一些检查鼠标等的代码。

正如你所看到的,事件循环之后的所有代码,都不属于while循环,它应该。除此之外,您可以通过创建新矩形并检查您单击的点是否位于矩形内来替换所有 if 条件。

my_rect = pygame.Rect((340,425),(125,20))
while True:
    evc = pygame.event.get()
    for event in evc:
        if event.type == pygame.QUIT:
            pygame.quit()       
    x,y = pygame.mouse.get_pos()
    #Setup mouse pos!!!
    if my_rect.collidepoint(x,y):
       for event in evc:
           if event.type ==pygame.MOUSEBUTTONUP:
               clear()
    labelfont = pygame.font.SysFont("Comic Sans MS", 12)
    new_text = labelfont.render('Play!!!!', 1, (255, 255, 255))
    screen.blit(new_text, (340, 425))
于 2014-01-31T20:05:01.983 回答