2015-11-18 182 views
0

接下來的問題是,我有下降的圓圈,如果它們與玩家重疊,我需要刪除它們。我試圖創建一堆方法來獲取圓和矩形的座標,但是當我嘗試檢查它們是否重疊時,我得到一個錯誤。矩形和圓形之間的碰撞

類型錯誤:unorderable類型:方法()>方法()

下面是代碼:

# Colour 
# Created by Niktia Kotter 

#!/usr/bin/env python 
import pygame, sys, random, time 
from pygame.locals import* 

# set up pygame 
pygame.init() 
FPS=60 
fpsclock = pygame.time.Clock() 

# colours R G B 
WHITE = (255, 255, 255) 
BLACK = (0 , 0 , 0 ) 
RED  = (237, 28 , 36) 

# set up screen 
SCREEN_W = 800 
SCREEN_H = 480 
SCREEN = pygame.display.set_mode((SCREEN_W,SCREEN_H),0,32)    
snapMultX = SCREEN_W/5 
snapMultY = SCREEN_H/5 
basicFont = pygame.font.SysFont(None, 32) 

# set up functions 
def isPointInsideRect(Cx, Cy, rectX, rectY, rectW, rectH): 
    if ((Cx > rectX) and \ 
    (Cx < rectY) and \ 
    (Cy > rectW) and \ 
    (Cy < rectH)): 
     return True 
    else: 
     return False 

""" 
def doRectsOverlap(rect1, rect2): 
    for a,b in [(rect1, rect2), (rect2, rect1)]: 
     # check if a's corners are inside b 
     if ((isPointInsideRect(a.left, a.top, b)) or 
      (isPointInsideRect(a.left, a.bottom, b)) or 
      (isPointInsideRect(a.right, a.top, b)) or 
      (isPointInsideRect(a.right, a.bottom, b))): 
      return True 

    return False 
""" 

# set up calsses 
class Actor: 

    def __init__ (self): 


     self._x = snapMultX*2 
     self._y = SCREEN_H - snapMultX/5 -(snapMultX/2) 
     self._w = snapMultX 
     self._h = snapMultX/2 
     self._colour = WHITE 
     self._Rect = pygame.Rect(self._x, self._y, self._w, self._h) 

    def moveRight(self): 
     self._x += snapMultX 

    def moveLeft(self): 
     self._x -= snapMultX 

    def draw(self): 
     pygame.draw.rect(SCREEN, self._colour, (self._x, self._y, self._w, self._h)) 
     return 
    def rectX(self): 
     return self._x 
    def rectY(self): 
     return self._y 
    def rectW(self): 
     return self._w 
    def rectH(self): 
     return self._h 

class Enemy: 

    def __init__ (self, location): 

     self._x = snapMultX*location+snapMultX/2 
     self._y = 0 
     self._r = snapMultX/10 
     self._colour = WHITE 

    def move(self, dy): 

     self._y += dy 

    def draw(self): 
     pygame.draw.circle(SCREEN, self._colour, (int(self._x),int(self._y)), int(self._r), 0) 
     return 

    def GetCircleX(self): 
     return self._x 

    def GetCircleY(self): 
     return self._y 

class Capture(object): 

    def __init__(self): 

     self.caption = pygame.display.set_caption('Space Invaders') 
     self.screen = SCREEN 
     self.startGame = True 
     self.gameOver = False 
     self.enemyCount = 0 
     self.timer = 50 
     self.score = 0 

    def main(self): 

     clock = pygame.time.Clock() 
     enemy =[] 
     player = Actor() 

     while True: 
      if self.startGame: 

       SCREEN.fill(BLACK) 

       pygame.draw.polygon(SCREEN,WHITE, [(snapMultX*1-snapMultX/5*2,0), (snapMultX*0+snapMultX/5*2,0), (snapMultX*0+snapMultX/2,snapMultY/4)]) 
       pygame.draw.polygon(SCREEN,WHITE, [(snapMultX*2-snapMultX/5*2,0), (snapMultX*1+snapMultX/5*2,0), (snapMultX*1+snapMultX/2,snapMultY/4)]) 
       pygame.draw.polygon(SCREEN,WHITE, [(snapMultX*3-snapMultX/5*2,0), (snapMultX*2+snapMultX/5*2,0), (snapMultX*2+snapMultX/2,snapMultY/4)]) 
       pygame.draw.polygon(SCREEN,WHITE, [(snapMultX*4-snapMultX/5*2,0), (snapMultX*3+snapMultX/5*2,0), (snapMultX*3+snapMultX/2,snapMultY/4)]) 
       pygame.draw.polygon(SCREEN,WHITE, [(snapMultX*5-snapMultX/5*2,0), (snapMultX*4+snapMultX/5*2,0), (snapMultX*4+snapMultX/2,snapMultY/4)])     

       player.draw() 

# enemy move/spawn timer 
       self.timer -= 1 

# enemy spawner 
       if self.timer <= 0: 

        num = random.randint(0, 5) 

        if num == 0: 
         print (0) 
         enemy.append(Enemy(0)) 
        if num == 1: 
         print (1) 
         enemy.append(Enemy(1)) 
        if num == 2: 
         print (2) 
         enemy.append(Enemy(2)) 
        if num == 3: 
         print (3) 
         enemy.append(Enemy(3)) 
        if num == 4: 
         print (4) 
         enemy.append(Enemy(4)) 

# player mover 
       for event in pygame.event.get(): 

        if player._x != snapMultX*4 and (event.type == KEYDOWN) and (event.key == K_d): 
         player.moveRight() 

        if player._x != 0 and(event.type == KEYDOWN) and (event.key == K_a): 
         player.moveLeft() 

        if event.type == QUIT: 
         pygame.quit() 
         sys.exit() 
# enemy logic 
       if self.timer <= 0: 
        for e in enemy: 
         e.move(snapMultY) 

         if isPointInsideRect(e.GetCircleX, e.GetCircleY, player.rectX, player.rectY, player.rectW, player.rectH): 
          self.score += 1 
          enemy.remove(e) 

         if e._y > snapMultY*5:  
          enemy.remove(e) 
# reste timer 
        self.timer = 50      

       for e in enemy: 
        e.draw() 

# score 
       self.myScore = "Score = " + str(self.score) 
       text = basicFont.render(self.myScore, True, RED, WHITE) 
       textRect = text.get_rect() 
       textRect.centerx = SCREEN.get_rect().centerx 
       textRect.centery = SCREEN.get_rect().centery 
       SCREEN.blit(text, textRect) 

       pygame.display.update() 
       fpsclock.tick(FPS) 

if __name__ == '__main__': 
    game = Capture() 
    game.main()  
+2

的行號,發生錯誤,將有助於... – Falko

+0

[用圓圈檢測用矩形碰撞(的可能的複製http://stackoverflow.com/questions/24727773 /檢測矩形 - 與圓形碰撞) – sloth

+0

該錯誤發生在isPointInsideRect函數 – FerrenReve

回答

1

你的錯誤的原因實際上是一個錯字和Python鴨打字的樂趣。調試的關鍵是瞭解錯誤的含義。

您的錯誤「TypeError:unorderable types:method()> method()」。這是什麼意思? 顯然你有一個類型錯誤,但這是什麼意思。這意味着Python正在嘗試完成具有特定要求的操作。在這種情況下,對於無法判定的類型做了一些處理 - 我們試圖對沒有可訂購屬性的兩件事進行比較,以便不能以這種方式進行比較。下一部分說「method()> method()」。這意味着我們試圖比較一種方法比另一種方法更大。這可能不是目的。

所以現在我們必須看看我們的ifs中的那些比較。讓我們看看isPointInsideRect。

def isPointInsideRect(Cx, Cy, rectX, rectY, rectW, rectH): 
    if ((Cx > rectX) and (Cx < rectY) and (Cy > rectW) and (Cy < rectH)): 
     return True 
    else: 
     return False 

這裏我們對6個值做了大量的比較。讓我們看看這個方法被調用的地方?該函數在一個未註釋行中被調用(第175行)。

if isPointInsideRect(e.GetCircleX, e.GetCircleY, player.rectX, player.rectY, player.rectW, player.rectH): 

你在這裏看到問題了嗎?您傳遞給函數的每個值都不是值,它們是方法定義。因此,每次isPointInsideRect都在做這些比較,他們正在比較一種方法與另一種方法。這不是一個有效的比較。

試圖改變線路175:

​​