【问题标题】:Sprite disappears after holding SPACE or DOWN when using scrolling background使用滚动背景时按住 SPACE 或 DOWN 后精灵消失
【发布时间】:2019-05-23 16:53:47
【问题描述】:

当按住 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() 

【问题讨论】:

  • 很有可能您正在观察的“消失”只是在背景之前绘制的精灵(这会将背景放在精灵的顶部,从而隐藏它)。我没有密切关注,所以我可能完全错了,但是这条线 window.blit(run2[runcount],(int(x),int(y))) 是在与您的其他 blits 分开的地方有条件地调用的。

标签: python python-3.x animation pygame sprite


【解决方案1】:

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

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

我没有可行的解决方案,正如我所说的我无法测试游戏,但我建议至少确保keepdrawing 总是画一些东西。试着弄清楚当run1False 时你应该画什么,并在keepdrawing 中添加几行关于它的内容。

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

keys = pygame.key.get_pressed()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-05-29
    • 1970-01-01
    • 2012-07-08
    • 1970-01-01
    • 1970-01-01
    • 2019-05-19
    相关资源
    最近更新 更多