【问题标题】:Pygame- Screen.blit(source, dest, area) returns empty rectanglePygame- Screen.blit(source, dest, area) 返回空矩形
【发布时间】:2019-10-06 08:21:10
【问题描述】:

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

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

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)?你想在里面玩吗?
  • 我正在尝试在orect 的参数内将oSprite.image 传送到screen

标签: pygame display rect


【解决方案1】:

您需要在 oSprite.image 表面上进行 blit,而不是在屏幕表面上。

这将在 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()

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

【讨论】:

  • 那不是需要我为动画的每一帧制作一个新图像吗?我只希望精灵向下移动,并且只希望在动画期间更改 orect 中的区域。
  • 我整理好了,谢谢。我误解了你原来的意思。
猜你喜欢
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 1970-01-01
  • 2011-06-30
相关资源
最近更新 更多