python控制机器移动速度_在Pygame中,你怎么知道一个物体的移动速度?

本文介绍了如何在Pygame中控制单个物体(如球)的移动速度,使其离开渐变的影子轨迹。通过调整时钟滴答频率解决了问题,但希望仅减缓球的移动速度,保持轨迹清晰可见。示例代码展示了如何实现这一目标,包括物体移动、影子轨迹的绘制以及背景效果的更新。
摘要由CSDN通过智能技术生成

所以我在玩pygame,问题是我有一个球,而当我移动这个球的时候,我希望它离开逐渐消失的影子轨迹,但问题是它发生在“影子轨迹”几乎看不见的地方。在

调整“时钟滴答声()“很好地解决了这个问题,但我只想要那个特定的物体,在这种情况下,球是唯一减速的物体。我不需要降低速度,但我想这是我可以做的事情,使我的褪色阴影痕迹可见。代码如下:import pygame

pygame.init()

game_over = False

BLACK = ( 0, 0, 0)

WHITE = (255, 255, 255)

screen_size = (500, 500)

ball_x, ball_y = 261.5, 261.5

post_xa, post_ya, post_xb, post_yb = 261.5, 261.5, 261.5, 261.5

line = []

ival = 0

clock = pygame.time.Clock()

screen = pygame.display.set_mode(screen_size)

pygame.display.set_caption("Keeping it Small and Simple")

for x, y in zip(range(250, -1, -5), range(0, 250, 5)):

line.append([y, 0, 0, x])

for x, y in zip(range(0, 250, 5), range(250, 500, 5)):

line.append([500, x, y, 0])

for x, y in zip(range(500, 250, -5), range(250, 500, 5)):

line.append([500, y, x, 500])

for x, y in zip(range(250, -1, -5), range(500, 250, -5)):

line.append([0, y, x, 500])

while not game_over:

screen.fill(BLACK)

for event in pygame.event.get():

if event.type == pygame.QUIT:

game_over = True

pressed = pygame.key.get_pressed()

if pressed[pygame.K_UP]:

ball_y -= 8

post_ya -= 8

if pressed[pygame.K_DOWN]:

ball_y += 8

post_ya += 8

if pressed[pygame.K_LEFT]:

ball_x -= 8

post_xa -= 8

if pressed[pygame.K_RIGHT]:

ball_x += 8

post_xa += 8

#Code for the shadow

colorb = 0

for i in range(8):

colors = colorb, colorb, colorb

pygame.draw.circle(screen, (colors), (int(post_xb), int(post_yb)), int(21.5))

colorb += 30

if post_xa > post_xb:

post_xb += 1

if post_xa < post_xb:

post_xb -= 1

if post_ya > post_yb:

post_yb += 1

if post_ya < post_yb:

post_yb -= 1

pygame.draw.circle(screen, WHITE, (int(ball_x), int(ball_y)), int(21.5))

# background effect

col = 0

cur = ival

for i in range (40):

x1, y1, x2, y2 = line[cur]

pygame.draw.line(screen, (col,col,col), (x1, y1), (x2, y2), 2)

cur += 1

if cur >= len(line):

cur = 0

col += 240 / 40

ival += 1

if ival >= len(line):

ival = 0

pygame.display.flip()

clock.tick(40)

pygame.quit()

下面是一个粗略的例子:

https://i.ytimg.com/vi/DzTyqcXhitY/maxresdefault.jpg

而球是最暗的,当它移动(轨迹)时,它会留下较亮的图形,如果没有移动,这些图形最终会消失。在

提前谢谢你。在

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值