Bug while running program in Pygame

Asked

Viewed 36 times

0

I have a problem creating the game menu. What happens is that when I create the function draw_text, the program is on black screen and I notice that it shows nothing on the screen causing it not to close the tab of the same.

I’ll leave the code for you to analyze and talk about if there’s something wrong that I did.

import pygame
from pygame.locals import *
from sys import exit
import random

pygame.init()
fonte = pygame.font.SysFont(None, 20)

def sentings()->int:
    global largura, altura
    largura = 600
    altura = 450
    return largura, altura

def draw_text(text, font, color, surface, x, y):
    textobj = font.render(text, 1, color)
    textrect = textobj.get_rect()
    textrect.topleft = (x, y)
    surface.blit(textobj, textrect)



def menu():
    while True:
        tela = pygame.display.set_mode((sentings()))
        tela.fill((0, 0, 0))
        draw_text('Menu', fonte, (200, 200, 200), tela, 20, 20)

def main():
    global tela
    tela = pygame.display.set_mode((sentings()))
    pygame.display.set_caption('jogo')
    x = largura/2
    y = altura/2
    relogio = pygame.time.Clock()
    while True:
        menu()
        relogio.tick(30)
        tela.fill((0, 0, 0))

        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                exit()

        if pygame.key.get_pressed()[K_a]:
            x = x - 20
        if pygame.key.get_pressed()[K_d]:
            x = x + 20
        if pygame.key.get_pressed()[K_w]:
            y = y - 20
        if pygame.key.get_pressed()[K_s]:
            y = y + 20

        choice = random.randint(100,250)
        red_ret = pygame.draw.rect((tela), (255, 0,0), (x,y, 40, 50))
        white_ret = pygame.draw.rect((tela), (200, 0, 0), (0, 0, 40, choice))

        pygame.display.update()


if __name__ == '__main__':
    main()
  • What is the function of while True in function menu?

  • Just to call the function before entering the game, I tried to start it down there with the main, but she charged another bug other than this. where the screen in the appeared the name

  • But if it has an infinite loop, it will always run this function and will not run the rest of the code. It makes sense that?

  • no, but I can’t put to run this function I created.

  • Just remove the while True and the tela.fill((0, 0, 0)) of function menu(). And put the line on which you call the function menu() after the line tela.fill((0, 0, 0)) within the function main(). What you’re doing is basically erasing what was written.

  • same thing, nothing has changed.

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.