Python Pygame:图像不出现在移动背景上

Python Pygame:图像不出现在移动背景上,python,background,pygame,Python,Background,Pygame,当我没有移动的背景时,我的代码运行良好。但现在,我开始让我的角色从左向右移动,它就不再出现了。好几天来我一直在想问题是什么,请帮忙!!谢谢 以下是我用于背景和角色的代码行: # create class for character (object) class player(object): def __init__(self, x, y, width, height): # initialize attributes self.x = x self.y

当我没有移动的背景时,我的代码运行良好。但现在,我开始让我的角色从左向右移动,它就不再出现了。好几天来我一直在想问题是什么,请帮忙!!谢谢

以下是我用于背景和角色的代码行:

# 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
#为角色(对象)创建类
类播放器(对象):
定义初始化(self、x、y、width、height):#初始化属性
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.left=True
self.right=True
self.isJump=False
self.stepCount=0
self.jumpCount=10
自立
def提取(自身,屏幕):
如果self.stepCount+1>=27:#9个精灵,3帧-大于27超出范围
self.stepCount=0
如果不是自立的:
如果self.left:
blit(leftDirection[self.stepCount//5],(self.x,self.y),)
self.stepCount+=1
elif self.right:
blit(rightDirection[self.stepCount//5],(self.x,self.y),)
self.stepCount+=1
其他:
如果你是对的:
screen.blit(righdirection[0],(self.x,self.y))#使用索引,包括右面照片
其他:
blit(左方向[0],(self.x,self.y))
类放大(对象):
定义初始(自、x、y、半径、颜色、面):
self.x=x
self.y=y
自半径=半径
self.color=颜色
自我面对
def提取(自身,屏幕):
pygame.draw.circle(屏幕,self.color,(self.x,self.y),self.radius,1)
#主回路
速度=30#新
男=玩家(200410,64,64)#设置主要角色属性
运行=真
运行时:
重画GameWindow()#调用过程
时钟。滴答声(速度)#新
backgroundX-=1.4#将两幅背景图像向后移动
背景x2-=1.4
如果backgroundX=-10:
负=1
如果man.jumpCount<0:
负=-1
man.y-=(man.jumpCount**2)*0.5*neg#使用抛物线跳跃
man.jumpCount-=1
其他:
man.isJump=False
man.jumpCount=10

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

您不需要在
(0,0)
中绘制静态背景,因为移动背景也会删除它

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

    man.draw(screen)

    pygame.display.update()

你在哪里画背景?哪里是重画的游戏窗口()?你在绘制背景后和屏幕前绘制播放器吗
flip()
/
update()
?我完全忘了添加这段代码(带背景)-谢谢!def redrawGameWindow():screen.blit(background,(0,0))man.draw(screen)#用于从右向左移动的屏幕的背景图像。blit(background,(backgroundX,0))screen.blit(background,(backgroundX2,0))pygame.display.update()始终将代码、数据和错误消息放在问题中,而不是放在注释中。代码
重画游戏
显示在绘制背景之前先绘制玩家,这样就可以将玩家从尖叫中移除。您必须始终在所有其他元素之前绘制背景。您不需要在
(0,0)
中绘制背景,因为移动背景也会删除它。感谢您的建议和帮助-我非常感谢!!!然而,如果我应该更改“重画游戏”函数的位置,我有点困惑。如果我要把它移到哪里?另外,当我从中删除(“0”)时,它显示“目的地出错”。谢谢