问题描述
我正在开发一个简单的 pygame 项目,您可以通过按空格来克隆自己。我在 Turtle 中使用 Turtle.stamp()
创建了这个项目,但我不确定 pygame 中是否存在这样的东西。
解决方法
简答:
不,没有。
长答案:
但是,您可以轻松模仿它。添加“邮票”列表。此列表包含对象 (pygame.Surface
) 和必须绘制对象的位置:
stamps = []
实现函数 stamp
,将图像 (pygame.Surface
) 和位置添加到列表中:
def stamp(pos,surf):
stamps.append((surf,pos))`
添加另一个将对象绘制到目标表面的函数:
def draw_stamps(target_surf):
for pos,surf in stamps:
target_surf.blit(surf,pos)
最小示例:
import pygame
pygame.init()
window = pygame.display.set_mode((400,400))
clock = pygame.time.Clock()
turtle = pygame.Surface((48,40),pygame.SRCALPHA)
for p in [(5,5),(27,(5,35),35)]:
pygame.draw.circle(turtle,"brown",p,5)
pygame.draw.circle(turtle,"green",(40,20),8)
pygame.draw.circle(turtle,"darkgreen",(16,16)
turtle_rect = turtle.get_rect(center = (60,60))
stamps = []
def stamp(pos,surf):
stamps.append((pos,surf))
def draw_stamps(target_surf):
for pos,pos)
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
stamp(turtle_rect.topleft,turtle)
keys = pygame.key.get_pressed()
turtle_rect.x += (keys[pygame.K_d] - keys[pygame.K_a]) * 5
turtle_rect.y += (keys[pygame.K_s] - keys[pygame.K_w]) * 5
turtle_rect.clamp_ip(window.get_rect())
window.fill((255,255,255))
draw_stamps(window)
window.blit(turtle,turtle_rect)
pygame.display.flip()
pygame.quit()
exit()