Pygame-Screen.blit(源,目标,区域)返回空矩形

标签 pygame display rect

我正在尝试制作一个动画,其中图像同时向下移动并消失,如下所示:

enter image description here

但是,我似乎无法让它正常工作。我只想改变屏幕上有动画的部分,所以我做了这样的事情......

orect = pygame.Rect(oSprite.rect)
    for i in range(10):
        screen.fill(Color(255,255,255),rect=orect)
        oSprite.rect.top += 12
        print(orect)
        print(screen.blit(oSprite.image, oSprite.rect, orect))
        pygame.display.update(orect)
        timer.tick(30)

其中 oSprite 是代表我想要制作动画的图像的 Sprite。

在文档中,screen.blit(source, dest, area) 应该返回一个代表已更改像素的 Rect,但是当我运行代码时,我得到了这个(10 次以上):

<rect(336, 48, 76, 74)>
<rect(336, 60, 0, 0)>

第二行是screen.blit()返回的内容,这意味着它改变了0x0区域,事实上,当代码运行时我在屏幕上看到的只是突然变成白色,而不是任何动画。为什么会出现这种情况?从第一个 print() 语句可以看出,我为区域值输入的矩形是 76x74,而不是 0x0。

最佳答案

您需要在 oSprite.image 表面上进行位图传输,而不是在屏幕表面上。

这将在 oSprite.image 之上绘制另一个图像,如果它更大,则不会在屏幕上扩展。

oSprite.image.blit(new_image, (0,0))

编辑: 以这个例子为例,运行它并看看发生了什么:

import pygame
from pygame.locals import *

pygame.init()
screen = pygame.display.set_mode((500,500))
run    = True

#Animation Speed
speed = 0.1

#Load an image.
player = pygame.image.load("player.png").convert_alpha()
x,y    = (0,0)

#Create a rectangular area to blit the player inside.
surf = pygame.Surface((player.get_width(),player.get_height()))


def Animate():
    global y

    if y > player.get_width():
        y = 0

    else:
        y += speed

while run:

    for event in pygame.event.get():
        if event.type==QUIT:
            run=False
            break;


    #Clear the surface where you draw the animation.
    surf.fill((255,255,255))

    #Draw the image inside the surface.
    surf.blit(player, (x,y))

    #Draw that surface on the screen.
    screen.blit(surf, (20,20))

    #Animate the image.
    Animate()

    pygame.display.update()

pygame.quit()




pygame.quit()

想象冲浪是一张纸,您在其中绘制图像,然后将那张纸放在屏幕上。

关于Pygame-Screen.blit(源,目标,区域)返回空矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56212875/

相关文章:

Javascript,Connect style.display 和 style.transition 问题

matlab - 在 MATLAB GUI 中显示分析结果

javascript - 更改选择突出显示的高度

javascript - 我需要帮助将矩形 append 一些深

python - Pygame 名称错误 : name 'image' is not defined

python - Pygame问题: how to execute conditional on collision?

python - 碰撞矩形

python - pygame 的崩溃事件

html - 如何将元素与显示表格对齐?如果您已经为您的 child 应用了 display table-cell?

swift - 如果 Sprite 的下半部分很重要,如何检查碰撞(想想有茎的樱桃)