因为某种原因,我在Pygame中没有画线

2024-09-29 00:15:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我在游戏中有一个墙类,它由一条线组成一堵墙。我计划将所有这些墙实例传递到一个房间实例中,该房间实例由一个房间实例和其中的墙实例组成。但是,由于某些原因,当我尝试绘制这些线时,它们不会显示在窗口中。你知道吗

我尝试了多个不同的类,以看看什么是有效的,但没有一个是有效的。我尝试创建一个Room类,它使用pygame.draw.lines()来绘制所有的墙。但是,我不知道如何检查这些行的碰撞检测。我还尝试创建另一个Room类,它使用pygame.draw.polygon(),并使用一个未填充的多边形,以便在播放器和房间边缘之间进行碰撞检测。然而,我意识到这可能不起作用,因为我需要在每个房间的门。我计划包括各种各样的墙,在它们之间有门,基本上是透明的墙,可以穿过。我不知道如何处理多边形。无论如何,这是我的密码。你知道吗

class Game(PygameGame):

    def init(self):
        super().__init__()
        Player.init()
        #Ghost.init()
        self.playerGroup=pygame.sprite.Group(Player(400,400))
        self.wallGroup=pygame.sprite.Group()
        wall1StartPos=(300,400)
        wall1EndPos=(400,300)
        wall1=Wall(wall1StartPos,wall1EndPos)
        self.wallGroup.add(wall1)
        wall2StartPos=(400,300)
        wall2EndPos=(550,300)
        wall2=Wall(wall2StartPos,wall2EndPos)
        self.wallGroup.add(wall2)
        exitStartPos=(550,300)
        exitEndPos=(600,300)
        exit=Exit(exitStartPos,exitEndPos)
        wall3StartPos=(600,300)
        wall3EndPos=(600,400)
        wall3=Wall(wall3StartPos,wall3EndPos)
        self.wallGroup.add(wall3)
        wall4StartPos=(500,400)
        wall4EndPos=(300,400)
        wall4=Wall(wall4StartPos,wall4EndPos)
        self.wallGroup.add(wall4)
        print(self.wallGroup)
        '''self.roomGroup=pygame.sprite.Group()
        xLeft=100
        yTop=20
        widthRoom1=200
        heightRoom1=300
        self.room1=Room(xLeft,yTop,widthRoom1,heightRoom1)
        self.roomGroup.add(self.room1)'''

    def timerFired(self,dt): 
        self.playerGroup.update(self.isKeyPressed)
        if pygame.sprite.groupcollide(self.playerGroup,self.wallGroup,False,\
        False,collided=None):
            self.playerGroup.speed=-60
        else:
            self.playerGroup.speed=5
        '''for room in self.roomGroup:#find a way to only check each active room
        #later
            for player in self.playerGroup:
                if player.x+player.r>=room.xLeft+room.width or player.x-\
                player.r<=room.xLeft or player.y-player.r<=room.yTop or \
                player.y+player.r>=room.yTop+room.height:
                    player.speed=-60
                else:
                    player.speed=5'''

    def redrawAll(self,screen):
        #self.roomGroup.draw(screen)
        self.wallGroup.draw(screen)
        print('draw wall group')
        self.playerGroup.draw(screen)

class Wall(pygame.sprite.Sprite):
    def __init__(self,startPos,endPos):
        super(Wall,self).__init__()
        self.startPos=startPos
        self.endPos=endPos
        self.width=2
        self.height=2
        xLeft=startPos[0]
        yTop=startPos[1]
        self.width=abs(endPos[0]-startPos[0])
        self.height=abs(endPos[1]-startPos[1])
        angle=-math.atan2(self.height,self.width)
        if self.width==0:
            self.width=1
        if self.height==0:
            self.height=1
        self.image=pygame.Surface((self.width,self.height),\
        pygame.SRCALPHA).convert_alpha()
        self.image=pygame.transform.rotate(self.image,angle)
        pygame.draw.line(self.image,(0,0,0),startPos,endPos,10)
        self.rect=self.image.get_rect()
        print(self.rect)
        print('wall __init__')

    def updateRect(self):
        #update the object's rect attribute with the new x,y coordinates
        pass
        self.rect=self.image.get_rect()
        print('wall updateRect')

    def update(self):
        self.updateRect()
        super(Wall,self).update()
        print('wall update')

class PygameGame(object):#taken from Lukas Peraza's framework, https://qwewy.gitbooks.io/pygame-module-manual/chapter1/framework.html

    def init(self):
        pass

    def mousePressed(self, x, y):
        pass

    def mouseReleased(self, x, y):
        pass

    def mouseMotion(self, x, y):
        pass

    def mouseDrag(self, x, y):
        pass

    def keyPressed(self, keyCode, modifier):
        pass

    def keyReleased(self, keyCode, modifier):
        pass

    def timerFired(self, dt):
        pass

    def redrawAll(self, screen):
        pass

    def isKeyPressed(self, key):
        ''' return whether a specific key is being held '''
        return self._keys.get(key, False)

    def __init__(self, width=3000, height=200, fps=50, title="112 Pygame Game"):
        self.width = width
        self.height = height
        self.fps = fps
        self.title = title
        self.bgColor = (255, 255, 255)
        pygame.init()

    def run(self):

        clock = pygame.time.Clock()
        screen = pygame.display.set_mode((self.width, self.height))
        # set the title of the window
        pygame.display.set_caption(self.title)

        # stores all the keys currently being held down
        self._keys = dict()

        # call game-specific initialization
        self.init()
        playing = True
        while playing:
            time = clock.tick(self.fps)
            self.timerFired(time)
            for event in pygame.event.get():
                if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                    self.mousePressed(*(event.pos))
                elif event.type == pygame.MOUSEBUTTONUP and event.button == 1:
                    self.mouseReleased(*(event.pos))
                elif (event.type == pygame.MOUSEMOTION and
                      event.buttons == (0, 0, 0)):
                    self.mouseMotion(*(event.pos))
                elif (event.type == pygame.MOUSEMOTION and
                      event.buttons[0] == 1):
                    self.mouseDrag(*(event.pos))
                elif event.type == pygame.KEYDOWN:
                    self._keys[event.key] = True
                    self.keyPressed(event.key, event.mod)
                elif event.type == pygame.KEYUP:
                    self._keys[event.key] = False
                    self.keyReleased(event.key, event.mod)
                elif event.type == pygame.QUIT:
                    playing = False
            screen.fill(self.bgColor)
            self.redrawAll(screen)
            pygame.display.flip()

        pygame.quit()


def main():
    game = PygameGame()
    pygame.init()
    game.run()

if __name__ == '__main__':
    main()

我希望看到画线的位置,我硬编码,但没有画线在所有。是什么导致了这个问题?你知道吗


Tags: selfeventinitdefpasswidthscreenpygame