横向滚动 Pygame 问题

Side Scrolling Pygame Problme

我试图用 pygame 制作横向卷轴游戏(我对 pygame 很陌生)我的游戏运行得很好但是一旦它转得很快出现几秒钟的奇怪故障,偶尔会发生,我认为它是某种背景错误,尺寸不够大,但我的屏幕尺寸是 800、447,但我使用的背景 png 是 1920、1080(黑屏是我的背景)这是我的代码

from pygame.locals import *
import os
import sys
import math

pygame.init()

W, H = 800, 447
win = pygame.display.set_mode((W,H))
pygame.display.set_caption('Side Scroller')
x = 200
y = 200
height = 30
width = 30

bg = pygame.image.load(os.path.join('im','bg.png')).convert()
bgX = 0
bgX2 = bg.get_width()

clock = pygame.time.Clock()

class player(object):
    run = [pygame.image.load(os.path.join('im', str(x) + '.png')) for x in range(8,16)]
    jump = [pygame.image.load(os.path.join('im', str(x) + '.png')) for x in range(1,8)]
    slide = [pygame.image.load(os.path.join('images', 'S1.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')),pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S2.png')), pygame.image.load(os.path.join('images', 'S3.png')), pygame.image.load(os.path.join('images', 'S4.png')), pygame.image.load(os.path.join('images', 'S5.png'))]
    jumpList = [![enter image description here][1]][1][1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,-1,-1,-1,-1,-1,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-2,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-3,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4,-4]
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height
        self.jumping = False
        self.sliding = False
        self.slideCount = 0
        self.jumpCount = 0
        self.runCount = 0
        self.slideUp = False

    def draw(self, win):
        if self.jumping:
            self.y -= self.jumpList[self.jumpCount] * 1.2
            win.blit(self.jump[self.jumpCount//18], (self.x,self.y))
            self.jumpCount += 1
            if self.jumpCount > 108:
                self.jumpCount = 0
                self.jumping = False
                self.runCount = 0
        elif self.sliding or self.slideUp:
            if self.slideCount < 20:
                self.y += 1
            elif self.slideCount == 80:
                self.y -= 19
                self.sliding = False
                self.slideUp = True
            if self.slideCount >= 110:
                self.slideCount = 0
                self.slideUp = False
                self.runCount = 0
            win.blit(self.slide[self.slideCount//10], (self.x,self.y))
            self.slideCount += 1
            
        else:
            if self.runCount > 42:
                self.runCount = 0
            win.blit(self.run[self.runCount//6], (self.x,self.y))
            self.runCount += 1


def redrawwindow():
      win.blit(bg, (bgX, 0))
      win.blit(bg, (bgX2, 0))
      runner.draw(win)
      pygame.display.update()
run = True
speed = 30[![enter image description here][1]][1]
pygame.time.set_timer(USEREVENT+1, 500)
runner = player(200, 313, 80, 64)
while run:

    bgX -= 1.4
    bgX2 -= 1.4
    if bgX < bg.get_width() * -1:
            bgX = bg.get_width()
    if bgX2 < bg.get_width() * -1:
            bg2 = bg.get_width()
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                  run = False

            if event.type == USEREVENT+1:
                speed +=1

    keys = pygame.key.get_pressed()
    if keys[pygame.K_SPACE] or keys[pygame.K_UP]:
        if not (runner.jumping):
            runner.jumping = True

    if keys[pygame.K_DOWN]:
        if not (runner.sliding):
            runner.sliding = True 

    redrawwindow()
    clock.tick(speed)
pygame.quit()

看来您只需要更新屏幕,像素就会正确更新。 你可以添加

pygame.display.update()

到游戏循环结束

如果这仍然不起作用,请尝试使用此命令仅使用黑色填充背景并清除所有内容

win.fill((0, 0, 0))

你应该把它放在游戏循环的开始