A Preliminary Study of pygame-Making Simple Games

Posted by clarencek on Sun, 04 Aug 2019 15:41:52 +0200

Introduction to pygame

PyGame is an extension module for game writing in Python. It is a game library written by SDL library. The full name of SDL is Simple Direct Media Layer, written by a bull named Sam Lantinga. SDL is written in C, but it can also be developed using C++, of course, there are many other languages, Pygame has existed for a long time, many excellent programmers joined in, Pygame to do better and better.

2. pygame installation

The installation of pygame is the same as that of other extension libraries. First, download the installation package on the official website and download the website as follows: http://www.pygame.org/download.shtml

Then we can install pyGame through the cmd command: pip install pygame. The specific steps can refer to the process of opening CV library installation. https://blog.csdn.net/qq_35166974/article/details/94651492.

3. Creating games

The original use of MATLAB to write games, the first use of pygame to write games, found that in fact, pygame and MATLAB writing methods are quite different, let's write our first small program according to the tutorial.

First, let's show a little girl in the garden.

Let me give you the code below:

litte_girl_image_path = 'C:\\Users\\Administrator\\Desktop\\nvhai_alpha.tif'
garden_image_path = 'C:\\Users\\Administrator\\Desktop\\huayuan.jpg'

import  pygame
import sys

pygame.init()
size = width,height = 1024,1024
screen = pygame.display.set_mode(size,0,32)
litte_girl = pygame.image.load(litte_girl_image_path).convert_alpha()
garden = pygame.image.load(garden_image_path).convert()

while(True):
    for event in pygame.event.get():
        if event.type == quit:
            sys.exit()
        
        screen.blit(garden,(0,0))
        screen.blit(litte_girl,(300,100))
        pygame.display.update()

The effect after operation is as follows:

I will not introduce the specific meaning of the code in detail here. You can go to the pygame website to find the corresponding introduction. Here we introduce the alpha channel of the picture. Here we use the alpha channel of the image to make the background of the image transparent. There will be a special article to introduce the alpha channel of the image.

Let the little girl move in the garden

The following code is given:

litte_girl_image_path = 'C:\\Users\\Administrator\\Desktop\\nvhai_alpha.tif'
garden_image_path = 'C:\\Users\\Administrator\\Desktop\\huayuan.jpg'

import  pygame
import sys

pygame.init()
size = width,height = 1024,1024
screen = pygame.display.set_mode(size,0,32)#Setting Display Screen Size
litte_girl = pygame.image.load(litte_girl_image_path).convert_alpha()#Load the little girl picture (alpha channel picture)
garden = pygame.image.load(garden_image_path).convert()#Load garden pictures
litte_girl_rect = litte_girl.get_rect()#Rectangular area of a girl's picture
speed = [1,0]#Girls'Moving Speed

while(True):
    for event in pygame.event.get():#Behavior Detection
        if event.type == quit:
            sys.exit()#Exit the game
        
    litte_girl_rect = litte_girl_rect.move(speed)
    screen.blit(garden,(0,0))
    screen.blit(litte_girl,litte_girl_rect)
    pygame.display.flip()

The animation effect is not given. My animation software has problems and can't be saved. Let's run the program to see the effect. ~Today, I just introduce pygame briefly. I hope you can learn something useful. ~It's interesting to play this when you're free.~

Make a little progress every day~

Topics: MATLAB Python pip