0

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.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
Eren Biçer
  • 171
  • 3
  • 10
  • I got it working thanks to mad meesh but the background ends at some point can someone tell me how i can loop it so it doesn't end? – Eren Biçer Jun 21 '17 at 03:12

2 Answers2

1

ok i've not used pygame before but just a quick glance at your code and the docs, it looks like you're calling get_height() on a number (x_bg and y_bg) not, what pygame refers to as a surface object.

the error, roughly translated, is saying that numbers do not have a method with the name get_height().

your surface objects are defined as bg1 and bg2 so my guess would be that those would have the method.

just swap the variables out with the correct ones and the error should be gone.

mad.meesh
  • 2,558
  • 1
  • 13
  • 20
  • It works now thanks but now how do i get it to loop? it ends at some point. – Eren Biçer Jun 21 '17 at 02:10
  • i'm sure there are many ways to do it but here's a _very_ condensed description of how i've achieved parallaxing planes: assuming (0,0) is bottom left and you want to scroll the BG bottom to top: `- 3 copies of the image - h = img height - align the copies at the same x - img1.y = centered - img2.y = h - img3.y = h * 3 * -1 - define two variables 'scroll_speed' and 'distance = 0'` on each frame `- for each img do img.y += scroll_speed - do distance += scroll_speed - if distance > h, swap y of img2 with img3, reset distance to 0` – mad.meesh Jun 21 '17 at 04:16
  • sorry i already got it working like an hour ago thanks anyways – Eren Biçer Jun 21 '17 at 04:18
  • ok. glad you got it worked out. i can't get the answer formatted properly in the comments anyway. – mad.meesh Jun 21 '17 at 04:21
0

I'm not entirely sure what some of your code is trying to do, but I can get you started.

You can see the error says: "AttributeError: 'int' object has no attribute 'get_height'"

This is telling you that x_bg is an integer, and not an object, so it cannot access any attribute named 'get_height'. I would recommend learning more about objects and functions in python, and how they work.

It seems like you are already using x_bg and y_bg as the position of your backround, so there is no need to try to use any function called get_height, you can replace it with:

if x_bg == -1 * x_bg:
    x_bg = y_bg + y_bg

Another problem with this piece of code is that is is checking when the value of x_bg is exactly equal to a number. While this can sometimes work, especially when you are only dealing with integers, its usually a good idea to check whether your variable is greater/less than a number. for example if your background is at y = 99, and you are checking for y==100, but the screen is moving 2 pixels each step, your condition will never be reached, while if you are checking for y > 100 it should always be caught. you might want something like:

if y_bg < 0:
    y_bg = height

This won't work perfectly, but it shouldn't cause any errors and should help you get on the right track

C. Brooks
  • 48
  • 6