Search code examples
pythonpygame2d-games

Sprite in Pygame moving up and down in a endless loop


I don't know much about pygame so I just copied a tutorial on pygame.org and the sprite just spins up and down in a loop.

import sys, pygame
pygame.init()

size = width, height = 500, 500
speed = [2, 2]
blue = 85, 118, 250

screen = pygame.display.set_mode(size)

bird = pygame.image.load("bird-sprite.png")
birdrect = bird.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT: sys.exit()

    birdrect = birdrect.move(speed)
    if birdrect.left < 0 or birdrect.right > width:
        speed[0] = -speed[0]
    if birdrect.top < 0 or birdrect.bottom > height:
        speed[1] = -speed[1]
        
    screen.fill(blue)
    screen.blit(bird, birdrect)
    pygame.display.flip()

I tried to create movement for a sprite but instead the sprite just moves around the entire window without stopping in a endless loop.

I used pygame.org because I couldn't find a exact tutorial on movement so yeah.

I don't know much Python so I don't know what I did wrong.


Solution

  • Well, it does exactly what code tells it to do - move bird every frame by 2 pixels (speed variable):

    # This line of code moves bird sprite every frame by 2 pixels
    birdrect = birdrect.move(speed)
    
    # This lines of code checks if bird sprite is outside of the screen
    # If it is - then speed inverts, so bird starts moving in the opposite way
    if birdrect.left < 0 or birdrect.right > width:
            speed[0] = -speed[0]
        if birdrect.top < 0 or birdrect.bottom > height:
            speed[1] = -speed[1]
    

    If you want to implement movement, you should really read a bunch of tutorials about pygame.

    Here is my simple implementation of movement (birds moves using arrow keys):

    import sys, pygame
    pygame.init()
    
    size = width, height = 500, 500
    speed = [0.1, 0.1]
    blue = 85, 118, 250
    
    screen = pygame.display.set_mode(size)
    clock = pygame.time.Clock()
    
    bird = pygame.image.load("bird-sprite.png")
    birdrect = bird.get_rect()
    
    # Coordinates of bird
    bird_x, bird_y = birdrect.x, birdrect.y
    
    # Speed of bird in px/second
    speed = 200
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT: sys.exit()
    
        # Calculate how many milliseconds passed since previous frames
        # This is necessary for smooth movement
        time_passed = clock.tick(60) / 1000
    
        # Get pressed keys state
        pressed_keys = pygame.key.get_pressed()
    
        # If LEFT is pressed - subtract speed*time_passed from bird`s X coordinate
        # this means that when you press LEFT for 1 second - bird will move 200 pixels left
        # Do same calculations for every key
        if pressed_keys[pygame.K_LEFT]:
            bird_x -= speed * time_passed
    
        if pressed_keys[pygame.K_RIGHT]:
            bird_x += speed * time_passed
    
        if pressed_keys[pygame.K_DOWN]:
            bird_y += speed * time_passed
    
        if pressed_keys[pygame.K_UP]:
            bird_y -= speed * time_passed
    
        # Apply changes to bird's rect
        birdrect.x = int(bird_x)
        birdrect.y = int(bird_y)
    
        # Draw everyting
        screen.fill(blue)
        screen.blit(bird, birdrect)
    
        # Update display
        pygame.display.flip()