2

簡単な質問: 最初のコードは機能するのに、同じように見える 2 番目のコードが pygame ウィンドウが表示されるとフリーズするのはなぜですか?

# Moving Pan
# Demonstrates mouse input

from livewires import games

games.init(screen_width = 640, screen_height = 480, fps = 50)

class Pan(games.Sprite):
    """ A pan controlled by the mouse. """
    def update(self):
        """ Move to mouse coordinates. """
        self.x = games.mouse.x
        self.y = games.mouse.y

def main():
    wall_image = games.load_image("wall.jpg", transparent = False)
    games.screen.background = wall_image

    pan_image = games.load_image("pan.bmp")
    the_pan = Pan(image = pan_image,
                  x = games.mouse.x,
                  y = games.mouse.y)
    games.screen.add(the_pan)

    games.mouse.is_visible = False

    games.screen.event_grab = True

    games.screen.mainloop()

# kick it off!
main()

誤動作秒:

from livewires import games,color
games.init (screen_width = 640, screen_height = 480, fps = 50)

#Creating a moving object tied to the cursor. This includes one method with two
#lines of code.
class Pan (games.Sprite):
    def moved (self):
    #Receives mouse position
        self.x = games.mouse.x
    #Changes mouse position to new x,y values.
        self.y = games.mouse.y

#The Main
myscr = games.screen
myscr.set_background (games.load_image ("wall.jpg", transparent = False))
pan_image = games.load_image ("pan.bmp")
le_pan = Pan (image = pan_image,
              x = games.mouse.x,
              y = games.mouse.y)

games.mouse.is_visible = False
myscr.add (le_pan)
myscr.event_grab = True
myscr.mainloop()
4

1 に答える 1

2

私はライブワイヤーを使ったことはありませんが、ゲームでは通常、多かれ少なかれ無限のゲームループが必要です。

ゲームループの背後にある意味は、一度だけではなく、マウスがどこにあるのか、どのキーが押されたのかを常に知りたいということです! ですから、何度も尋ねなければなりませんWhere is the mouse?。これを実現するには、実行されるたびに必要なものをすべてチェックするループを使用します。

最初の例では、ゲームループは functionmainです。申請の流れはこんな感じです。

  1. 必要なライブラリをインポートする

    from livewires import games
    
  2. ゲーム画面の初期化

    games.init(screen_width = 640, screen_height = 480, fps = 50)
    
  3. 画面に表示できるスプライトを宣言する

    class Pan(games.Sprite):
        """ A pan controlled by the mouse. """
        def update(self):
            """ Move to mouse coordinates. """
            self.x = games.mouse.x
            self.y = games.mouse.y
    
  4. main メソッドを宣言し、ゲーム画面の背景を設定する

    def main():
        wall_image = games.load_image("wall.jpg", transparent = False)
        games.screen.background = wall_image
    
  5. 上記で定義したスプライトを画面に追加し、マウスカーソルの位置に移動します

        pan_image = games.load_image("pan.bmp")
        the_pan = Pan(image = pan_image,
                      x = games.mouse.x,
                      y = games.mouse.y)
        games.screen.add(the_pan)
    
  6. マウス カーソルを非表示にし、イベントを有効にする

        games.mouse.is_visible = False
        games.screen.event_grab = True
    
  7. mainloopを実行します。このメソッドの呼び出しは次のように述べています。Run me( functionmain)over and over!

        games.screen.mainloop()
    
  8. main を初めて呼び出す

    main()
    

2 番目の例では、ゲームループはありません。アプリケーションの流れ (密集) は次のようになります。

  1. ライブラリのインポート、ゲーム画面の初期化、スプライトの宣言

    from livewires import games,color
    games.init (screen_width = 640, screen_height = 480, fps = 50)
    
    class Pan (games.Sprite):
        def moved (self):
            self.x = games.mouse.x
            self.y = games.mouse.y
    
  2. ゲーム画面の背景設定とスプライトの追加

    myscr = games.screen
    myscr.set_background (games.load_image ("wall.jpg", transparent = False))
    pan_image = games.load_image ("pan.bmp")
    le_pan = Pan (image = pan_image,
                  x = games.mouse.x,
                  y = games.mouse.y)
    myscr.add(le_pan)
    
  3. マウス カーソルを無効にし、イベントを有効にする

    games.mouse.is_visible = False
    myscr.event_grab = True
    
  4. mainloopを実行します。このメソッドの呼び出しは次のように述べています。Run me( functionundefined)over and over!

    myscr.mainloop()
    

そしてここがこだわりポイント!Python ファイルのルートにあるコードを呼び出すことはできません! 関数は、mainloopどこから戻るか、どこから開始するかを知りません。呼び出しが失われ、プログラムがフリーズします。更新方法が何も指示されていないため、ゲーム画面を更新できません。

結論:ゲームループ用の関数が必要です!

于 2011-06-24T14:38:57.467 に答える