2017-06-10 26 views
1

私はStackoverflowの他の質問を理解できませんでした。円が画面の端に向かって移動すると、円は反対方向に移動します。Python TypeError:引数1はpygame.Surfaceでなければならず、pygame.Rectではなく、

import pygame 
import sys 
pygame.init() 
screen = pygame.display.set_mode([640,480]) 
screen.fill([255,255,255]) 
circle = pygame.draw.circle(screen, [255,0,0],[100,100],30,0) 
x = 50 
y = 50 
x_speed = 5 
y_speed = 5 

done = "False" 
while done == "False": 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done="True" 
    pygame.time.delay(20) 
    pygame.draw.rect(screen,[255,255,255],[x,y,30,30],0) 
    x = x + x_speed 
    y = y + y_speed 
    if x > screen.get_width() - 30 or x < 0: 
     x_speed = -x_speed 
    if y > screen.get_height() - 30 or y < 0: 
     y_speed = -y_speed 
    screen.blit(circle,[x,y]) 
    pygame.display.flip() 

pygame.quit() 

エラーメッセージは実装されずに発行されます。

screen.blit(circle,[x,y])
TypeError: argument 1 must be pygame.Surface, not pygame.Rect

どうしたのですか?

答えて

1
pygame.draw.circle

pygame.Rectないpygame.Surface返すとすることができますのみblitサーフェスないrects(つまり、トレースバックがあなたを伝えるものです)。サーフェスオブジェクトを作成し、pygame.draw.circleを使用して円を描画し、このサーフェスをメインループの画面にblitします。

import pygame 
import sys 

pygame.init() 
screen = pygame.display.set_mode([640,480]) 
screen.fill([255,255,255]) 

# A transparent surface with per-pixel alpha. 
circle = pygame.Surface((60, 60), pygame.SRCALPHA) 
# Draw a circle onto the `circle` surface. 
pygame.draw.circle(circle, [255,0,0], [30, 30], 30) 

x = 50 
y = 50 
x_speed = 5 
y_speed = 5 

done = False 
while not done: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      done = True 

    pygame.time.delay(20) 
    screen.fill((40, 40, 40)) 
    x = x + x_speed 
    y = y + y_speed 
    if x > screen.get_width() - 60 or x < 0: 
     x_speed = -x_speed 
    if y > screen.get_height() - 60 or y < 0: 
     y_speed = -y_speed 
    # Now blit the surface. 
    screen.blit(circle, [x, y]) 
    pygame.display.flip() 

pygame.quit() 
sys.exit() 
+0

pygame.draw.circle(円、[255,0,0]、[30、30]、30)、 [30、30]が増加し、衝突壁は窓ウィンドウではない場合。 pygame surface()には関連していないようです。 どうしたのですか?答えを教えてください... – SsolGom

+0

'x> screen.get_width() - 30'の場合、私の例ではサークルのサイズなので、30から60に変更する必要があります。私は答えを編集しました。 – skrx

関連する問題