Pygame的:图像没有出现在运动的背景 [英] Pygame: image not appearing on moving background

查看:97
本文介绍了Pygame的:图像没有出现在运动的背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我没有活动的背景时,我的代码工作正常.但是现在,我开始使角色从左向右移动,并且它停止显示.香港专业教育学院一直试图找出问题的根源,请帮忙!谢谢!!!

my code was working fine when i didnt have a moving background. But now, i started making my character move from left to right and it stopped showing up. Ive been trying to figure out what the issue is for days, please help!! Thanks!!!

以下是我用于背景和字符的代码行:

the following is the line of code i used for the background and character:

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



#  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

推荐答案

背景将所有内容从屏幕上删除-因此您必须始终在其他任何元素之前绘制背景.

Background removes all from screen - so you have to always draw background before any other elements.

您无需在(0,0)中绘制静态背景,因为移动背景也会将其删除.

You don't need to draw static background in (0,0) because moving background also remove it.

def redrawGameWindow():
    # background
    screen.blit(background, (backgroundX, 0))
    screen.blit(background, (backgroundX2, 0))

    man.draw(screen)

    pygame.display.update()

这篇关于Pygame的:图像没有出现在运动的背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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