简体   繁体   English

通过子类从父类进行Python调用的方法无法正常工作

[英]Python calling method from super class via child class not working as expected

I have extended pygame.Rect with my Ball class. 我在Ball类中扩展了pygame.Rect When I use nstanceOfBall.colliderect() (line 66), no errors are thrown, yet it never returns true. 当我使用nstanceOfBall.colliderect() (第66行)时,不会引发任何错误,但它永远不会返回true。 Any insight into why colliderect isn't working for my Ball instances? 关于为何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()

Using super(Ball, self) instead of super(pygame.Rect, self) in Ball.init did the trick. 在Ball.init中使用super(Ball,self)代替super(pygame.Rect,self)可以解决问题。

Thank you. 谢谢。

The first arument to super must be the current class, ie Ball instead of pygame.Rect : 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)
        ...

There is an example in the documentation on super . 在super文档中有一个示例。

The point of super is that it allows you to call the next method in the inheritance order without requiring you to refer to the parent class explicitly. super的要点是,它允许您按继承顺序调用下一个方法,而无需您显式引用父类。 This comes handy when using multiple inheritance, see eg super confusing python multiple inheritance super() 使用多重继承时,这很方便,例如参见超级混淆python多重继承super()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM