Home > database >  I was wondering why my playerIMG wont load in pygame
I was wondering why my playerIMG wont load in pygame

Time:08-10

Here is my code below this is my first pygame project any help will be appreciated! I think its something to do with the

def player(playerX,playerY):
pygame.display.update()

block of code. Though when I play around with it sometimes it won't even display my background object and only displays the screen filler black.

import pygame

#initializing the game
pygame.init()

FPS = 60
black = (0,0,0)
white = (255,255,255)

#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))

#setting the title of the window
pygame.display.set_caption('Space Crashers')

#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')

#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480

#creating player function
def player(x,y):
    gameDisplay.blit(playerImg, (x,y))




# window creation
clock = pygame.time.Clock()
running = True
while running:
    
     #setting the background to black
    gameDisplay.fill(black)
   
    #then changing the background to the image we loaded
    gameDisplay.blit(background,(0,0))
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False
    pygame.display.update() 
    
        # if the keystroke is pressed, the player will move
    if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX -= .01
            if event.key == pygame.K_RIGHT:
                playerX  = .01
            if event.key == pygame.K_UP:
                playerY -= .01
            if event.key == pygame.K_DOWN:
                playerY  = .01
    
    
    
               
def player(playerX,playerY):         
        pygame.display.update()             
clock.tick(FPS)   
pygame.quit()
quit()

CodePudding user response:

See How can I make a sprite move when key is held down. Also learn about Indentation. Your code must have the correct indentation to work properly.

The typical PyGame application loop has to:

import pygame

#initializing the game
pygame.init()

FPS = 60
black = (0,0,0)
white = (255,255,255)

#creating the screen and setting the size
gameDisplay = pygame.display.set_mode((800,600))

#setting the title of the window
pygame.display.set_caption('Space Crashers')

#going into our files and loading the image for the background
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')

#player image and model
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')
#these cords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480

# creating player function
def player(x,y):
    gameDisplay.blit(playerImg, (x,y))

# application loop
clock = pygame.time.Clock()
running = True
while running:
    
    # handle the events 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False

    # update the game states and positions of objects
    keys = pygame.key.get_pressed()
    playerX  = (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT])  * 3
    playerY  = (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * 3

    # clear the entire display or draw the background
    gameDisplay.fill(black)
    gameDisplay.blit(background,(0,0))

    # draw the entire scene (blit all the objects)
    player(playerX, playerY)
    
    # update the display 
    pygame.display.update()  

    # limit the frames per second to limit CPU usage with
    clock.tick(FPS)   

pygame.quit()
quit()

CodePudding user response:

Issues found:

  1. if block for keyboard controls is outside the for event loop.
  2. did not call for player function before the pygame.display.update()
  3. player function has duplicate
while running:

    # setting the background to black
    gameDisplay.fill(black)

    # then changing the background to the image we loaded
    gameDisplay.blit(background, (0, 0))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        # if the keystroke is pressed, the player will move
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX -= .01 * 100
            if event.key == pygame.K_RIGHT:
                playerX  = .01 * 100
            if event.key == pygame.K_UP:
                playerY -= .01 * 100
            if event.key == pygame.K_DOWN:
                playerY  = .01 * 100

    player(playerX, playerY)
    pygame.display.update()

# def player(playerX, playerY):
#     pygame.display.update()
  • Related