We assume that you have already installed sublime text for our python program development. If you don't know how to install it, you can refer to my previous blog post. The only thing that needs to be solved here is the problem of configuring Pygame. This blog post mainly solves the development environment configuration and the display problem of the first game interface.
Article directory
Original source of the article: https://blog.csdn.net/haigear/article/details/130173836
1. Configure pygame
Without pygame, it is still relatively difficult for us to use Python to write games. Although we all say how simple and powerful Python is, the reason why Python is so powerful is that it has all kinds of support libraries behind it. Here we focus on how to install in sumbline text, why use sublime text, it is lightweight and suitable for small partners who are trying to get started.
1. Install pygame
If you have never heard of pygame before, you can go directly to its official website, www.pygame.org . The old way, we enter the command pip install pygame in cmd mode.
My installation event is relatively long, but remember A few years ago, the installation was very fast, and I didn't know where the problem was. Are there many more people playing this stuff now? ? Don't know, we don't care about it. If you really dislike it too slow, use the Tsinghua mirror image:
pip install pygame -i https://pypi.mirrors.ustc.edu.cn/simple/
What is called a successful installation? If we include the pygame package in our test code file, the compilation is successful without reporting an error. Of course, you can also execute import pygame under cmd, as follows: Both verification methods indicate that you can use pygame
.
3. There is a problem installing pygame
If you encounter this problem, it means that your Pip needs to be upgraded, we can check pip
Then let's upgrade it. If the network is not smooth, use the Tsinghua mirror image:
python -m pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade pip
3. Configure pygame for automatic code completion
With this auto-completion tool, we can improve our future development efficiency, and I won’t say much about the others.
2. Start the first game form
1. The general steps of the game
The necessary steps for a pygame game code are as follows:
1. Import the pygame package, including pygame.locals
2. Initialize pygame.init, and set the display mode
3. Open the while loop, otherwise your window will disappear in a flash
import pygame
from pygame.locals import *
runing=True
pygame.init()
size=width,height=(400,600)
pygame.display.set_mode(size)
pygame.display.set_caption("firstPygame Window")
while runing:
for event in pygame.event.get():
if event.type==QUIT:
runing=False
2. Code Description
from pygame.locals import * This includes the type of pygame.event used later, pygame.key, pygame.time and other commonly used type constant enumerations, etc.
pygame.init() is the initialization method of pygame, it must be run first , not
less .event.get() obtains all kinds of event types . This is used here. If there is a QUIT type among the obtained event types, then exit the while loop and the game is over. Running it, we get a black screen
3. Let the game move!
1. Change the background color
pygame.init()
size=width,height=(400,600)
screen=pygame.display.set_mode(size)
pygame.display.set_caption("firstPygame Window")
screen.fill((200,100,0))
pygame.display.update()
Note that there is no display.update here, and any modifications and settings we make to the screen will not be displayed.
2. Load a picture
Here, we load an airplane icon and background image to see how it works;
code show as below:
import pygame
from pygame.locals import *
import os
# 加载图片
aircraft=pygame.transform.scale(pygame.image.load(os.path.join("assets","aircraft.png")),(80,80))
bg=pygame.transform.scale(pygame.image.load(os.path.join("assets","bg.jpg")),(width,height))
runing=True
pygame.init()
size=width,height=(400,600)
screen=pygame.display.set_mode(size)
pygame.display.set_caption("firstPygame Window")
screen.fill((200,100,0))
#游戏运行循环
while runing:
for event in pygame.event.get():
if event.type==QUIT:
runing=False
screen.blit(aircraft,(100,300))
pygame.display.update()
When loading images here, we use the path method in the os library, and we still use the image scaling method transform.scale to scale to our satisfactory size.
3. Let's move
The purpose of the following code is to allow our aircraft to move up, down, left, and right after pressing the corresponding keys (W, S, A, D). It mainly uses pygame.key.get_pressed() to obtain the key value. One of the important methods blit is used to draw and refresh the screen.
import pygame
from pygame.locals import *
import os
size=width,height=(400,600)
# 加载图片
aircraft=pygame.transform.scale(pygame.image.load(os.path.join("assets","aircraft.png")),(80,80))
bg=pygame.transform.scale(pygame.image.load(os.path.join("assets","bg.jpg")),(width,height))
runing=True
pygame.init()
screen=pygame.display.set_mode(size)
aircraft_local=aircraft.get_rect(x=100,y=200)
pygame.display.set_caption("firstPygame Window")
screen.fill((200,100,0))
while runing:
for event in pygame.event.get():
if event.type==QUIT or pygame.key.get_pressed()[pygame.K_ESCAPE]:
runing=False
presskey=pygame.key.get_pressed()
if presskey[pygame.K_a]:#left
aircraft_local.x=aircraft_local.x-1
if presskey[pygame.K_d]:#left
aircraft_local.x=aircraft_local.x+1
if presskey[pygame.K_s]:#left
aircraft_local.y=aircraft_local.y+1
if presskey[pygame.K_w]:#left
aircraft_local.y=aircraft_local.y-1
screen.blit(bg,(0,0))
screen.blit(aircraft,aircraft_local)
pygame.display.update()
If you find that the plane does not move after you press the button when the above code is running, it may be that your Chinese input method is turned on, and it is normal to switch to the English input state or simply turn it off.
The running effect is as follows:
There may be many problems in the above code, which is what we need to continue to improve later, here is just to make the plane move. Maybe you will find that the planes here take off too fast, some uncontrollably fast, and the pictures need to be beautified, and a series of other problems, we will leave them to be solved one by one later.
In the next article, we will continue to introduce how to make our aircraft more flexible.
Click here to read the second section " It is very suitable for beginners to use Python to develop games pygame-02 "
The article may be updated at any time, please indicate the original source for reprinting: https://blog.csdn.net/haigear/article/details/130173836