1

The menu page I made in my game does not work correctly. I'm trying to make a menu using pygame. My menu has 2 buttons start and exit. When I press the start button, it should go to the game screen, but it works incorrectly, why? This is my code:

run = True
while run:

    clock.tick(FPS)

    for etkinlik in pygame.event.get():
        if etkinlik.type == pygame.QUIT:
            run = False
    screen.fill((202, 228, 241))
    if start_button.draw(screen):
        start_button.clicked==True
        print('Play')
    if exit_button.draw(screen):
        run = False
        print('EXIT')
    if start_button.clicked==True:
        # arka planı çizmek için kullandığım fonksiyoumu çağırdım
        draw_bg()

        # Savaşcıların canlarını göster.
        draw_health_bar(fighter_1.health, 20, 20)
        draw_health_bar(fighter_2.health, 580, 20)
        # Savaşcıların skorlarını göster.
        draw_text("P1: " + str(score[0]), score_font, WHITE, 20, 60)
        draw_text("P2: " + str(score[1]), score_font, WHITE, 580, 60)

        # geri sayımı güncelle
        if intro_count <= 0:
            # karakterleri taşı
            fighter_1.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, fighter_2, round_over)
            fighter_2.move(SCREEN_WIDTH, SCREEN_HEIGHT, screen, fighter_1, round_over)
        else:
            # Yazıyı görüntüle.
            draw_text(str(intro_count), count_font, WHITE, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 3)
            if (pygame.time.get_ticks() - last_count_update) >= 1000:
                intro_count -= 1
                last_count_update = pygame.time.get_ticks()

        # Karakterleri güncelle
        fighter_1.update()
        fighter_2.update()

        # döcüşcü çizimleri
        fighter_1.draw(screen)
        fighter_2.draw(screen)

        # Oyuncu kazandı mi kontrol et
        if round_over == False:
            if fighter_1.alive == False:
                score[1] += 1
                round_over = True
                round_over_time = pygame.time.get_ticks()
            elif fighter_2.alive == False:
                score[0] += 1
                round_over = True
                round_over_time = pygame.time.get_ticks()
        else:
            # kazandın yazısını yazdır.
            screen.blit(victory_img, (360, 150))
            if pygame.time.get_ticks() - round_over_time > ROUND_OVER_COOLDOWN:
                round_over = False
                intro_count = 3
                fighter_1 = Fighter(1, 200, 310, False, WARRIOR_DATA, warrior_sheet, WARRIOR_ANIMATION_STEPS,
                                    sword_fx)
                fighter_2 = Fighter(2, 700, 310, True, WIZARD_DATA, wizard_sheet, WIZARD_ANIMATION_STEPS, magic_fx)



    pygame.display.flip()
    # Görüntüyü güncelleme komutu
    pygame.display.update()

# çıkış komutu
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Explain "works incorrectly". What happens, what should happen? If there are error messages, show them completely as properly formatted text in the question. – Michael Butscher Apr 30 '23 at 23:30
  • When I press the start button, it should go to the game screen, but it works incorrectly, why? Please describe exactly what happens and why it is not the expected behaviour, if possible also make some guesses as to which parts of the code might be responsible for this – Caridorc Apr 30 '23 at 23:30
  • 1
    `start_button.clicked==True` This _compares_ the values. It does not _assign_ a value... – John Gordon Apr 30 '23 at 23:32

1 Answers1

0

John Gordon already told you the bug:

start_button.clicked==True This compares the values. It does not assign a value... –

But teaching a man how to fish is more important than giving him a fish. Whenever you have a weird bug in Python code, you should run pylint to highlight weird things that are happening that might be bugs:

You can install it with

pip install pylint

Then run it with:

pylint smallui.py # smallui.py is your file

In the output you will see (among other things):

smallui.py:11:8: W0104: Statement seems to have no effect (pointless-statement)

And line 11 is exactly your problematic line!

start_button.clicked==True

Other ways to check your code can be stepping through in a debugger, adding unit tests, adding pprint.pprint(locals()) in key places, adding type annotations and running mypy, you can even try asking ChatGPT to look for potential bugs, but be careful about hallucinations (do not treat it as if it were all-knowing) and do not post ChatGPT outputs on StackOverflow. Enjoy!

Caridorc
  • 6,222
  • 2
  • 31
  • 46
  • @MCRdünyası You are welcome, keep in mind to use all the tools at your disposal to make your life easier ;) – Caridorc May 01 '23 at 00:17