python - Pygame,如何在屏幕上绘制形状并删除之前的表面?

标签 python python-3.x pygame

所以我有这段代码,它可以正常工作。我想要它做的是按不同的数量随机缩放正方形,它确实这样做了。我的问题在于 blit 函数,我的正方形似乎只是按比例放大,因为 blit 不会删除旧形状,它只是将新形状复制到表面。

如何使形状扩大和缩小,而不仅仅是扩大?

我的代码:

import sys, random, pygame
from pygame.locals import *

pygame.init()

w = 640
h = 480

screen = pygame.display.set_mode((w,h))
morphingShape = pygame.Surface((20,20))
morphingShape.fill((255, 137, 0)) #random colour for testing
morphingRect = morphingShape.get_rect()

def ShapeSizeChange(shape, screen):
    x = random.randint(-21, 20)
    w = shape.get_width()
    h = shape.get_height()
    if w + x > 0 and h + x > 0:
        shape = pygame.transform.smoothscale(shape, (w + x, h + x))
    else:
        shape = pygame.transform.smoothscale(shape, (w - x, h - x))
    shape.fill((255, 137, 0))
    rect = shape.get_rect()
    screen.blit(shape, rect)
    return shape


while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    morphingShape = ShapeSizeChange(morphingShape, screen)
    pygame.display.update()

最佳答案

在每一帧(While 循环的每次迭代)你都应该删除屏幕。默认情况下,屏幕(窗口)颜色为黑色,因此您应该通过调用 screen.fill( (0,0,0) ) 清除屏幕。下面是完整的代码,现在可以正常工作了:

import sys, random, pygame
from pygame.locals import *

pygame.init()

w = 640
h = 480

screen = pygame.display.set_mode((w,h))
morphingShape = pygame.Surface((20,20))
morphingShape.fill((255, 137, 0)) #random colour for testing
morphingRect = morphingShape.get_rect()

# clock object that will be used to make the animation
# have the same speed on all machines regardless
# of the actual machine speed.
clock = pygame.time.Clock()

def ShapeSizeChange(shape, screen):
    x = random.randint(-21, 20)
    w = shape.get_width()
    h = shape.get_height()
    if w + x > 0 and h + x > 0:
        shape = pygame.transform.smoothscale(shape, (w + x, h + x))
    else:
        shape = pygame.transform.smoothscale(shape, (w - x, h - x))
    shape.fill((255, 137, 0))
    rect = shape.get_rect()
    screen.blit(shape, rect)
    return shape


while True:
    # limit the demo to 50 frames per second
    clock.tick( 50 );

    # clear screen with black color
    # THIS IS WHAT WAS REALLY MISSING...
    screen.fill( (0,0,0) )

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()
    morphingShape = ShapeSizeChange(morphingShape, screen)
    pygame.display.update()

请注意,只需添加 screen.fill( (0,0,0) ) 即可解决您的问题。

关于python - Pygame,如何在屏幕上绘制形状并删除之前的表面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11767236/

相关文章:

python-3.x - Ctrl + t - 打开新标签在 Selenium + Firefox Python 中不起作用

python - Pygame 文本未绘制

python - Pyganim 在 Sidescroller 游戏中的帮助

Python - 调用覆盖方法时发生 TypeError

python - ValueError : malformed string when using ast. literal_eval

python - 使用 Python 和 NumPy 组合数组

python - 如何使用python提取图像和图像BBox坐标?

python-3.x - 计算滚动 3 天 Pandas 中的不同计数?

python - 在 tkinter 标签中显示本地化日期会导致文本损坏

python - 在 Pygame 中获取等距平铺鼠标选择