In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article focuses on "how to use Python to create video games", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to create video games with Python.
Use Python in one minute.
Python is a versatile programming language, which means that it (like most languages) provides "simple tricks" for functions to deal with numbers and characters. Users of the Linux operating system have installed Python. Mac operating system users use an older version of Python, but you can install the latest version from the Python.org website. Windows operating system users can learn how to install Python from this article on installing Python on Windows.
After the installation is complete, you can start the interactive Python Shell to perform arithmetic operations:
$python3 > 5000611 > 11,25.5 > 11,25 > 11% 21
As you can see from this example, some special symbols are needed, but people who have studied mathematics are most familiar with them. Maybe you don't like numbers, but you prefer letters:
$python3 > > string = "hello world" > print (string) hello world > print (string.upper () HELLO WORLD > print (string [0]) h > print (string [1]) e > print (string [2]) l > > print (string [3]) l > > print (string [4]) o
Similarly, the basic task has a relatively special symbolic representation, but even if it is not specified, you may have found that the [0] and [1] symbolic representations "slice" the data and use the print function to display the data on the screen.
Use Pygame in five minutes.
If you just want to use Python to create a video game or any project that goes beyond basic computing, it may take a lot of study, effort, and time. Fortunately, it has been 20 years since Python was born, and developers have developed a code base to help you accomplish typical program feats with relative ease. Pygame is a set of code modules for creating video games. It is not the only such class library, but it is the oldest (for better or worse), so there are many documents and examples online.
First learn the recommended Python virtual environment workflow:
$python3-m venv mycode/venv$ cd mycode$ source. / venv/bin/activate (venv) $
After entering the virtual environment, you can safely install Pygame into the project folder:
(venv) $echo "pygame" > > requirements.txt (venv) $python-m pip install-r requirements.txt [...] Installing collected packages: pygameSuccessfully installed pygame-x.y.z
Now that you have Pygame installed, you can create a simple demo application. It's easier than you think. Python can do so-called object-oriented programming (OOP), a beautiful computer science term used to describe when code is structured, just as you are using code to create physical objects. However, programmers are not confused. They know that they are not really creating physical objects when writing code, but it helps to imagine, because then you can understand the limitations of the programming world.
For example, if you are trapped on a desert island and want a cup of coffee, you must collect some clay, make a cup, and then bake it. If you are smart enough, create a mold first so that whenever you need another cup, you can quickly create a new cup from the template. Even if each cup comes from the same template, they are physically independent: if one cup breaks, you will have another cup. You can make each coffee cup look unique by adding color or etching.
You use similar logic in Pygame and many programming tasks. It will not appear in your programming project until it is defined. Here's how to make coffee cups appear in Python and Pygame programs.
Using Pygame for object-oriented programming
Create a file called main.py, enter the following code to start the Pygame module, and create a window using the Pygame template:
Import pygame pygame.init () screen = pygame.display.set_mode (960720)
Just as you might use templates to create objects in real life, you can also use templates provided by Pygame to create a leprechaun sprite (a visual game object term for Pygame). In object-oriented programming, class represents a template for an object. Enter the following code in your document:
Class Cup (pygame.sprite.Sprite): def _ init__ (self): pygame.sprite.Sprite.__init__ (self) # image img = pygame.image.load ('coffeecup.png'). Convert () self.image = img # volume self.rect = self.image.get_rect () self.rect.x = 10 self.rect.y = 10
This code block uses Pygame's sprite template to design a coffee cup leprechaun. Because of self.image, your coffee cup leprechaun has an image, while self.rect gives it volume (width and height). These are the attributes that Pygame expects goblins to have, but if you want to create a playable video game, you can specify any other required attributes, such as health points and scores.
So far, all you have to do is create a window and a template for coffee cups. You don't actually have a cup in your game.
The last part of your code must use a template to generate the cup and add it to the game world. As you know, the computer runs very fast, so technically, the code you have created so far will only run for about a millisecond. When writing a graphics computer application, it must be forced to keep it open, regardless of whether the computer thinks it has completed the required task or not. Programmers use an infinite loop to do this, which is represented by a while True statement in Python (True is always true, so the loop never ends).
An infinite loop ensures that your application stays open long enough so that computer users can view and use the application:
Cup = Cup () while True: pygame.display.update () screen.blit (cup.image, cup.rect)
This code example creates a cup from the template Cup, and then uses the Pygame function to update the display. Finally, use the blit function of Pygame to draw the image of the cup within its border.
Get Drawin
Before you can run the code successfully, you need to prepare a graph for the coffee cup. You can find a lot of public creative coffee cup graphics on FreeSVG.org. I used this. Save the drawing in the project directory and name it coffeecup.png.
Run the game
Start the application:
(venv) $python. / main.py
At this point, I believe you have a deeper understanding of "how to create video games with Python". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.