在过去的几天里,我一直在解决代码问题,因为我一直试图在pygame代码中添加启动屏幕,所以我已经整理了很多错误,但我无法弄清楚出了什么问题与这个。
我在代码中使用了一个不活动的变量名称来调用它,以表示用于屏幕上“按钮”的颜色。
Traceback (most recent call last):
File "D:\Computing Homework\Programming Projects\pong development.py", line 8, in <module>
gameDisplay = pygame.display.set_mode([display_width,display_height,inactive,active])
NameError: name 'inactive' is not defined
我试图在整个代码中的不同位置定义非活动状态,但是没有进步,我在代码顶部定义了它,并且我停止获取错误消息,但这也意味着当我运行代码时,什么都没有出现甚至没有显示启动屏幕的窗口,并且抛出0错误。
下面是使用非活动状态的代码:
def button(msg,x,y,width,height,active,inactive,action=None):
mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if ((x+width > mouse[0]) > x and (y+height > mouse [1] > y)):
pygame.draw.rect(gameDisplay, active,[x,y,width,height])
if click[0] == 1 and action != None:
if action == "play 1":
game_loop_one()
elif action == "play 2":
game_loop_two()
if x+width > mouse[0] > x and y+height > mouse[1] > y:
pygame.draw.rect(gameDisplay, active,(x,y,width,height))
else:
pygame.draw.rect(gameDisplay, inactive,(x,y,width,height))
smallText = pygame.font.Font("freesansbold.ttf",20) #defining the small text to be used on the buttons.
textSurface, textRect = text_objects(msg, smallText)
textRect.center = ( (x+(width/2)), (y+hight/2))
gameDisplay.blit(textSurface, textRect)
这是程序顶部的功能,以防需要解决此问题:
display_width = 640
display_height = 480
gameDisplay = pygame.display.set_mode([display_width,display_height,inactive,active])
pygame.display.set_caption('Ethong')
fps = 60
pygame.font.init()
font = pygame.font.Font('freesansbold.ttf',115)
clock = pygame.time.Clock()
pygame.display.set_caption('ethong')
green = (0,200,0)#base colours.
white = (255,255,255)#base colours.
black = (0,0,0)#base colours.
red = (255,0,0)#base colours.
blue = (0,0,255)#base colours.
bright_green =(0,255,0)#defining colours this will be used to make the button 'glows' when hoovered over.
我希望开始屏幕运行,然后显示一个带有游戏名称和两个按钮的开始屏幕,这些按钮将变为绿色,然后将鼠标悬停在它们上时,它们将变为亮绿色。
相反,我因为未定义非活动状态而陷入错误。