Animation doesn't loop it just stops

Question:

When I run this code for my game, the code doesn’t loop the asteroid animation. It falls to the bottom of the window and never re-appears at the top. I’m guessing python sees both asteroids as the same thing rather than 2 different things. (my theory). Not sure how to go about this one. Maybe start using objects/classes? maybe a function for asteroid 1 and asteroid 2?


import pygame
import sys

pygame.init()

window = 900, 700
screen =  pygame.display.set_mode(window)
title = pygame.display.set_caption("ASTEROOOIDDD")
clock = pygame.time.Clock()


# coordinates of ship
x = 340
y = 280
step = 12

# coordinates of asteroid
x1 = 200
y1 = 0

x2 = 350
y2 = 0



background_surface = pygame.image.load('asteroid/images/background.jpg')

asteroid_surface = pygame.image.load('asteroid/images/rock.png')
asteroid_surface = pygame.transform.scale(asteroid_surface, (80,80))

asteroid_surface1 = pygame.image.load('asteroid/images/rock.png')
asteroid_surface1 = pygame.transform.scale(asteroid_surface, (200,200))

asteroid_surface2 = pygame.image.load('asteroid/images/rock.png')
asteroid_surface2 = pygame.transform.scale(asteroid_surface, (100,100))


spaceship_surface =  pygame.image.load('asteroid/images/ship.png')
spaceship_surface = pygame.transform.scale(spaceship_surface, (80,80))





    


while True:
    


    screen.blit(background_surface,(0,0))


    y1 = y1 + 5
    if y1 > 700: y1 = -100
    screen.blit(asteroid_surface, (x1,y1))

    y2 = y2 + 5
    if y2 > 700: y1 = -112
    screen.blit(asteroid_surface, (x2,y2))
   
  
    screen.blit(spaceship_surface,(x,y))
  


    for eve in pygame.event.get():
        if eve.type==pygame.QUIT:
            pygame.quit()
            sys.exit()

        

    
    
    key_input = pygame.key.get_pressed()   
    if key_input[pygame.K_LEFT]:
        x  -= step
    if key_input[pygame.K_UP]:
        y -= step
    if key_input[pygame.K_RIGHT]:
        x += step
    if key_input[pygame.K_DOWN]:
        y += step
     


    spaceship_surface.blit(spaceship_surface, (x,y))
    pygame.display.update()
    clock.tick(30)




Asked By: user16818230

||

Answers:

You are setting their y coordinates to -100, or off screen. If you are meaning to subtract 100 do y -= 100

Answered By: Anthony L

It is because of if y2 > 700: y1 = -112. It should be if y2 > 700: y2 = -112

Answered By: Jobo Fernandez
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.