2013-04-20 81 views
0

我已經擴展pygame.Rect與我的Ball類。當我使用nstanceOfBall.colliderect()(第66行)時,不會引發錯誤,但它永遠不會返回true。深入瞭解爲什麼colliderect不適用於我的Ball實例?從超類通過子類的Python調用方法不按預期工作

import sys, pygame 
    import os 
    import ball 
    import random 
    import math 
    #from ball import Ball 

    ############################################################################################### 

    class Ball(pygame.Rect): 
     def __init__(self, x, y, width, height, screenWidth, screenHeight): 
      super(pygame.Rect,self).__init__(x, y, width, height) 
      self.floatX=x 
      self.floatY=x 
      self.screenWidth=screenWidth 
      self.screenHeight=screenHeight 
      self.speed=[random.random()*5-2.5, random.random()*5-2.5] 

     def runLogic(self): 
      self.floatX+=self.speed[0] 
      self.floatY+=self.speed[1] 

      if self.floatX+16<0: self.floatX=self.screenWidth 
      if self.floatX>self.screenWidth: self.floatX=-16 
      if self.floatY+16<0: self.floatY=self.screenHeight 
      if self.floatY>self.screenHeight: self.floatY=-16 

      self.x=self.floatX 
      self.y=self.floatY 

    ############################################################################################### 

    os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (700, 200)#1680,1440) 

    pygame.init() 

    size = width, height = 320, 240 
    white = 255, 255, 255 
    blue=0,0,255 

    screen = pygame.display.set_mode(size) 

    image = pygame.image.load("ball.png") 
    ballRect=image.get_rect() 
    balls = [] 

    for x in range(0, 100): 
     balls.append(Ball(random.random()*width, random.random()*height, ballRect.width, ballRect.height, width, height)) 

    lastFrameStartTime=pygame.time.get_ticks() 

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

      if event.type == pygame.KEYDOWN: 
       if event.key == pygame.K_ESCAPE: 
        sys.exit() 

     for ball in balls: 
      ball.runLogic() 

      for ball2 in balls: 
       if ball != ball2:     
        if ball.colliderect(ball2): 
         print("collision detected!") 
         balls.pop(balls.index(ball)) 
         balls.pop(balls.index(ball2)) 
         #balls.remove(ball2) 

     screen.fill(blue) 
     for ball in balls: 
      screen.blit(image, ball) 
     pygame.display.flip() 

     pygame.time.wait(33-(pygame.time.get_ticks()-lastFrameStartTime)) 
     lastFrameStartTime=pygame.time.get_ticks() 

在Ball.init採用超(球,個體經營),而不是超(pygame.Rect,個體經營)並獲得成功。

謝謝。

+1

在'Ball .__ init__'中,你應該調用'super(Ball,self)'而不是'super(pygame.Rect,self)''。請參閱[超級文檔](http://docs.python.org/2/library/functions.html#super)。 – 2013-04-20 00:29:51

+0

謝謝!那樣做了。 ballInstance.colliderect不符合要求。 – user2301013 2013-04-20 00:44:32

+0

@NCao你應該做出答案,以便可以接受。 – Keith 2013-04-20 05:16:27

回答

0

第一arument到super必須是當前類,即Ball代替pygame.Rect

class Ball(pygame.Rect): 
    def __init__(self, x, y, width, height, screenWidth, screenHeight): 
     super(Ball, self).__init__(x, y, width, height) 
     ... 

有一個在documentation on super一個例子。

super的意義在於它允許您按繼承順序調用下一個方法,而不需要顯式引用父類。這在使用多重繼承時非常方便,請參閱super confusing python multiple inheritance super()