In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to use Python and Pygame module to build a game framework, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Explore Python by creating a simple dice game. Now is the time to make your own game from scratch.
This time, I'll show you how to use the Python module Pygame to create a graphical game. It will take several articles to get a game that does make something, but by the end of this series, you will have a better understanding of how to find and learn new Python modules and how to build an application from them.
Before you begin, you must install Pygame.
Install a new Python module
There are several ways to install the Python module, but the two most common are:
From your distribution's software repository
Software package manager pip using Python
Both methods work well, and each has its own set of advantages. If you are developing on Linux or BSD, you can use your distribution's software repository to update automatically and in a timely manner.
However, using Python's built-in package manager gives you the ability to control when the module is updated. Moreover, it is not operating system-specific, which means that you can use it even when you are not on your usual development machine. The other advantage of pip is that it allows modules to be installed locally, which is useful if you don't have administrative privileges on the computer you are using.
Use pip
If both Python and Python3 are installed on your system, the command you want to use is probably pip3, which is used to distinguish between the pip commands of Python 2.x. If you're not sure, try pip3 first.
The pip command works somewhat like most Linux package managers. You can use search to search for Python modules and then use install to install them. If you do not have administrative privileges to install the software on the computer you are using, you can use the-- user option to just install the module to your home directory.
$pip3 search pygame [...] Pygame (1.9.3)-Python Game Developmentsge-pygame (1.5)-A 2murD game engine for Pythonpygame_camera (0.1.1)-A Camera lib for PyGamepygame_cffi (0.2.1)-A cffi-based SDL wrapper that copies the pygame API. [...] $pip3 install Pygame-- user
Pygame is a Python module, which means it's just a set of libraries that can be used in your Python program. In other words, it's not a program that you can start like IDLE or Ninja-IDE.
Getting started with Pygame
A video game requires a background setting: where the story takes place. In Python, there are two different ways to create your story background:
Set a background color
Set up a background picture
Your background is only a picture or a color. Your video game character cannot interact with what is in the background, so don't put something too important behind it. It's just a setup decoration.
Set up your Pygame script
To start a new Pygame project, first create a folder on your computer. All the files of the game are placed in this directory. It is extremely important to keep all the files you need to run the game inside your project folder.
A Python script starts with the file type, your name, and the license you want to use. Use an open source license so that your friends can improve your game and share their changes with you:
#! / usr/bin/env python3# by Seth Kenlon # # GPLv3# This program is free software: you can redistribute it and/or# modify it under the terms of the GNU General Public License as# published by the Free Software Foundation, either version 3 of the# License, or (at your option) any later version.## This program is distributed in the hope that it will be useful, but# WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU# General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program. If not, see.
Then you tell Python which module you want to use. Some modules are common Python libraries, and of course you want to include a Pygame module that you just installed.
Import pygame # load the pygame keyword import sys # Let python use your file system import os # to help python identify your operating system
Since you will do a lot of work with this script file, it is helpful to divide it into paragraphs in the file so that you know where to put the code. You can do this with block comments, which are visible only when you look at your source code. Create three blocks in your code.
'' Objects''' # places Python classes and functions here''Setup''' # places one-time running code here' 'Main Loop''' # places game loop code instructions here
Next, set the window size for your game. Note that not everyone has a large computer screen, so * use the screen size of a computer that suits most people.
Here's a way to switch to full-screen mode, as many modern video games do, but since you're just getting started, just set one size for simplicity.
'' Setup'''worldx = 960worldy = 720
Before you can use the Pygame engine in your script, you need some basic settings. You must set the frame rate, start its internal clock, and then start (init) Pygame.
Fps = 40 # frame rate ani = 4 # Animation Loop clock = pygame.time.Clock () pygame.init ()
Now you can set your background.
Set background
Before you continue, open a graphics application to create a background for your game world. Save it as stage.png inside the images folder in your project directory.
Here are some free graphics applications you can use.
Krita is a professional drawing material simulator that can be used to create beautiful pictures. If you are very interested in creating video game works of art, you can even buy a series of tutorials of game works of art.
Pinta is a basic, easy-to-learn drawing application.
Inkscape is a vector graphics application. Use it to draw shapes, lines, splines, and Bezier curves.
Your image doesn't have to be complicated, you can go back and change it later. Once you have it, add this code to the Setup section of your file:
World = pygame.display.set_mode ([worldx,worldy]) backdrop = pygame.image.load (os.path.join ('images','stage.png'). Convert ()) backdropbox = world.get_rect ()
If you fill the background of your game with only one color, what you need to do is:
World = pygame.display.set_mode ([worldx,worldy])
You must also define colors to use. In your Setup section, use red, green, and blue (RGB) values to create some color definitions.
'' Setup''' BLUE = (25,25200) BLACK = (23her23pence23) WHITE = (254254254)
At this point, you can theoretically start your game. The problem is, it may only last a millisecond.
To prove this, save your file as your-name_game.py (replace your-name with your real name). And start your game.
If you are using IDLE, run your game by selecting "Run Module" from the "Run" menu.
If you are using Ninja, click the "Run file" button in the left button bar.
You can also run a Python script directly from a Unix terminal or a Windows command prompt.
$python3. / your-name_game.py
If you are using Windows, use this command:
Py.exe your-name_game.py
Start it, but don't expect much, because your game now lasts only a few milliseconds. You can fix it in the next section.
Cycle
Unless otherwise noted, a Python script is run once and only once. The computer is running very fast these days, so your Python script will take less than 1 second to run.
To force your game to be open and active long enough for people to see it (let alone play with it), use a while loop. To keep your game open, you can set a variable to some value and then tell a while loop to keep the loop as long as the variable remains unchanged.
This is often referred to as a "main loop", and you can use the term main as your variable. Add code anywhere in your Setup section:
Main = True
During the main loop, use the Pygame keyword to check whether the key on the keyboard has been pressed or released. Add this code to your main loop:
'' Main loop'''while main = = True: for event in pygame.event.get (): if event.type = = pygame.QUIT: pygame.quit () Sys.exit () main = False if event.type = = pygame.KEYDOWN: if event.key = = ord ('q'): pygame.quit () sys.exit () main = False
It is also in your cycle that refreshes the background of your world.
If you use a picture as the background:
World.blit (backdrop, backdropbox)
If you use a color as the background:
World.fill (BLUE)
*, tell Pygame to refresh everything on the screen and push the game's internal clock.
Pygame.display.flip () clock.tick (fps)
Save your file and run it again to see the most boring game you've ever created.
Quit the game and press Q on your keyboard.
After reading the above, have you mastered how to build a game framework with Python and Pygame modules? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.