Pygame 中的多个显示器 [英] Multiple Displays in Pygame

查看:73
本文介绍了Pygame 中的多个显示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个小游戏,我想制作另一个与主窗口分开的窗口.

我在主窗口中有主游戏,我想在用户做某事时打开一个新窗口并做一些动画.

在我下面的示例代码中,当用户按下a"时,我希望它打开一个新窗口并转到那里.

我在这里设置了两个窗口:(我知道这行不通,这是我要问的)

SCREEN_X = 400屏幕_Y = 400BSCREEN_X = 240BSCREEN_Y = 160BATTLE_SCENE = pygame.display.set_mode((BSCREEN_X, BSCREEN_Y))SCREEN = pygame.display.set_mode((SCREEN_X, SCREEN_Y))

然后是程序:

def run_ani():#做动画,bitting到BATTLE_SCENE返回def main_game():结尾=假而结束==假:时钟滴答(30)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:ending=Trueif event.type == KEYDOWN: # key down 还是 up?如果 event.key == K_ESCAPE:Ending=True # 离开的时间打印(用户提前停止")elif event.key == K_a:运行_ani()#正常屏幕运动,闪烁到屏幕如果结束:pygame.quit()返回

目前做的是绘制主屏幕,然后当按下A时,停止绘制主屏幕动画,但仍然在主屏幕上绘制其他动画并在左上角绘制.

我很确定它会这样做,因为我将 BATTLE_SCENE 设置为小于主屏幕,因此当 blitting 到 BATTLE_SCENE 时,它会转到我创建的区域(240x160) 在主屏幕的顶角.

但是我希望 BATTLE_SCENE 成为一个单独的窗口,这样当我按a"时它会弹出,做它的事情,然后关闭或至少在主屏幕后面.

如何做到这一点?甚至有可能吗?

解决方案

您真的需要多个窗口吗?我的意思是,你真的需要它们吗?

如果是,那么您可能应该使用 pyglet/cocos2d 代替.

要在 pygame 中拥有多个窗口,您需要多个进程(每个窗口一个).虽然这是可行的,但不值得付出代价.您需要 IPC 在窗口之间交换数据,我想您的代码将变成容易出错且丑陋.

当您需要多个窗口时,请使用 pyglet.

更好的解决方案可能是将您的游戏分成多个场景.创建多个场景,让每个场景代表游戏的一个阶段,例如 MenuScene、MainScene、BattleScene、GameOverScene、OptionScene 等.

然后让每个场景处理游戏的那个部分的输入/绘制.

  • MenuScene 处理游戏菜单的绘制和输入等
  • MainScene 处理正在运行的游戏的绘制和输入等
  • BattleScene 处理您在 run_ani
  • 中所做的任何事情的绘图和输入等

在您的主循环中,只需通过实现方法 draw()handle_event()update() 将控制权交给当前场景代码>.

一些示例代码来获得这个想法:

scenes = {'Main': MainScene(),'战斗':BattleScene()} #etc场景=场景['主要']类 MainScene():...def handle_event(self, event):如果 event.type == KEYUP:如果 event.key == K_a:场景=场景['战斗']...类 BattleScene():...定义绘制(自我):# 绘制动画定义更新(自我):# 如果动画结束:场景=场景['主要']...def main_game():结尾=假虽然没有结束:时钟滴答(30)对于 pygame.event.get() 中的事件:scene.handle_event(事件)场景.更新()场景绘制()

这是一种干净分离游戏逻辑并允许上下文切换的简单方法.

I'm making a little game and I want to make another window separately from my main one.

I have the the main game in a main window, and I want to open a new window and do a little animation when the user does something.

In my example code below, when the user presses "a" I want it to open a new window and blit to there.

Here I set up the two windows: (I know this doesnt work, its what I'm asking how to do)

SCREEN_X = 400
SCREEN_Y = 400
BSCREEN_X = 240
BSCREEN_Y = 160

BATTLE_SCENE = pygame.display.set_mode((BSCREEN_X, BSCREEN_Y))
SCREEN = pygame.display.set_mode((SCREEN_X, SCREEN_Y))

and then the program:

def run_ani ():
    #Do animation, blitting to BATTLE_SCENE
    return

def main_game():
    ending=False
    while ending==False:
        clock.tick(30)
        for event in pygame.event.get():
            if event.type == pygame.QUIT: ending=True
            if event.type == KEYDOWN: # key down or up?
                if event.key == K_ESCAPE:
                    ending=True # Time to leave
                    print("Stopped Early by user")
                elif event.key == K_a:
                    run_ani()
        #Normal screen motion, blitting to SCREEN
        if ending: pygame.quit()
    return

So far what this does is draws the main screen, then when A is pressed, it stops drawing the main screen animations, but still draws the other animations on the main screen and draws in the top left corner.

I'm pretty sure it does this because I am setting BATTLE_SCENE to be smaller than the main screen, thus when blitting to BATTLE_SCENE it blits to the area I created (240x160) in the top corner of the main screen.

However I want BATTLE_SCENE to be a seperate window, so that when I press 'a' it will pop up, do its thing, then close or at least go behind the main screen.

How to do this? Is it even possible?

解决方案

Do you really need multiple windows? I mean, do you really need them?

If yes, then you should probably use pyglet/cocos2d instead.

To have multiple windows in pygame, you need multiple processes (one for each window). While this is doable, it's not worth the efford. You'll need IPC to exchange data between the windows, and I guess your code will become error-prone and ugly.

Go with pyglet when you need more than one window.

The better solution is probably to divide your game into scenes. Create multiple scenes so that each one represent one stage of the game, something like MenuScene, MainScene, BattleScene, GameOverScene, OptionScene etc.

Then let each of those scenes handle input/drawing of that very part of the game.

  • MenuScene handles drawing and input etc. of the game's menu
  • MainScene handles drawing and input etc. of the running game
  • BattleScene handles drawing and input etc. of whatever you do in run_ani

In your mainloop, just pass control over to the current scene by implementing the methods draw(), handle_event(), and update().

Some example code to get the idea:

scenes = {'Main': MainScene(),
          'Battle': BattleScene()} #etc

scene = scenes['Main']

class MainScene():
  ...
  def handle_event(self, event):
    if event.type == KEYUP:
      if event.key == K_a:
        scene = scenes['Battle']
  ...

class BattleScene():
  ...
  def draw(self):
    # draw your animation

  def update(self):
    # if animation is over:
    scene = scenes['Main']

...

def main_game():
  ending=False
  While Not ending:
      clock.tick(30)
      for event in pygame.event.get():
        scene.handle_event(event)
        scene.update()
        scene.draw()

This is an easy way to cleanly seperate the game logic and allow context switching.

这篇关于Pygame 中的多个显示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆