Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.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 想给sprite pygame添加一条线索吗_Python_Pygame - Fatal编程技术网

Python 想给sprite pygame添加一条线索吗

Python 想给sprite pygame添加一条线索吗,python,pygame,Python,Pygame,我试图创造一个简单的蛇游戏,留下一个线索,它自己 我已经尝试过查看pygame.display.update和alphasurf,但我只想让rect本身留下一条简单的轨迹 就像它在移动时创造了自己的复制品一样 我也不能仅仅删除screen.fill,因为我还想添加其他移动部件。根据,可以这样做的一种方法是创建一个具有屏幕大小的透明曲面,在该曲面上用轨迹将对象点到该曲面上。它需要是每像素alpha曲面,您可以通过传递pygame.SRCALPHA特殊标志来创建该曲面。 Blit对象,并通过使用透明

我试图创造一个简单的蛇游戏,留下一个线索,它自己

我已经尝试过查看pygame.display.update和alphasurf,但我只想让rect本身留下一条简单的轨迹

就像它在移动时创造了自己的复制品一样

我也不能仅仅删除screen.fill,因为我还想添加其他移动部件。根据,可以这样做的一种方法是创建一个具有屏幕大小的透明曲面,在该曲面上用轨迹将对象点到该曲面上。它需要是每像素alpha曲面,您可以通过传递
pygame.SRCALPHA
特殊标志来创建该曲面。 Blit对象,并通过使用透明白色填充每个帧来减少alpha\u surf上所有像素的alpha值,同时传递
pygame.BLEND\u RGBA\u MULT
标志,以便仅影响alpha通道。
例如:

import pygame as pg
from pygame.math import Vector2
class Player(pg.sprite.Sprite):
def __init__(self, pos, *groups):
super().__init__(*groups)
self.image = pg.Surface((50, 50), pg.SRCALPHA)
pg.draw.circle(self.image, pg.Color('dodgerblue'), (25, 25), 25)
self.rect = self.image.get_rect(center=pos)
self.vel = Vector2(0, 0)
self.pos = Vector2(pos)
def update(self):
self.pos += self.vel
self.rect.center = self.pos
def main():
pg.init()
screen = pg.display.set_mode((640, 480))
# Blit objects with trails onto this surface instead of the screen.
alpha_surf = pg.Surface(screen.get_size(), pg.SRCALPHA)
clock = pg.time.Clock()
all_sprites = pg.sprite.Group()
player = Player((150, 150), all_sprites)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
return
elif event.type == pg.KEYDOWN:
if event.key == pg.K_d:
player.vel.x = 5
elif event.key == pg.K_a:
player.vel.x = -5
elif event.key == pg.K_w:
player.vel.y = -5
elif event.key == pg.K_s:
player.vel.y = 5
elif event.type == pg.KEYUP:
if event.key == pg.K_d and player.vel.x > 0:
player.vel.x = 0
elif event.key == pg.K_a and player.vel.x < 0:
player.vel.x = 0
elif event.key == pg.K_w:
player.vel.y = 0
elif event.key == pg.K_s:
player.vel.y = 0
# Reduce the alpha of all pixels on this surface each frame.
# Control the fade speed with the alpha value.
alpha_surf.fill((255, 255, 255, 220), special_flags=pg.BLEND_RGBA_MULT)
all_sprites.update()
screen.fill((20, 50, 80)) # Clear the screen.
all_sprites.draw(alpha_surf) # Draw the objects onto the alpha_surf.
screen.blit(alpha_surf, (0, 0)) # Blit the alpha_surf onto the screen.
pg.display.flip()
clock.tick(60)
if __name__ == '__main__':
main()
pg.quit()
将pygame导入为pg
从pygame.math导入向量2
职业玩家(第三页精灵):
定义初始化(自我、位置、*组):
超级()
self.image=pg.Surface((50,50),pg.SRCALPHA)
画圈(self.image,画圈颜色('dodgerblue'),(25,25),25)
self.rect=self.image.get_rect(中心=pos)
self.vel=Vector2(0,0)
self.pos=矢量2(pos)
def更新(自我):
self.pos+=self.vel
self.rect.center=self.pos
def main():
第init页()
屏幕=pg.display.set_模式((640480))
#将带有轨迹的对象显示在此曲面上,而不是屏幕上。
alpha_surf=pg.Surface(screen.get_size(),pg.SRCALPHA)
时钟=pg.time.clock()
所有精灵=pg.sprite.Group()
玩家=玩家((150150),所有精灵)
尽管如此:
对于pg.event.get()中的事件:
如果event.type==pg.QUIT:
返回
elif event.type==pg.KEYDOWN:
如果event.key==pg.K\u d:
player.vel.x=5
elif event.key==pg.K_a:
player.vel.x=-5
elif event.key==pg.K_w:
player.vel.y=-5
elif event.key==pg.K_s:
玩家等级y=5
elif event.type==pg.KEYUP:
如果event.key==pg.K_d且player.vel.x>0:
player.vel.x=0
elif event.key==pg.K_a和player.vel.x<0:
player.vel.x=0
elif event.key==pg.K_w:
player.vel.y=0
elif event.key==pg.K_s:
player.vel.y=0
#每帧减少此曲面上所有像素的alpha。
#使用alpha值控制淡入淡出速度。
阿尔法表面填充((255、255、255、220),特殊标志=pg.BLEND\u RGBA\u MULT)
所有精灵更新()
屏幕。填充((20,50,80))#清除屏幕。
所有精灵。绘制(alpha_surf)#将对象绘制到alpha_surf上。
blit(alpha_surf,(0,0))#blit将alpha_surf显示在屏幕上。
pg.display.flip()
时钟滴答(60)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
第页退出

干杯。

我完全不明白这一点。一个简单的方法是保存一份蛇“身体”部位的历史坐标列表。当蛇“成长”时,向列表中添加一个新元素。要在屏幕上绘制蛇,请在列表上循环,绘制每个元素。当蛇移动时,更新每个身体部位的坐标。如果每个身体部位都相同,也可以对其进行优化,以便只需移动头部和尾部。