I'm very new to python and I have been trying to make a game. But I want to make the background continuously going down in a loop and I can't do it. Here's my code:
import pygame
pygame.init()
width = 800
height = 600
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
ship_width = 35
ship_height = 64
disp = pygame.display.set_mode((width,height))
pygame.display.set_caption("Space Jump")
clock = pygame.time.Clock()
bg1 = pygame.image.load("Space.png")
bg2 = pygame.image.load("Space.png")
shipImg = pygame.image.load("Ship.png")
def ship(x,y):
disp.blit(shipImg, (x,y))
def gameLoop():
x = (width * 0.45)
y = (height * 0.8)
x_ch = 0
y_ch = 0
x_bg = 0
y_bg = bg1.get_height()
gameExit = False
while not gameExit:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
x_ch = -5
elif event.key == pygame.K_RIGHT:
x_ch = 5
elif event.key == pygame.K_UP:
y_ch = -5
elif event.key == pygame.K_DOWN:
y_ch = 5
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_ch = 0
if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_ch = 0
x += x_ch
y += y_ch
if x > width - ship_width or x < 0:
x_ch = 0
if y > height - ship_height or y < 0:
y_ch = 0
disp.blit(bg1, (0,x_bg))
disp.blit(bg2, (0,y_bg))
ship(x,y)
pygame.display.update()
x_bg += 1
y_bg += 1
if x_bg == -1 * x_bg.get_height(): #This is the part I'm stuck on
x_bg = y_bg + y_bg.get_height() #When I run this i get and error:
#if x_bg == -1 * x_bg.get_height():
#AttributeError: 'int' object has no
#attribute 'get_height'
if y_bg == -1 * y_bg.get_height():
y_bg = x_bg + x_bg.get_height()
clock.tick(30)
gameLoop()
pygame.quit()
quit()
So I get an error when i run this code but when I just write the background code on a new file it works but the background ends at some point it doesn't loop. I've been trying to do this looping background for the past 3 days please help me. Thanks.