-1

i am trying to make a flappy bird movement that moves the rect forward and down at the same time but after implementing it. The rect on the pygame window doesnt appear and the window crashes. I tried it with a for and while statement but both gave the same result.

    #whole code
        import pygame
    import random
    from random import randint
    import math
    import time
    
    pygame.init()
    
    window = pygame.display.set_mode((600, 600))
    clock = pygame.time.Clock()
    
    # main application loop
    run = True
    while run:
        # limit frames per second
        clock.tick(100)
    
        # event loop
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
    
        # clear the display
        window.fill(0)
    
        x = 270
        y = 270
        speed = 5
        key_input = pygame.key.get_pressed()
        xr = random.randint(30, 270)
        yr = random.randint(30, 270)
        color1 = (255, 102, 255)
        dead = False
        while dead == False:
            x + 2
            y + 2
        if key_input[pygame.K_UP]:
            y - 2
        main = pygame.draw.rect(window, color1, pygame.Rect(x, y, 30, 30))
        
    
    
        # update the display
        pygame.display.flip()
    
    
    
    pygame.quit()
    exit()

#code crashing the game

    while dead == False:
        x + 2
        y + 2
    if key_input[pygame.K_UP]:
        y - 2
Umseizure
  • 63
  • 5

1 Answers1

1

You have an infinite loop:

while dead == False:
    x + 2
    y + 2

Once we enter this loop, there is never any chance for the value of the variable dead to change to True.

This is likely causing a stack overflow, which is why the program crashes.

Another note: the lines inside the loop don't actually do anything. If you want to add 2 to x and y, you need to reassign them:

x = x + 2
y = y + 2