Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pygame,如何在屏幕上绘制形状并删除之前的曲面?_Python_Python 3.x_Pygame - Fatal编程技术网

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

Python Pygame,如何在屏幕上绘制形状并删除之前的曲面?,python,python-3.x,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,

所以我有这个代码,它做了应该做的事情。我想让它做的是随机缩放不同数量的正方形,它会这样做。我的问题在于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))
即可解决您的问题