2017-05-27 77 views
0

所以我在搞pygame,事情是我有一個球,而當我移動這個球,我希望它離開褪色的陰影痕跡,但事情是它發生在「陰影痕跡」幾乎看不見的地方。Pygame中物體的移動速度如何?

調整「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 而球是最黑暗的人物,然後離開較輕的,因爲它移動(路徑),如果沒有運動發生,最終消失。

預先感謝您。

回答

0

clock.tick(40)只需設置您的遊戲的FPS限制。

雖然不是很清楚,但我認爲通過說「背景效果」,您的意思是主循環的速度,它由clock.tick定義。因此,不要將40更改爲其他值,您可以簡單地減少每個移動的像素以降低移動對象的速度。

+0

我說的背景效果是在屏幕上發生的動畫,側面的線條動畫,我希望它保持儘可能快的速度。無論我減少或增加球的運動,陰影仍然太快,幾乎看不見。我希望我的球效果能夠像我的動畫一樣工作,而當我的球移動時,它會留下一條白色到黑色的漸變線,與我的背景效果非常相似,其中最輕的線條就像球和淡出的線條就是它留下的線條。 改變球的顏色使「小徑」更清晰可見。 –

+0

它看起來像你需要進入「球/陰影」的代碼,併爲它定義一個速度,就像你爲背景效果做的那樣。 –