学习 Pyxel:使用鼠标移动精灵

  • Pyxel Overview: Pyxel is a retrogaming framework built with Rust, exposing a Python API. It's used in this book chapter to create games.
  • Books for Learning Python: Mentioned are No Starch Press' "Python for Kids" (second edition) and O'Reilly's "Learning Python" (sixth edition). It's emphasized that learning development requires reading proper books rather than relying on Youtube videos or copy-pasting tutorials.
  • Demo: The demo involves a bat in a dark cave. With the mouse, the player can control the bat's flight.

    • Assets: There are two 16x16px sprite images of the bat - one with wings up and one with wings down.
    • Code:

      • The Bat class wraps the bat's functionality. It has attributes like x, y, frame, and methods like swap_wings and draw.
      • The App class initializes the game, loads assets, and contains the main loop with update and draw methods. In different versions of the code:

        • In the first version, the bat flaps its wings every 10 frames and can be quit by hitting the Q key.
        • In the second version, clicking the left mouse button moves the bat to the cursor position, and if the button is held, the bat follows the mouse.
        • In the third version, the bat flies towards the click position using math (Euclidean distance and trigonometry) to compute the path. Vectors and steps use real numbers, and screen positions use natural numbers, causing rounding issues. The vector is adjusted to use the center of the sprite.
  • Beyond: Encouraged to own the code, make changes, add behaviors like randomness in flight or an additional standby sprite, and add sounds. The source code of the bat is hosted on Github.
阅读 9
0 条评论