I am stuck in an infinite loop but I'm not sure how to get out of it. I am trying to build a clicker game and want an automatic character to deal damage for me. I haven't spent much time refining it all and it's probably messy and inconvenient, but I am a very new coder and want to get some experience.
I tried to make loop
equal to 2 and run an identical while loop, however I did that in the if statement when I know it has to be in the while loop. Just can't figure out how to fix it. Any other tips would also be appriciated!
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()
Which if
statement are you using to update the loop
variable?
There are two rules for the loop to work properly:
while
loop, which is happening as you have the loop
variable equal to 1, which is also your while condition.while
loop, which would happen by setting the loop
variable to something that is not equal to 1, and this should happen inside the while loop. As stated in the comment, I do not see the loop
variable updating inside the while
loop. You need an if
condition or something similar that will set the loop
variable to something other than 1 so that the loop can finish. I also suggest that you add a print
statement inside the while
loop and right above the line you update the loop
variable to see which lines are executed and which lines are not.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.