【问题标题】:Pygame, how to draw a shape to the screen and delete the previous surface?Pygame,如何在屏幕上绘制一个形状并删除以前的表面?
【发布时间】:2012-07-30 19:41:51
【问题描述】:

所以我有这个代码,它做了它应该做的事。我想要它做的是将正方形随机缩放不同的数量,它确实如此。我的问题在于 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()

【问题讨论】:

    标签: python python-3.x pygame


    【解决方案1】:

    在每一帧(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) ) 即可解决您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多