按住SPACE或DOWN时,精灵会由于滚动背景而消失。当背景是静止图像时,它将起作用。此外,动画速度很快,因此我使用了一个计时器使其变慢,但是放开DOWN按钮后,屏幕加速。

我尝试过在线搜索语法,并且在背景图像静止时也可以使用。

from time import sleep as s
import random
import pygame
pygame.init()
import time

window = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("Practice Game")
image = pygame.image.load('pixel6.png')
image = pygame.transform.scale(image, (1000, 500))

jump1 = [pygame.image.load('adventurer-jump-00.png'),pygame.image.load('adventurer-jump-01.png'),pygame.image.load('adventurer-jump-02.png'),pygame.image.load('adventurer-jump-03.png'), pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]

run2 = [pygame.image.load('adventurer-run-00.png'), pygame.image.load('adventurer-run-01.png'),pygame.image.load('adventurer-run-02.png'),pygame.image.load('adventurer-run-03.png')]

slide1 = [pygame.image.load('adventurer-slide-00.png'),pygame.image.load('adventurer-slide-01.png'),pygame.image.load('adventurer-stand-00.png'),pygame.image.load('adventurer-stand-01.png'),pygame.image.load('adventurer-stand-02.png')]

#attack = [pygame.image.load('
imagex = 0
imagex2 = image.get_width()
clock = pygame.time.Clock()

run = True
run1 = True
jump2 = True
slide2 = True
imagess = True
x = 40
y = 391
FPS = 60
speed = 0.6
jumpcount = 10
jumpcount1 = 0
runcount = 0
slide = 0
isJump = False
down = False

class obstacles(object):
    img = [pygame.image.load('img.png')]

    def __init__(self, x,y, width, height):
        self.x = x
        self.y =y
        self.width = width
        self.height = height
        self.hitbox = (x,y,width,height)
        self.count = 0

    def draw(self, window):
        self.hitbox = (self.x + 5, self.y + 5, self.width, self.height)
        if self.count >= 8:
            self.count = 0
        self.count +=1
        window.blit(pygame.transform.scale(self.img[self.count//1000], (150,100)), (self.x, self.y))
        pygame.draw.rect(window, (255,0,0), self.hitbox, 2)

objects = []
def keepdrawing():
    global runcount, slide, run1,jumpcount1
    window.blit(image, (imagex,0))
    window.blit(image, (imagex2,0))
    for object1 in objects:
        object1.draw(window)

    if runcount >= 3:
        runcount = 0

    if run1 == True:
        window.blit(run2[runcount],(int(x),int(y)))
        runcount +=1

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_DOWN and y == 391:
            run1 = False
            if slide >= 4:
                slide = 0

            if slide2:
                window.blit(slide1[slide],(int(x),int(y)))
                slide +=1

        if event.key == pygame.K_SPACE:
            run1 = False
            if jumpcount1 >= 7:
                jumpcount1 = 0
            if jump2 and y!=391:
                window.blit(jump1[jumpcount1],(int(x),int(y)))
                jumpcount1 +=1

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_DOWN:
            run1 = True

        if event.key == pygame.K_SPACE:
            run1=True


    pygame.display.update()

pygame.time.set_timer(pygame.USEREVENT+1, 500)
pygame.time.set_timer(pygame.USEREVENT+2, random.randrange(1000,2000))
obstacles = obstacles(1050,300,64,64)

while run:
    clock.tick(60)
    imagex -= 2
    imagex2 -= 2
    if imagex < image.get_width() * -1:
        imagex = image.get_width()
    if imagex2 < image.get_width() * -1:
        imagex2 = image.get_width()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.USEREVENT+1:
            speed += 1
        if event.type == pygame.USEREVENT+2:
            objects.append(obstacles)

    for object1 in objects:
        object1.x -= 1
        if object1.x < -100:
            objects.pop(objects.index(object1))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    keys = pygame.key.get_pressed()

    if not(isJump):

        if keys[pygame.K_SPACE]:
            isJump =True

    else:
        if jumpcount >= -10:
            neg=1
            if jumpcount <0:
                neg = -1
                s(0.01)
            y-= (jumpcount**2)*0.3*neg
            s(0.01)
            jumpcount -=1

        else:
            isJump = False
            jumpcount = 10
    keepdrawing()

最佳答案

由于我没有很多图像,因此我无法测试您的游戏,但我认为您误解了事件系统的工作原理,或者至少您忽略了一个案例。

按下键时,发出KEYDOWN,而抬起键时,发出KEYUP
这些事件是瞬时的(嗯,不是真的,但是它们非常快),并且仅当按钮的状态更改(从按下到未按下,反之亦然)时,事件管理器才能捕获这些事件。您不得依靠它们来检查按钮是否保持按下状态。请参阅this post以获得更好的理解。

考虑到这一点,让我们看看keepdrawing函数发生了什么。这里相关部分:

def keepdrawing():
    #...
    if run1 == True:
        #blitting
    if event.type == pygame.KEYDOWN:
        run1 = False
        #stuff and blitting

    if event.type == pygame.KEYUP:
        run1 = True


如您所见,按住按钮(即在KEYDOWNKEYUP事件之间)时,布尔值run1False。所以什么都不会破。这就是为什么您的图像消失了。
您仍然应该能够看到运动的开始:当您按下按钮时,将捕获KEYDOWN事件并执行该部分代码。但是在随后的帧中,没有任何东西发白,因为run1False并且没有任何事件可以捕捉。

我没有可行的解决方案,因为我说过我无法测试游戏,但我建议至少确保keepdrawing总是画些东西。尝试弄清楚run1False时应绘制的内容,并在keepdrawing中添加几行内容。

作为更一般的建议,请勿在keepdrawing函数中使用pygame事件系统。只需像在这里一样检查按钮是否被按下即可:

keys = pygame.key.get_pressed()

10-04 10:55