-1

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.


#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()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
akassem
  • 1
  • 1
  • Your code is completely messed up. Why do you repeat the question? [I was wondering why my playerIMG wont load in pygame](https://stackoverflow.com/questions/73254140/i-was-wondering-why-my-playerimg-wont-load-in-pygame). Read the linked answers and at least try to solve the problem. Stack Overflow is not a code writing service. You obviously didn't put enough effort into your code. See [How can I make a sprite move when key is held down](https://stackoverflow.com/questions/9961563/how-can-i-make-a-sprite-move-when-key-is-held-down) – Rabbid76 Aug 06 '22 at 05:38

1 Answers1

0

In your game loop you should normally

  • fill your screen black
  • draw and blit everything to the screen
  • update the screen
  • catch the events
  • let the clock tick

You didn't call the player() function in your loop to draw the player image neither the clock.tick() function. You don't need to overwrite the player function. Also if ran without any arguments, you should only have one event loop, otherwise some events could be ignored.

pygame.init()

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

# window
gameDisplay = pygame.display.set_mode((800,600))
pygame.display.set_caption('Space Crashers')

# background image
background = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\newBackground.jpg')

# player image
playerImg = pygame.image.load(r'C:\\Users\\ahmad\\Downloads\\pygame projects\\Assets\\blueShip.png')

# these coords are for the player ship to be in the middle of the screen
playerX = 370
playerY = 480

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

# window creation
clock = pygame.time.Clock()
running = True

while running:
    # reset display
    gameDisplay.fill(black)
   
    # blit background
    gameDisplay.blit(background, (0, 0))

    ### blit the player ###
    player(playerX, playerY)
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
             running = False
        # move player
        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
    
    pygame.display.update() 
    clock.tick(FPS)

pygame.quit()

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Jerry
  • 401
  • 4
  • 11