Pygame:角色未出现在屏幕上 [英] Pygame: Character not appearing on screen

查看:161
本文介绍了Pygame:角色未出现在屏幕上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使此代码正常工作数周,无法解决问题.我有一个图像列表,我正在使用这些图像来动画角色.这款游戏起初运行良好,但是自从我设置背景动画(开始从左向右移动)以来,我的角色就​​停止出现了.我是否需要更改某些代码的位置?真的很困惑.谢谢!!

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()

推荐答案

您在先前的一个问题中遇到了疑问-请参阅 https://stackoverflow.com/a/59761318/1832058

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

您必须绘制man 之后 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()

这篇关于Pygame:角色未出现在屏幕上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆