【问题标题】:Python calling method from super class via child class not working as expected通过子类从超类调用Python方法未按预期工作
【发布时间】:2013-04-13 11:22:58
【问题描述】:

我已经用我的Ball 类扩展了pygame.Rect。当我使用nstanceOfBall.colliderect()(第 66 行)时,不会抛出任何错误,但它永远不会返回 true。知道为什么 colliderect 不适用于我的 Ball 实例吗?

    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 中使用 super(Ball, self) 而不是 super(pygame.Rect, self) 可以解决问题。

谢谢。

【问题讨论】:

  • Ball.__init__ 中,您应该调用super(Ball, self) 而不是super(pygame.Rect, self)。请参阅documentation on super
  • 谢谢!做到了。 ballInstance.colliderect 的行为不符合预期。
  • @NCao 你应该这样回答才能被接受。

标签: python inheritance polymorphism pygame duck-typing


【解决方案1】:

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()

【讨论】:

    猜你喜欢
    • 2013-07-16
    • 1970-01-01
    • 2021-10-13
    • 2020-10-04
    • 1970-01-01
    • 2011-12-08
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多