2014-12-08 142 views
1

因此,我目前正在學習Python 2.7的pyglet,並且試圖製作一個簡單的具有關卡的遊戲。第一個'場景'是標題/介紹部分,第二個是某種教程,其餘部分是遊戲關卡。爲此,我創建了7個批次(1個簡介,1個教程,5個級別),即batch,batch1,... batch6。我還爲代表場景/層次的每個批次創建了7個類。這是我爲介紹批次和類所做的:如何使用pyglet批處理繪製場景或級別

batch = pyglet.graphics.Batch() 
batch1 = pyglet.graphics.Batch() 
class StartState: 
    def __init__(self): 
     self.welcome = pyglet.text.Label('WELCOME TO', font_name='Arial', font_size=32, color=(200,255,255,255), x=400, y=550, anchor_x='center', anchor_y='center', batch=batch) 
     self.title = pyglet.text.Label("MY GAME", font_name='Arial', font_size=32, color=(100,200,170,255), x=400, y=450, anchor_x='center', anchor_y='center', batch=batch) 
     self.press = pyglet.text.Label("press 'SPACE' to continue", font_name='Arial', font_size=32, color=(200,255,150,255), x=400, y=250, anchor_x='center', anchor_y='center', batch=batch) 
    def update(self, dt): 
     if keymap[pyglet.window.key.SPACE]: 
      self.welcome.delete() 
      self.title.delete() 
      self.press.delete() 
      states.pop() 
      batch1.draw() 

其他場景也會如此。州列表是我用來存儲我的課程/場景的列表。 states = [Level5(),Level4(),...,TutorialState(),StartState()]。所以每當前進的條件滿足時,在這個類中按下'SPACE',窗口將被'清除',即刪除精靈/標籤並通過使用states.pop()和batch1進入下一個場景。畫()。

我已經輸入這些類之後,我加入這個底:

@window.event 
def on_draw(): 
    window.clear() 
    batch.draw() 

def update(dt): 
    if len(states): 
     states[-1].update(dt) 
    else: 
     pyglet.app.exit() 

states.append(Level5()) 
states.append(Level4()) 
states.append(Level3()) 
states.append(Level2()) 
states.append(Level1()) 
states.append(TutorialState()) 
states.append(StartState()) 


pyglet.clock.schedule_interval(update, 1.0/60.0) 
window.clear() 
window.flip() 
window.set_visible(True) 
pyglet.app.run() 

這裏的問題是,它只會加載起始配料/場景。每當按'空格'進入教程場景時,起始批次的標籤/精靈都會消失,但它不會繪製批次1或加載教程課程/場景。有什麼建議麼?

+0

在處理'on_draw'事件時,您只能繪製批次''批次''。不應該用每次按空間時輸入的「狀態」對應的批量覆蓋它?那麼每個具有字段'batch'並在'update()'方法中返回它的狀態類,那麼您可以將它分配給您在'on_draw()'中繪製的全局批處理? – 2014-12-09 12:43:48

回答

0

每個場景類創建一個批處理之後:

import pyglet 
    from pyglet.window import key 

    class SceneTemplate(object): 
     """a template with common things used by every scene""" 
     def __init__(self, text): 
      self.batch = pyglet.graphics.Batch() 
      self.label = pyglet.text.Label(
       text, 
       font_name='Arial', font_size=32, 
       color=(200, 255, 255, 255), x=32, y=704, 
       batch=self.batch) 
      # (...) 

    class MainMenuScene(SceneTemplate): 
     def __init__(self): 
      super(MainMenuScene, self).__init__(text='MainMenuScene') 
      # (...) 

    class IntroScene(SceneTemplate): 
     def __init__(self): 
      super(IntroScene, self).__init__(text='Introduction') 
      # (...) 

    class Level1(SceneTemplate): 
     def __init__(self): 
      super(Level1, self).__init__(text='Level 1') 
      # (...) 

您可以控制在另一個類,狀態/場景,如窗口類(我個人喜歡subclass the pyglet window, to keep things organized and some other reasons):

class Window(pyglet.window.Window): 
     def __init__(self): 
      super(Window, self).__init__(width=1024, height=768) 
      self.states = [MainMenuScene(), IntroScene(), Level1()] # and so on... 
      self.current_state = 0 # later you change it to get the scene you want 
      self.set_visible() 

     def on_draw(self): 
      self.clear() 
      self.states[self.current_state].batch.draw() 

     def on_key_press(self, symbol, modifiers): 
      if symbol == key.SPACE: 
       new_state = self.current_state + 1 
       new_state = new_state % len(self.states) 
       self.current_state = new_state 
      # if you want each scene to handle input, you could use pyglet's push_handlers(), or even something like: 
      #  self.states[self.current_state].on_key_press(symbol, modifiers) 
      # giving them access to the window instance might be needed. 

    if __name__ == '__main__': 
     window = Window() 
     pyglet.app.run()