简体   繁体   中英

Pygame character does not show up on screen

I try to get my Pygame character on screen but I cannot get it to appear so I can control it for a game I am building Here is the code:

import pygame
#turn on pygame

pygame.init()

#window, window name and icon
screen = pygame.display.set_mode((1920,1080))

pygame.display.set_caption("When The Sky Turns Grey")
icon = pygame.image.load('cat.png')
pygame.display.set_icon(icon)

#Player
playerIMG = pygame.image.load('cat.png')
playerX = 960
playerY = 1080
playerXchange = 0

def player():
    screen.blit(playerIMG, (playerX, playerY))

#Game loop

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

        #KEYS
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                playerX_change = -5
            if event.key == pygame.K_d:
                playerX_change = 5
                


        if event.type == pygame.KEYUP:
            if event.key == pygame.K_a or event.key == pygame.K_d:
                playerXchange = 0

    playerX += playerXchange        
    #RGB
    screen.fill((0, 0, 255))
    #draw player (call player function)
    player()
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    pygame.display.update()

I cannot get it to show up at all, no matter what I change. I am new to pygame and if anyone can help I will be grateful Thank you! aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

There is 2 problems in this question

First your cat Y position is to much that it went under the screen, it should be like this:

playerY = 540 # Don't go over 1080 which is the screen height

Second you didn't update the display so it doesn't show anything on the screen, at the very end of the while loop put pygame.display.update after player()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM