你怎么能在Pygame中移动物体的速度?

时间:2017-05-27 13:55:40

标签: python pygame

所以我正在搞乱pygame,问题是我有一个球,而当我移动这个球时,我希望它留下褪色的阴影痕迹,但事情就是快速发生在“阴影痕迹”几乎看不到的地方。 / p>

调整“clock.tick()”可以解决这个问题,但我只想要那个特定的对象,在这种情况下,球是唯一一个减速的球。我不一定需要降低速度,但我认为我可以做的事情是让我的褪色阴影痕迹可见。这是代码:

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 而球是最黑暗的数字,然后它会移动(小径)时更轻的,如果没有移动就会消失。

提前谢谢。

1 个答案:

答案 0 :(得分:0)

clock.tick(40)只需为游戏设置FPS限制即可。

虽然不清楚,但我认为通过说“背景效应”,你的意思是主循环的速度由clock.tick定义。因此,您可以简单地减少每次移动的像素,以降低移动对象的速度,而不是将40更改为其他值。