2016-09-02 7 views
0

簡略化されたロケットである長方形を描きたいのですが、pycharmは画像のようにargument 1 must be pygame.Surface, not pygame.Rectです。 enter image description here"rect"クラスはメソッド "screen.blit()"のパラメータではありません

次は私のコードです:

screen = pygame.display.set_mode([400, 600]) 
screen.fill([0, 0, 0]) 
ship = pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0) 
moon = pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0) 
screen.blit(moon, [0, 500, 400, 100]) 

それから私はこれに私のコードを変更:

screen = pygame.display.set_mode([400, 600]) 
screen.fill([0, 0, 0]) 
ship_mode = pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0) 
ship = ship_mode.convert() 
moon_mode = pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0) 
moon = moon_mode.convert() 
screen.blit(moon, [0, 500, 400, 100]) 

今回、それは私が期待してい何を終えることができましたどのようにTraceback (most recent call last): File "E:/untitled/launching rocket.py", line 11, in <module> ship = ship_mode.convert() AttributeError: 'pygame.Rect' object has no attribute 'convert' を示していますか?

答えて

1

関数pygame.draw.rectはSurface(引数として指定したもの)に直接描画し、描かれた領域を表すRectオブジェクトを返します。 Surfaceを返しません。

に矩形が描画されているため、screen.blit(moon, [0, 500, 400, 100])行を削除することができます。矩形が描かれた場所への参照が必要な場合を除いて、関数を何かに割り当てる必要はありません。

import pygame 
pygame.init() 

screen = pygame.display.set_mode([400, 600]) 
screen.fill([0, 0, 0]) 
pygame.draw.rect(screen, [255, 22, 150], [200, 400, 50, 100], 0) 
pygame.draw.rect(screen, [79, 200, 145], [0, 550, 400, 50], 0) 

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

    pygame.display.update() 

あなたは私はあなたがこのような何かを示唆周りの画像を移動したい場合:

import pygame 
pygame.init() 

screen = pygame.display.set_mode([400, 600]) 

ship_image = pygame.Surface((50, 100)) # Create a rectangular Surface. 
ship_image.fill((255, 22, 150)) # Fill it with a color. 
ship_rect = pygame.Rect(200, 400, 50, 100) # Create a rect for where to blit the ship. 

moon_image = pygame.Surface((400, 50)) # Create a rectangular Surface. 
moon_image.fill((79, 200, 145)) # Fill it with a color. 
moon_rect = pygame.Rect(0, 550, 400, 50) # Create a rect for where to blit the ship. 


while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      quit() 
     elif event.type == pygame.KEYDOWN: # If you press a key down. 
      if event.key == pygame.K_RIGHT: # If the key is the right arrow key. 
       ship_rect.move_ip(5, 0) # Move the rect to the right. 
      elif event.key == pygame.K_LEFT: 
       ship_rect.move_ip(-5, 0) 
      elif event.key == pygame.K_UP: 
       ship_rect.move_ip(0, -5) 
      elif event.key == pygame.K_DOWN: 
       ship_rect.move_ip(0, 5) 

    # Cover up the screen with the background color. 
    # This is to erase all images on the screen. 
    # Try removing this line to see why we want to do so. 
    screen.fill((0, 0, 0)) 

    screen.blit(ship_image, ship_rect) # Blit the ship image at the position of the ship_rect. 
    screen.blit(moon_image, moon_rect) # Blit the moon image at the position of the moon_rect. 

    pygame.display.update() # Update the display so all changes are visable 
+0

それから私は、この 'ship.rect.top = y_pos'のような私の船の位置を変更したいです、しかしそれはうまくいかなかった。しかし、私の船は 'ship = pygame.draw.rect(screen、[255、22、150]、[200,400,50,100]、0)'を描いているので、私のインタプリタはどうすれば 'AttributeError: 'pygameと言うことができますか? Rect 'オブジェクトに' rect 'という属性はありませんか? –

+0

チュートリアルをご覧ください。私は単純なゲーム[ここ](http://stackoverflow.com/documentation/pygame/3959/introduction-to-pygame/14697/a-simple-game#t=201609031439464337092)について簡単な説明をしましたあなたの質問に答えるには:描画されたものは画面上に移動することはできません。 [この回答](http://stackoverflow.com/questions/38904986/pygame-how-to-replace-an-image-with-an-other-one/38907531#38907531)をご覧ください。あなたの他の質問に答えるには、 'ship'はRectオブジェクトであり、Rectオブジェクトは' rect'という属性を持っていません。私は私の答えに短い例を加えました。 –

関連する問題