0

我陷入了无限循环,但我不知道如何摆脱它。我正在尝试构建一个点击游戏,并希望一个自动角色为我造成伤害。我没有花太多时间来完善它,它可能很混乱和不方便,但我是一个非常新的编码器,想获得一些经验。

我试图使 loop等于 2 并运行一个相同的 while 循环,但是当我知道它必须在 while 循环中时,我在 if 语句中这样做了。只是不知道如何解决它。任何其他提示也将被应用!

import pygame
from sys import exit

pygame.init()
rotation_angle = 180
screen = pygame.display.set_mode((1080, 900))
clock = pygame.time.Clock()
font1 = pygame.font.Font(None, 50)

monster1 = pygame.image.load("C:/Users/t_cro/OneDrive/Pictures/Saved Pictures/snail1.png")
monster2 = pygame.image.load("C:/Users/t_cro/OneDrive/Pictures/Saved Pictures/fly1.png")
background = pygame.image.load("C:/Users/t_cro/OneDrive/Pictures/Saved Pictures/background.png")
automatic_character_background = pygame.image.load("C:/Users/t_cro/OneDrive/Pictures/Saved Pictures/scrollbackground.png")
automatic_character = pygame.image.load("C:/Users/t_cro/OneDrive/Pictures/Saved Pictures/player_walk_1.png")
background = pygame.transform.scale(background, (1080, 900))
monster1 = pygame.transform.scale(monster1, (125, 75))
monster2 = pygame.transform.scale(monster2, (0, 0))
automatic_character_background = pygame.transform.scale(automatic_character_background, (500, 820))
monster1_health = 10
monster2_health = 10
death_count = 0
money = 0
cost_character_1 = 50
character_1_amount = 0
red = 255,0,0
text_surface = font1.render("number of monsters killed:", True, red)
money_text = font1.render("money:", True, red)
buy_auto_character_1 = font1.render("buy/upgrade (1)", True, red)
monstercheck = 1
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and monstercheck == 1:
                monster1_health -= 1
                if monster1_health <= 0:
                    print("wow this code actually works")
                    death_count += 1
                    monster1_health = 10
                    monstercheck = 1
                    money += 10
            if event.key == pygame.K_SPACE and monstercheck == 2:
                monster2_health -= 1
                if monster2_health <= 0:
                    print("how have i not given up yet?")
                    monster2_health = 10
                    death_count += 1
                    monstercheck = 1
                    money += 10
---------------------------------error here-------------------------------------------------------
            if event.key == pygame.K_1 and cost_character_1 <= money:
                character_1_amount += 1
            if character_1_amount >= 1:
                loop = 1
                while loop == 1:
                    dt = clock.tick()
                    time_since_last_hit = dt
                    if time_since_last_hit >= 1000:
                        monster1_health -= character_1_amount
                        time_since_last_hit = 0
--------------------------------------------------------------------------------------------------
    death_count_blit = font1.render(str(death_count), True, red)
    money_number = font1.render(str(money), True, red)
    monster1_health_blit = font1.render(str(monster1_health), True, red)
    character_1_amount_blit = font1.render(str(character_1_amount), True, red)
    screen.blit(background, (0, 0))
    screen.blit(monster1, (700, 450))
    screen.blit(monster2, (700, 450))
    screen.blit(death_count_blit, (530, 50))
    screen.blit(text_surface, (330, 13))
    screen.blit(automatic_character_background, (30, 50))
    screen.blit(automatic_character, (75, 175))
    screen.blit(money_text, (125, 97))
    screen.blit(money_number, (250, 100))
    screen.blit(buy_auto_character_1, (150, 200))
    screen.blit(monster1_health_blit, (750, 550))
    screen.blit(character_1_amount_blit, (1,1))
    pygame.display.update()
4

1 回答 1

0

您使用哪个if语句来更新loop变量?

循环正常工作有两个规则:

  1. 您应该能够进入while循环,这是因为您的loop变量等于 1,这也是您的 while 条件。
  2. 您应该能够退出while循环,这可以通过将loop变量设置为不等于 1 的值来实现,这应该发生在 while 循环内。

如评论中所述,我没有看到循环loop内的变量更新。while您需要一个if条件或类似的东西将loop变量设置为 1 以外的值,以便循环可以完成。我还建议您print在循环内添加一条语句,while并在更新变量的行上方添加一条语句,loop以查看哪些行已执行,哪些行未执行。

于 2022-01-26T19:44:58.467 回答