Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the PyGame Zero of python

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how to use python's PyGame Zero". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "how to use python's PyGame Zero" can help you solve your doubts.

With PyGame Zero in your game development process, say goodbye to boring templates.

Python is a good entry-level programming language. Also, games are a good starter: they are visual, self-driven, and can be happily shared with friends and family. Although, most Python-written libraries, such as PyGame, can confuse beginners because forgetting small details can easily lead to nothing being rendered.

Before they understand what all the parts do, they think of many of them as "unconscious template files"-magical paragraphs that need to be copied and pasted into the program to make them work.

PyGame Zero tries to bridge this gap by placing an abstraction layer on top of PyGame, so it literally does not need a template.

When we say "literally", we mean literally.

This is a qualified PyGame Zero file:

# This comment is here for clarity reasons

We can 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 interrupted by closing the window or pressing CTRL-C.

Unfortunately, it will be a boring game. Nothing happened.

To make it more interesting, we can draw a different background:

Def draw (): screen.fill ((255,0,0))

This will change the background color from black to red. But it was still a boring game and nothing happened. We can make it a little more interesting:

Colors = [0,0,0] def draw (): screen.fill (tuple (colors)) def update (): colors [0] = (colors [0] + 1)% 256

This will make the window start in black, brighten gradually until it turns bright red, return to black, and cycle over and over again.

The update function updates the values of the parameters, and draw renders the game based on those parameters.

Even so, there is no way to give players a way 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. This article "how to use PyGame Zero in python" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report