2012-06-03 215 views
1

我正在試着製作一個圓形動畫,並希望它在窗口內反彈,只是爲了練習。我被告知pygame.draw.rect返回Rect對象,但是,我做了這個小代碼,看看它是否工作,並沒有。圓圈不像預期的那樣每次迭代向下移動一個像素。可能是什麼問題?pygame圓圈動畫

import pygame, sys 
from pygame.locals import * 

pygame.init() 
screen = pygame.display.set_mode((600, 500), 0, 32) 
mainClock = pygame.time.Clock() 

screen.lock() 
circleRect = pygame.draw.circle(screen, (255, 255, 255), (100, 200), 40) 
screen.unlock() 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    circleRect.top += 1 

    pygame.display.update() 
    mainClock.tick(40) 
+0

由於這只是pygame教程中顯示的第一項任務,我建議你閱讀它。 – msw

回答

0

您必須在每一幀中繪製圓圈並更新矩形。提示:您需要稍微編輯並移動一行

好的,現在您正在繪製圓,但是您將它繪製在相同位置。仔細看看你通過繪製的論據。你是在哪裏告訴它畫圓?

+0

對不起,我不太明白你的意思?你是否建議我應該在while循環內繪製圓形函數?我試過了,而圈子依然沒有移動。 – geekkid

0

在這裏,你只是畫圈到(100, 200)和存儲rect以備後用(即使你再也不使用它)。

screen.lock() 
circleRect = pygame.draw.circle(screen, (255, 255, 255), (100, 200), 40) 
screen.unlock() 

你趕上從圓到屏幕的blitrect。稍後無所謂,因爲circleRect不會再次使用,所以您正在移動circleRect。您將不得不更改您的代碼,以便每次在circleRect的位置繪製該圓圈,而不是在(100, 200)處繪製該圓圈。

circleRect= = pygame.Rect(100,200, 0, 0) 
screen.lock() 
circleRect = pygame.draw.circle(screen, (255, 255, 255), circleRect, 40) 
screen.unlock() 

,然後在你的主循環,你需要像以前一樣重繪同一行了一圈,剛下top ATTRIB的調整:

circleRect.top += 1 
circleRect = pygame.draw.circle(screen, (255, 255, 255), circleRect, 40)