I've ben programming a game and everything works so now I want is a screen before the game starts. You must then click on start to proceed to the game. So far I have this code:
if menu == 'start':
screen.fill((0,255,0))
txt = font.render('Start',True,(255,255,0))
txt_x = 480
txt_y = 290
buttonrect = pygame.Rect((txt_x,txt_y),txt.get_size())
pygame.draw.rect(screen,(0,0,255),buttonrect)
screen.blit(txt,(txt_x,txt_y))
if pygame.mouse.get_pressed()[0] and buttonrect.collidepoint(pygame.mouse.get_pos()):
menu = 'game'
I've set menu to 'start' but it doesn't work. The screen is black instead of green. But when I click on the coordinates, I go to the game. What is wrong with my code?
You need to update the main display after setting the screen background :
if menu == 'start':
screen.fill((0,255,0))
txt = font.render('Start',True,(255,255,0))
txt_x = 480
txt_y = 290
buttonrect = pygame.Rect((txt_x,txt_y),txt.get_size())
pygame.draw.rect(screen,(0,0,255),buttonrect)
screen.blit(txt,(txt_x,txt_y))
pygame.display.update()
if pygame.mouse.get_pressed()[0] and buttonrect.collidepoint(pygame.mouse.get_pos()):
menu = 'game'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.