简体   繁体   English

Pygame:如何连续移动角色和视野?

[英]Pygame : How to continously move character and view?

I'm not sure how to explain, but I'm trying to make game where player can walk on window and meet enemies etc. 我不确定该如何解释,但我正在尝试制作一款游戏,使玩家可以在窗户上行走并遇见敌人等。

I have code with one window. 我有一个窗口的代码。 Like this: 像这样:

window = pygame.display.set_mode((500, 480))

And my character can move left, right and jump on this window, but when he gets to the corner he stops. 我的角色可以在该窗口中左右移动,但当他到达角落时,他停了下来。 So my question is how to make him move further and, for example, change background, meet another enemies etc. when he walks? 因此,我的问题是如何使他走得更远,例如,在他走路时改变背景,结识其他敌人等? Do I have to make a class and call her when character touch corner maybe? 当角色接触角落时,我是否必须上课并打电话给她? I hope you understand. 我希望你明白。 Thanks. 谢谢。

You need to understand when your character touch / is close enough to the border of the screen and then take appropriate action. 您需要了解何时字符触摸/足够靠近屏幕边界,然后采取适当的措施。
In a game I'm writing for fun, the class representing the main character has a method to check when the character is inside a surface sface . 在我正在玩的游戏中,代表主要角色的类具有一种检查角色何时在表面sface内的sface I copy-paste its skeleton here to show an example: 我在此处复制粘贴其骨架以显示示例:

def insidesurf(self, sface):
    if sface.get_rect().contains(self.rect):
        return None
    else:
        if self.rect.top < sface.get_rect().top:
            #the character is going out from the screen from the top-side
        elif self.rect.left < sface.get_rect().left:
            #the character is going out from the screen from the left-side
        elif self.rect.bottom > sface.get_rect().bottom:
            #the character is going out from the screen from the bottom-side
        elif self.rect.right > sface.get_rect().right:
            #the character is going out from the screen from the right-side

This method is called at each iteration of the main loop after the character is moved (after a key press). 字符移动后(按键后)在主循环的每次迭代中调用此方法。 The argument sface is your window . 争论的sface是你的window
What to do exactly in the various if statements is up to you. 在各种if语句中究竟要做什么完全取决于您。 In my case, it redraws the screen replacing the character at the opposite side of the screen. 在我的情况下,它将重画屏幕,替换屏幕另一侧的字符。 If the character is exiting from left side, it's redrawn at right side, to give the impression that the camera is showing the next room and the character is at the same position. 如果角色从左侧退出,则会在右侧重绘,以给人一种印象,即相机正在显示下一个房间,并且角色处于同一位置。
You may also wish to not pass your window but a smaller surface in order to scroll the scenario instead of redrawing it all. 您可能还希望不要通过window而是通过较小的表面来滚动场景,而不是全部重绘。

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

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