Pygame: изображение не появляется на движущемся фоне - PullRequest
0 голосов
/ 15 января 2020

мой код работал нормально, когда у меня не было движущегося фона. Но теперь я начал двигать своего персонажа слева направо, и он перестал появляться. Я пытался выяснить, в чем проблема в течение нескольких дней, пожалуйста, помогите! Спасибо !!!

Ниже приведена строка кода, которую я использовал для фона и символа:

# 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

1 Ответ

0 голосов
/ 16 января 2020

Фон удаляет все с экрана - поэтому вы всегда должны рисовать фон перед любыми другими элементами.

Вам не нужно рисовать фон c в (0,0), потому что движущийся фон также удаляет его.

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

    man.draw(screen)

    pygame.display.update()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...