2016-12-10 6 views
-1

最近pythonとpygameで始まりました。私の最初のプロジェクトは、ランダムに画面に表示されるブロックをクリックする必要があるゲームを作成することです。数時間の研究と問題解決の後、私は解決策を思いつきませんでした。私は自分のコードに対する他の答えをカスタマイズするのはそれほど良くないと思います。ここでは、コードです:AttributeError: 'tuple'オブジェクトに 'collidepoint'属性がありません

import pygame 
import time 
import random 

pygame.init() 

game_width = 800 
game_height = 600 

black = (0,0,0) 
white = (255,255,255) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 

gameDisplay = pygame.display.set_mode((game_width, game_height)) 
pygame.display.set_caption('Click It') 
clock = pygame.time.Clock() 

def text_objects(text, font): 
    textSurface = font.render(text, True, red) 
    return textSurface, textSurface.get_rect() 

def message_display(text): 
    largeText = pygame.font.Font('freesansbold.ttf',115) 
    TextSurf, TextRect = text_objects(text, largeText) 
    TextRect.center = ((display_width/2),(display_height/2)) 
    gameDisplay.blit(TextSurf, TextRect) 

    pygame.display.update() 
    time.sleep(2) 
    game_loop() 

def failed(): 
    message_diplay('You Failed') 

def game_loop(): 
    thing_height = 100 
    thing_width = 100 
    thing_startx = random.randrange(0, game_width-thing_width) 
    thing_starty = random.randrange(0, game_height-thing_height) 
    rectangle = (thing_startx, thing_starty, thing_width, thing_height) 

    def things(thingx, thingy, thingw, thingh, color): 
     pygame.draw.rect(gameDisplay, color, [thingx, thingy, thingw, thingh]) 

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

     gameDisplay.fill(white) 
     things(thing_startx, thing_starty, thing_width, thing_height, blue) 

     for event in pygame.event.get(): 
      if event.type == pygame.MOUSEBUTTONDOWN: 
       click = rectangle.collidepoint(pygame.mouse.get_pos()) 

       if click == 1: 
        print ('Clicked!!') 

     pygame.display.update() 
     clock.tick(60) 

game_loop() 
pygame.quit() 
quit() 

答えて

1

あなたはタプルを作成していると(タプルが持っていない)メソッドcollidepointを使用しようとしています。あなたは、おそらくそうところで

rectangle = pygame.Rect(thing_startx, thing_starty, thing_width, thing_height) 
+0

にライン

rectangle = (thing_startx, thing_starty, thing_width, thing_height) 

を変更、代わりにRectオブジェクトを使用することを意図している:あなたはpygame.Rect'ドキュメント 'へのリンクを追加することができます。 – furas

+0

@furasそうですね、それを追加します。ありがとう! –

関連する問題