[Python] Text only displays when the last button is pressed in pygame [duplicate]

Discussão em 'Python' iniciado por Stack, Março 6, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to build a turn based RPG and have been facing issues with the code. So the issue is basically after the user clicks on a button, it should display the enemy action and wait for user input(another mouse click) to display the buttons again. However the enemy action text is only displayed for the last button for some reason and I have not been able to figure out the issue with the code. The buttons are but in a dictionary where the key is the button name and value is the rect details. I have put down the main pygame code, I am using multiple files to perform various tasks if required I can link the git repository
    Thanks for the help in advance

    button={}
    side="player"
    hov="NA"
    def buttons():
    global hov,side
    mouse=pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    hov="NA"
    for x,y in button.items():
    if side=="player":
    if y[0]+y[2]>mouse[0]>y[0] and y[1]+y[3]>mouse[1]>y[1]:
    hov=x
    if click[0]==1:
    side="monster"
    game.battle.plturn(game,x)
    elif side=="monster":
    if click[0]==1:
    game.battle.mturn(game)
    if click[0]==1:
    side="player"
    class Sprites:
    def __init__(self,screen):
    self.screen=screen;
    self.texttemp=Textboxes(game,self.screen)
    self.buttonsprite=Buttons(game,self.screen)
    def update(self):
    self.buttonsprite.updatecolor(hov);
    self.texttemp.update(game)
    def drawbox(self):
    global side
    self.texttemp.infobox()
    if side=="monster":
    self.texttemp.monsterside()
    if side=="player":
    self.buttonsprite.drawbutton()

    class Game:
    def __init__(self):
    self.screen=pygame.display.set_mode((Win_Width,Win_Height))
    self.clock=pygame.time.Clock()
    self.running=True

    def maketextrects(self,x,y,width,height):
    return pygame.Rect(x,y,width,height)
    def makerects(self,variable,x,y,width,height):
    button[variable]=[x,y,width,height]
    return pygame.Rect(x,y,width,height)
    def drawshapes(self,color,x,y,width,height,linewidth):
    if(linewidth==0):
    pygame.draw.rect(self.screen,color,(x,y,width,height))
    else:
    pygame.draw.rect(self.screen,color,(x,y,width,height),linewidth)
    def drawtext(self,text,color,rect):
    font=pygame.font.Font('fonts/Game-Font.ttf',25)
    text_surface=font.render(text,True,color)
    text_rect=text_surface.get_rect(center=rect.center)
    self.screen.blit(text_surface,text_rect)
    def drawmovingtext(self,text,color,rect):
    font=pygame.font.Font('fonts/OwreKynge.ttf',35)
    text_surface=font.render(text,True,color)
    text_rect=text_surface.get_rect(topleft=(50,440))
    self.screen.blit(text_surface,text_rect)

    def envi(self):
    pass
    def create(self):
    self.sprite=Sprites(self.screen);
    self.battle=Battle(game)
    def update(self):
    self.sprite.update()
    def events(self):
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    self.running=False
    if event.type == pygame.MOUSEMOTION or pygame.MOUSEBUTTONDOWN:
    buttons()


    def draw(self):
    self.screen.fill((0,0,0));
    self.clock.tick(FPS);
    self.sprite.drawbox();
    pygame.display.update()
    def main(self):
    while self.running:
    self.events()
    self.update()
    self.draw()

    game=Game()
    game.create()

    while game.running:
    game.main()

    pygame.quit()
    sys.exit()

    Continue reading...

Compartilhe esta Página