mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
commit
4e6b013bbf
@ -1,99 +0,0 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (xiqingongzi)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (PyGame Zero: Games without boilerplate)
|
||||
[#]: via: (https://opensource.com/article/19/1/pygame-zero)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
PyGame Zero: Games without boilerplate
|
||||
======
|
||||
Say goodbye to boring boilerplate in your game development with PyGame Zero.
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python3-game.png?itok=jG9UdwC3)
|
||||
|
||||
Python is a good beginner programming language. And games are a good beginner project: they are visual, self-motivating, and fun to show off to friends and family. However, the most common library to write games in Python, [PyGame][1], can be frustrating for beginners because forgetting seemingly small details can easily lead to nothing rendering.
|
||||
|
||||
Until people understand why all the parts are there, they treat many of them as "mindless boilerplate"—magic paragraphs that need to be copied and pasted into their program to make it work.
|
||||
|
||||
[PyGame Zero][2] is intended to bridge that gap by putting a layer of abstraction over PyGame so it requires literally no boilerplate.
|
||||
|
||||
When we say literally, we mean it.
|
||||
|
||||
This is a valid PyGame Zero file:
|
||||
|
||||
```
|
||||
# This comment is here for clarity reasons
|
||||
```
|
||||
|
||||
We can run put it in a **game.py** file and run:
|
||||
|
||||
```
|
||||
$ pgzrun game.py
|
||||
```
|
||||
|
||||
This will show a window and run a game loop that can be shut down by closing the window or interrupting the program with **CTRL-C**.
|
||||
|
||||
This will, sadly, be a boring game. Nothing happens.
|
||||
|
||||
To make it slightly more interesting, we can draw a different background:
|
||||
|
||||
```
|
||||
def draw():
|
||||
screen.fill((255, 0, 0))
|
||||
```
|
||||
|
||||
This will make the background red instead of black. But it is still a boring game. Nothing is happening. We can make it slightly more interesting:
|
||||
|
||||
```
|
||||
colors = [0, 0, 0]
|
||||
|
||||
def draw():
|
||||
screen.fill(tuple(colors))
|
||||
|
||||
def update():
|
||||
colors[0] = (colors[0] + 1) % 256
|
||||
```
|
||||
|
||||
This will make a window that starts black, becomes brighter and brighter red, then goes back to black, over and over again.
|
||||
|
||||
The **update** function updates parameters, while the **draw** function renders the game based on these parameters.
|
||||
|
||||
However, there is no way for the player to interact with the game! Let's try something else:
|
||||
|
||||
```
|
||||
colors = [0, 0, 0]
|
||||
|
||||
def draw():
|
||||
screen.fill(tuple(colors))
|
||||
|
||||
def update():
|
||||
colors[0] = (colors[0] + 1) % 256
|
||||
|
||||
def on_key_down(key, mod, unicode):
|
||||
colors[1] = (colors[1] + 1) % 256
|
||||
```
|
||||
|
||||
Now pressing keys on the keyboard will increase the "greenness."
|
||||
|
||||
These comprise the three important parts of a game loop: respond to user input, update parameters, and re-render the screen.
|
||||
|
||||
PyGame Zero offers much more, including functions for drawing sprites and playing sound clips.
|
||||
|
||||
Try it out and see what type of game you can come up with!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/1/pygame-zero
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[xiqingongzi](https://github.com/xiqingongzi)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.pygame.org/news
|
||||
[2]: https://pygame-zero.readthedocs.io/en/stable/
|
@ -0,0 +1,102 @@
|
||||
[#]: collector: (lujun9972)
|
||||
[#]: translator: (bestony)
|
||||
[#]: reviewer: ( )
|
||||
[#]: publisher: ( )
|
||||
[#]: url: ( )
|
||||
[#]: subject: (PyGame Zero: Games without boilerplate)
|
||||
[#]: via: (https://opensource.com/article/19/1/pygame-zero)
|
||||
[#]: author: (Moshe Zadka https://opensource.com/users/moshez)
|
||||
|
||||
PyGame Zero: 从 0 开始的游戏
|
||||
======
|
||||
|
||||
在你的游戏开发过程中有了 PyGame Zero,和枯燥的模板说再见吧。
|
||||
|
||||
![](https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/python3-game.png?itok=jG9UdwC3)
|
||||
|
||||
Python 说一个很好的入门编程语言。并且,游戏是一个很好的入门项目:它们是可视化的,自驱动的,并且可以很愉快的与朋友和家人分享。虽然,绝大多数的 Python 写就的库,比如 [PyGame][1] ,会让初学者因为忘记微小的细节很容易导致什么都没渲染而感到困扰
|
||||
|
||||
在人们理解所有部分的原因前,他们将其中的许多部分都视为“无意识的样板文件”——需要复制和粘贴到程序中才能使其工作的神奇段落。
|
||||
|
||||
[PyGame Zero][2] 试图通过在 PyGame 上放置一个抽象的层来弥合这一差距,因此它实际上并不需要模板。
|
||||
|
||||
我们在字面上说的,就是我们的意思
|
||||
|
||||
这是一个合格的 PyGame Zero 文件:
|
||||
|
||||
```
|
||||
# This comment is here for clarity reasons
|
||||
```
|
||||
|
||||
我们可以将它放在一个 **game.py** 文件里,并运行:
|
||||
|
||||
```
|
||||
$ pgzrun game.py
|
||||
```
|
||||
|
||||
这将会展示一个可以通过关闭窗口或按下**CTRL-C**中断的窗口,并在后台运行一个游戏循环 ,
|
||||
|
||||
遗憾的是,这将是一场无聊的游戏。什么都没发生。
|
||||
|
||||
为了让他更有趣一点,我们可以画一个不同的背景:
|
||||
|
||||
```
|
||||
def draw():
|
||||
screen.fill((255, 0, 0))
|
||||
```
|
||||
|
||||
这将会把背景色从黑色换为红色。但是这仍是一个很无聊的游戏,什么都没发生。我们可以让它变的更有意思一点:
|
||||
|
||||
```
|
||||
colors = [0, 0, 0]
|
||||
|
||||
def draw():
|
||||
screen.fill(tuple(colors))
|
||||
|
||||
def update():
|
||||
colors[0] = (colors[0] + 1) % 256
|
||||
```
|
||||
|
||||
这将会让窗口从黑色开始,逐渐变亮,直到变为亮红色,再返回黑色,一遍一遍循环。
|
||||
|
||||
**update** 函数更新了 **draw** 渲染这个游戏所需的这些参数的值。
|
||||
|
||||
即使是这样,这里也没有任何方式给玩家与这个游戏的交互的方式。
|
||||
让我们试试其他一些事情:
|
||||
|
||||
```
|
||||
colors = [0, 0, 0]
|
||||
|
||||
def draw():
|
||||
screen.fill(tuple(colors))
|
||||
|
||||
def update():
|
||||
colors[0] = (colors[0] + 1) % 256
|
||||
|
||||
def on_key_down(key, mod, unicode):
|
||||
colors[1] = (colors[1] + 1) % 256
|
||||
```
|
||||
|
||||
现在,按下按钮来提升亮度。
|
||||
|
||||
这些包括游戏循环的三个重要部分:响应用户输入,更新参数和重新渲染屏幕。
|
||||
|
||||
PyGame Zero 提供了更多功能,包括绘制精灵图和播放声音片段的功能。
|
||||
|
||||
试一试,看看你能想出什么类型的游戏!
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: https://opensource.com/article/19/1/pygame-zero
|
||||
|
||||
作者:[Moshe Zadka][a]
|
||||
选题:[lujun9972][b]
|
||||
译者:[bestony](https://github.com/bestony)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]: https://opensource.com/users/moshez
|
||||
[b]: https://github.com/lujun9972
|
||||
[1]: https://www.pygame.org/news
|
||||
[2]: https://pygame-zero.readthedocs.io/en/stable/
|
Loading…
Reference in New Issue
Block a user