python - 使用滚动背景时按住空格键或向下键后 Sprite 会消失

标签 python python-3.x animation pygame sprite

当按住 SPACE 或 DOWN 时, Sprite 会因滚动背景而消失。当背景是静止图像时,它起作用。此外,动画速度很快,所以我使用了计时器来减慢速度,但是当松开向下按钮时,屏幕速度会加快。

我尝试过在线搜索语法,当背景图像静止时它可以工作。

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 事件之间), bool 值 run1。所以没有什么是 blit 的。这就是你的图像消失的原因。 您应该仍然能够看到运动的开始:按下按钮时的帧,KEYDOWN 事件被捕获并执行该部分代码。但帧之后,没有任何 blit,因为 run1False 并且没有要捕获的事件。

我没有有效的解决方案,正如我所说,我无法测试游戏,但我建议至少确保 keepdrawing 始终绘制一些东西。尝试计算当 run1False 时应该绘制什么,并在 keepdrawing 中添加几行相关内容。

作为更一般的建议,不要在 keepdrawing 函数中使用 pygame 事件系统。只需检查按钮是否被按下,就像您在此处所做的那样:

keys = pygame.key.get_pressed()

关于python - 使用滚动背景时按住空格键或向下键后 Sprite 会消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56279832/

相关文章:

Python 使用次要 y 轴制作组合条形图和线图

javascript - React Animations - 在组件之间滑动

css - SVG倒车动画

python - Django 通用多对多?

python - 正则表达式搜索删除单词

python - SQLAlachmey : ORM filter to match all items in a list, 没有

ios - 默认推送 viewController 动画不播放

python - 当我再进行 GET 调用时,get_rate_status "remaining hits"不会减少

Python 不等式 : ! = vs 不 ==

python - 如何使用 Selenium 在动态加载的网页中正确滚动?