1
這是我開始用來製作開始菜單的代碼。如何將文本添加到這些矩形?
# we need some colours!!
black = (0,0,0)
white = (255,255,200)
red = (200,0,0)
green = (0, 200, 0)
bright_red = (255, 0 ,0)
bright_green = (0, 255, 0)
bright_white = (255, 255, 255)
def main():
pygame.init()
screen = pygame.display.set_mode((600, 600))
pygame.display.set_caption("CrazyPongMainMenu")
menu = cMenu(50, 50, 20, 5, "vertical", 100, screen,
[("Start Game", 1, None),
("Options", 2, None),
("Exit", 3, None)])
menu.set_center(True, True)
menu.set_alignment("center", "center")
state = 0
prev_state = 1
rect_list = []
pygame.event.set_blocked(pygame.MOUSEMOTION)
while 1:
if prev_state != state:
pygame.event.post(pygame.event.Event(EVENT_CHANGE_STATE, key = 0))
prev_state = state
e = pygame.event.wait()
if e.type == pygame.KEYDOWN or e.type == EVENT_CHANGE_STATE:
if state == 0:
rect_list, state = menu.update(e, state)
elif state == 1:
print ("Start Game!")
state = 0
elif state == 2:
print ("Options!")
state = 0
else:
print ("Exit!")
pygame.quit()
sys.exit()
if e.type == pygame.QUIT:
pygame.quit()
sys.exit()
mouse = pygame.mouse.get_pos()
#print(mouse)
if 200+150 > mouse[0] > 200 and 250+30 > mouse[1] > 250:
pygame.draw.rect(screen, bright_green,(200, 250, 150, 30))
else:
pygame.draw.rect(screen, green,(200, 250, 150, 30))
if 200+150 > mouse[0] > 200 and 290+21 > mouse[1] > 290:
pygame.draw.rect(screen, bright_white,(200, 290, 150, 21))
else:
pygame.draw.rect(screen, white,(200, 290, 150, 21))
if 200+150 > mouse[0] > 200 and 318+25 > mouse[1] > 318:
pygame.draw.rect(screen, bright_red,(200, 318, 150, 25))
else:
pygame.draw.rect(screen, red,(200, 318, 150, 25))
pygame.display.update(rect_list)
if __name__ == ("__main__"):
main()
我想把文本放到3個矩形上,但我不知道如何去做。如果有人可以請告訴我如何把文字放在矩形上,這將是非常感謝!
謝謝我認爲這解決了我的問題! – RedNax