2016-06-16 30 views
0

合計のpython初心者がここにあります。PythonとPygletの入力

私はPythonライブラリPygletでゲームを作ろうとしています。私は入力が必要な部分に到達するまで、それは素晴らしい仕事をしてきました。

私は例とドキュメントに従っており、私のコードはこのように見えます。私は、キーボード入力が同じように動作するように見えるので、今はマウス入力にのみ焦点を合わせます。

import pyglet 
    import button 

from pyglet.window import key 

class GameWindow(pyglet.window.Window): 
    def __init__(self): 
     super(GameWindow, self).__init__() 
     self.button = button.Button() 

    def on_draw(self): 
     self.clear() 

    def on_mouse_press(self, x, y, button, modifiers): 
     self.button.on_mouse_press(x, y, button, modifiers) 
     print("mouse pressed") 

    def on_key_press(self, symbol, modifiers): 
     print("key pressed") 

    def on_mouse_motion(self, x, y, dx, dy): 
     print("mouse moved") 

    def update(dt): 
     pass 



#initializes window and keyboard 
window = GameWindow() 
keys = key.KeyStateHandler() 
window.push_handlers(keys) 

pyglet.clock.schedule_interval(GameWindow.update, 1/60.0) 

if __name__ == '__main__': 

    pyglet.app.run() 

コードはうまく動作しますが、これは私の質問です。入力を処理する正しい方法ですか? ボタンクラスに手動でパラメータを手動で送信する必要がありますか? ボタンクラスが入力用の関数をオーバーライドすることはできないので、ボタンは入力自体を収集しますか?

私のようなウィンドウを作成して、たとえばButtonクラスのような別の無関係なクラスで入力を集めることは可能ですか?

現在のところ、私はボタン、チャットウィンドウ、物理学、その他の場所への入力を送らなければなりません。私は後ろにそれをやっていることを恐れて、それは後でお尻に私をかむでしょう。

答えて

0

これは良いPygletクラスの基本的なスケルトンです。
好きなように変更してください。ここで重要なことは、マウスとキーボードのI/Oを別々に処理することです。

import pyglet 
from pyglet.gl import * 

# Optional audio outputs (Linux examples): 
# pyglet.options['audio'] = ('alsa', 'openal', 'silent') 
key = pyglet.window.key 

class Spr(pyglet.sprite.Sprite): 
    def __init__(self, texture, x=None, y=None, moveable=True): 
     self.texture = pyglet.image.load(texture) 
     super(Spr, self).__init__(self.texture) 
     self.moveable = moveable 

    def move(self, x, y): 
     if self.moveable: 
      self.x += x 
      self.y += y 

class main(pyglet.window.Window): 
    def __init__ (self): 
     super(main, self).__init__(800, 800, fullscreen = False) 
     self.x, self.y = 0, 0 

     self.bg = pyglet.sprite.Sprite(pyglet.image.load('background.jpg')) 
     self.sprites = {'SuperMario' : Spr('mario.jpg')} 
     self.player = pyglet.media.Player() 
     self.alive = 1 

    def on_draw(self): 
     self.render() 

    def on_close(self): 
     self.alive = 0 

    def on_key_press(self, symbol, modifiers): 
     # Do something when a key is pressed? 
     # Pause the audio for instance? 
     # use `if symbol == key.SPACE: ...` 

     # This is just an example of how you could load the audio. 
     # You could also do a standard input() call and enter a string 
     # on the command line. 
     if symbol == key.ENTER: 
      self.player.queue(media.load('beep.mp3', streaming = False)) 
      if nog self.player.playing: 
       self.player.play() 
     if symbol == key.ESC: # [ESC] 
      self.alive = 0 
     elif symbol == key.LEFT: 
      self.sprites['SuperMario'].move(-1, 0) 
     #elif symbol == key.RIGHT: ... 

    def render(self): 
     self.clear() 
     self.bg.draw() 

     # self.sprites is a dictionary where you store sprites 
     # to be rendered, if you have any. 
     for sprite_name, sprite in self.sprites.items(): 
      sprite.draw() 

     self.flip() 

    def run(self): 
     while self.alive == 1: 
      self.render() 

      # -----------> This is key <---------- 
      # This is what replaces pyglet.app.run() 
      # but is required for the GUI to not freeze 
      # 
      event = self.dispatch_events() 
     self.player.delete() # Free resources. (Not really needed but as an example) 

x = main() 
x.run() 

これはを入力して、左にあなたの好みの文字を移動当たったときに音beep.mp3を再生することができる方法の一例です。

私はあなたが疑問に思っていることは確かではありませんが、これは私が理解してくれたクラスであり、あなたにとって最高のパフォーマンスを提供します。あなたがそれをさらに改善できるなら、私に知らせてください。

+0

あなたの答えをありがとうが、私はマリオブラザーズを作成していると言うことができ、私はマリオに関連するすべてを処理するクラスがあります。私はどのように行って、マリオのクラスにその入力を得るでしょうか?あなたのバージョンは、私が基本的にメインからMario.Input(ボタン)を実行することを示唆しています。それはそれを行う最善の方法ですか? – Mattias

+0

@Mattiasこれはすべてあなたがメインクラスから制御したい**ものになります。例えば、ユーザー入力の場合、私はメインクラスにメインキャラクターの移動をさせながら、通常はスプライトに渡します。私は例を挙げて答えを更新します。メインクラスから制御される最小限の機能を備えた**スプライト**クラスを追加する必要はありませんでした。そして、はい、これは基本的に物事を設定する方法です。 – Torxed

+0

ありがとうございます。それが私が知りたかったものです。 – Mattias

関連する問題