0

I am trying to make a background show up but it keeps coming with a error??? Here is the code..

import pygame, sys

# == (1) Create 'global' variables ==
# These are variables that every part of your
# code can 'see' and change
global screen, current_keys


# == (2) Define the functions for each task first ==

# == GameInit ==
# Put initialisation stuff here
# it is called just once
# at the beginning of our game
def GameInit():
    global screen
    pygame.init()
    screen = pygame.display.set_mode((1024,640))

spaceship = pygame.image.load("Space Ship.png")
Background = pygame.image.load("Space Background.png")
backrect = Background.get_rect()
shiprect = spaceship.get_rect()
shiprect = shiprect.move(448, 240)

# == GameLoop ==
# Put things that have to occur repeatedly
# here. It is called every frame
def GameLoop():
    global current_keys

    # Lock the timing to 60 frames per second
    pygame.time.Clock().tick(60)

    # Check for exit
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    pressed_keys = pygame.key.get_pressed()

    # update our key states
    current_keys = pygame.key.get_pressed()

    #if up or down move the Space Ship
    if pressed_keys[pygame.K_UP]:
    shiprect = shiprect.move(0, -20)
    if pressed_keys[pygame.K_DOWN]:
    shiprect = shiprect.move(0, 20)
    if pressed_keys[pygame.K_LEFT]:
    shiprect = shiprect.move(-20, 0)
    if pressed_keys[pygame.K_RIGHT]:
    shiprect = shiprect.move(20, 0) 

    # GameUpdate functions will go here
    # GameDraw functions will go here

    #Drawing the characters & Background
    screen.blit(Background, backrect)
    screen.blit(spaceship, shiprect)

    # flip the screen
    pygame.display.flip()

# == (3) Call the functions to run the game ==
# We have only *defined* our functions above.
# Here we actually call them to make them happen
GameInit()
while True:
    GameLoop()`

and here is the error

libpng warning: iCCP: known incorrect sRGB profile
Traceback (most recent call last):
  File "C:\Users\Naeem\Desktop\Python Subline Games\Space Game.py", line 70, in <module>
    GameLoop()
  File "C:\Users\Naeem\Desktop\Python Subline Games\Space Game.py", line 60, in GameLoop
    screen.blit(spaceship, shiprect)
UnboundLocalError: local variable 'shiprect' referenced before assignment
[Finished in 7.2s]
2
  • 1
    Once you get this working you might want to make a post over on codereview.stackexchange.com because there's a number of things about your code that could be improved. Commented Jan 22, 2015 at 20:15
  • possible duplicate of Python variable scope error Commented Jan 22, 2015 at 20:27

2 Answers 2

2

shiprect is not defined as global within GameLoop().

Try adding global shiprect to the beginning of that function.

(Edit: this is generally considered bad practice for many reasons: How to avoid global variables)

Sign up to request clarification or add additional context in comments.

3 Comments

no, you rarely (if ever) need to use globals. Passing the variable to the function is much better.
wait where do I have to add it? which function?
@GuyNaeem in the function GameLoop().
-1

If you define a value outside of a function, and want to use it inside a function, pass it to the function as a parameter:

foo = 1

def bar(foo):
    foo += 1
    return foo

So, in your case, pass shiprect to your GameLoop() function:

...
shiprect = shiprect.move(448, 240)

def GameLoop(shiprect):
    ...
    if pressed_keys[pygame.K_UP]:
        shiprect = shiprect.move(0, -20)

...
while True:
    GameLoop(shiprect)

2 Comments

This will not work - shiprect will be the same at each iteration of the main loop.
Thanks for the help, it helped a lot! But the ship goes up then it just comes down again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.