Pygame: Character not appearing on screen

杀马特。学长 韩版系。学妹 提交于 2020-01-26 03:54:07

问题


I have been trying to get this code to work for weeks and cannot figure out the issue. I have a list of images, that I am using to animate my character. THe game worked fine at first, but ever since I made the background animate (start moving left to right), my character stopped appearing. Do I have to change the location of some of my code? Really confused. Thank you!!

# create lists with images of character walking left and right
rightDirection = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'),
                  pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'),
                  pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')]
leftDirection = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'),
                 pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'),
                 pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')]

background = pygame.image.load('background.png')
backgroundX = 0
backgroundX2 = background.get_width()
homeScreen = pygame.image.load('home_screen.png')

# frame rate
clock = pygame.time.Clock()


# use procedure for game window rather than using it within loop
def redrawGameWindow():
    man.draw(screen)
    # background images for right to left moving screen
    screen.blit(background, (backgroundX, 0))
    screen.blit(background, (backgroundX2, 0))
    pygame.display.update()


# create class for character (object)
class player(object):
    def __init__(self, x, y, width, height):  # initialize attributes
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.left = True
        self.right = True
        self.isJump = False
        self.stepCount = 0
        self.jumpCount = 10
        self.standing = True

    def draw(self, screen):
        if self.stepCount + 1 >= 27:  # 9 sprites, with 3 frames - above 27 goes out of range
            self.stepCount = 0

        if not self.standing:
            if self.left:
                screen.blit(leftDirection[self.stepCount // 5], (self.x, self.y), )
                self.stepCount += 1
            elif self.right:
                screen.blit(rightDirection[self.stepCount // 5], (self.x, self.y), )
                self.stepCount += 1
        else:
            if self.right:
                screen.blit(rightDirection[0], (self.x, self.y))  # using index, include right faced photo
            else:
                screen.blit(leftDirection[0], (self.x, self.y))


class enlargement(object):
    def __init__(self, x, y, radius, color, facing):
        self.x = x
        self.y = y
        self.radius = radius
        self.color = color
        self.facing = facing

    def draw(self, screen):
        pygame.draw.circle(screen, self.color, (self.x, self.y), self.radius, 1)



man = player(200, 313, 64, 64)



#  main loop

speed = 30  # NEW
man = player(200, 410, 64, 64)  # set main character attributes
run = True
while run:
    redrawGameWindow()  # call procedure
    clock.tick(speed)  # NEW
    backgroundX -= 1.4  # Move both background images back
    backgroundX2 -= 1.4

    if backgroundX < background.get_width() * -1:  # If our background is at the -width then reset its position
        backgroundX = background.get_width()

    if backgroundX2 < background.get_width() * -1:
        backgroundX2 = background.get_width()

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

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_LEFT]:
        man.left = True
        man.right = False
        man.standing = False  # false, because man is walking
    # verify that character is within window parameters
    elif keys[pygame.K_RIGHT]:
        man.right = True
        man.left = False
        man.standing = False  # false, because man is walking
    else:
        man.standing = True
        man.stepCount = 0

    if not man.isJump:
        if keys[pygame.K_SPACE]:
            man.isJump = True  # when jumping, man shouldn't move directly left or right
            man.right = False
            man.left = False
            man.stepCount = 0
    else:
        if man.jumpCount >= -10:
            neg = 1
            if man.jumpCount < 0:
                neg = -1
            man.y -= (man.jumpCount ** 2) * 0.5 * neg  # to jump use parabola
            man.jumpCount -= 1
        else:
            man.isJump = False
            man.jumpCount = 10

pygame.quit()

回答1:


You get anwser in one of previous questions - see https://stackoverflow.com/a/59761318/1832058

You have to draw man AFTER background.

def redrawGameWindow():

    # background images for right to left moving screen
    screen.blit(background, (backgroundX, 0))
    screen.blit(background, (backgroundX2, 0))

    # AFTER BACKGROUND !!!!
    man.draw(screen)

    pygame.display.update()


来源:https://stackoverflow.com/questions/59761825/pygame-character-not-appearing-on-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!