[Python] Weird bug I cant figure how to fix [duplicate]

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

  1. Stack

    Stack Membro Participativo

    import pygame
    from os.path import isfile, join
    from sys import exit
    from random import randint
    pygame.init()

    class GameTitle():
    def __init__(self):
    pass

    class PlayButton(pygame.sprite.Sprite):
    def __init__(self, x, y, groups):
    super().__init__(groups)
    self.image = pygame.image.load(join('images', 'Main Menu', 'play_button.png')).convert_alpha()
    self.rect = self.image.get_frect(center = (x, y))

    def animations(self):
    self.mouse_pos = pygame.mouse.get_pos()
    if self.rect.collidepoint(self.mouse_pos):
    self.image = pygame.image.load(join('images', 'Main Menu', 'play_button_hover.png')).convert_alpha()
    else:
    self.image = pygame.image.load(join('images', 'Main Menu', 'play_button.png')).convert_alpha()

    def input(self):
    for event.type in pygame.event.get():
    if event.type == pygame.MOUSEBUTTONDOWN:
    if self.rect.collidepoint(self.mouse_pos):
    Play()

    def update(self):
    self.animations()

    class Player(pygame.sprite.Sprite):
    def __init__(self, groups):
    super().__init__(groups)
    self.image = pygame.image.load(join('images', 'begin_pong_bar.png')).convert_alpha()
    self.rect = self.image.get_frect(center = ((WINDOW_WIDTH / 6 - 50), (WINDOW_HEIGHT / 2 - 15)))
    self.speed = (WINDOW_HEIGHT / 0.64)

    def Input(self):
    # Inputs
    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]: self.rect.y += -0.5 * self.speed * dt
    if keys[pygame.K_DOWN]: self.rect.y += 0.5 * self.speed * dt
    if keys[pygame.K_w]: self.rect.y += -0.5 * self.speed * dt
    if keys[pygame.K_s]: self.rect.y += 0.5 * self.speed * dt

    def Collisions(self):
    # Border Collisions
    if self.rect.collidepoint(self.rect.x, 5): self.rect.y = 5
    if self.rect.collidepoint(self.rect.x, 597): self.rect.y = 492

    def update(self):
    self.Collisions()
    self.Input()

    def Play():
    pygame.display.set_caption('Offset: In Game')

    while True:

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

    win.fill('black')

    topline = pygame.draw.line(win, 'darkgray', (0,0), (WINDOW_WIDTH, 0), 8)
    bottomline = pygame.draw.line(win, 'darkgray', (0,WINDOW_HEIGHT), (WINDOW_WIDTH, WINDOW_HEIGHT), 8)
    leftline = pygame.draw.line(win, 'darkgray', (0,0), (0, WINDOW_HEIGHT), 8)
    rightline = pygame.draw.line(win, 'darkgray', (WINDOW_WIDTH, 0), (WINDOW_WIDTH, WINDOW_HEIGHT), 8)

    all_sprites.update()
    pygame.display.flip()

    def MainMenu():
    while True:
    win.fill('lightgreen')

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    exit()
    if event.type == pygame.MOUSEBUTTONDOWN:
    if player.rect.collidepoint(playbutton.mouse_pos):
    Play()


    mainmenu_sprites.update()
    pygame.display.flip()

    # General Setup
    WINDOW_WIDTH, WINDOW_HEIGHT = 960, 600
    game_icon = pygame.image.load(join('images', 'game_icon.png'))
    win = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
    pygame.display.set_caption('Offset: Main Menu')
    pygame.display.set_icon(game_icon)
    clock = pygame.time.Clock()

    # Imports
    all_sprites = pygame.sprite.Group()
    player = Player(all_sprites)
    all_sprites.add(player)
    mainmenu_sprites = pygame.sprite.Group()
    playbutton = PlayButton(WINDOW_WIDTH / 2, WINDOW_HEIGHT * 7/20, mainmenu_sprites)
    mainmenu_sprites.add(playbutton)

    while True:
    win.fill('lightgreen')

    mainmenu_sprites.draw(win)
    # Delta Time
    dt = clock.tick() / 1000
    # Event Loop
    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    pygame.quit()
    exit()


    MainMenu()

    pygame.display.flip()



    There should be a play button appearing but there, well is not. [​IMG]

    i expected play button to appear, and if i click on play it switched the screen.fill to the game. as of the play def.

    Continue reading...

Compartilhe esta Página