Basic tutorial for getting started with Python pygame

WBOY
Release: 2022-07-29 21:04:27
forward
4292 people have browsed it

This article brings you relevant knowledge about Python. Everyone knows that pygame is a cross-platform Python module, specially designed for video games, including images and sounds. The following is an introduction to Python pygame I hope this information is helpful to everyone.

Basic tutorial for getting started with Python pygame

[Related recommendations: Python3 video tutorial]

Introduction to pygame

pygame can implement one of the python games Basic package.

pygame implementation window

Initialize pygame, init() is similar to the initialization method of the java class and is used for pygame initialization.

pygame.init()

Set the screen, (500,400) sets the initial size of the screen to 500 * 400, 0 and 32 are more advanced usage. In this way we set up a 500*400 screen.

surface = pygame.display.set_mode((500, 400), 0, 32)

If the pygame event is not set, the window will flash away. Here we capture pygame events. If you do not press exit, the window will remain open, which makes it easier for us to set up different content displays.

pygame.display.set_caption("My pygame game")

pygame.display,set_caption sets the title of the window

import pygame, sys
from pygame.locals import *

pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
Copy after login

Set the screen background color

Set the background color here to (255, 255,255), and then update the screen

# 设置背景颜色
surface.fill((255, 255, 255))
# 更新屏幕
pygame.display.update()
Copy after login

Add text

First get the Font object, render the Font object, and then set the text position. pygame.font.SysFont(None, 40) gets the text object, and then renders the text as a surface object. The first parameter of the basicFont.render method is the text, the second is whether to remove aliasing, the third and fourth are the color of the text and the background color of the text. Then use blit to render the text to the screen in an area of ​​the screen. Note that the rendering here must be after the fill color of the screen, otherwise the text will be covered.

# 获取字体对象
basicFont = pygame.font.SysFont(None, 40)
# surface对象
text = basicFont.render('秀儿', True, (255,255,255), (0,255,0))
# 设置文本位置
textRect = text.get_rect()

textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
# 将渲染的surface对象更新到屏幕上
surface.blit(text,textRect)
Copy after login

As shown in the picture above, Chinese displays garbled characters. Here we get the system font and set one of the Chinese fonts as the default font.

# 获取当前系统字体
fonts = pygame.font.get_fonts()
print(fonts)
Copy after login

Complete code

import pygame,sys
from pygame.locals import *


pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")
surface.fill((255, 255, 255))

# 获取字体对象
basicFont = pygame.font.SysFont("方正粗黑宋简体", 48)
# surface对象
text = basicFont.render('秀儿', True, (255,255,255), (0,255,0))
# 设置文本位置
textRect = text.get_rect()

textRect.centerx = surface.get_rect().centerx
textRect.centery = surface.get_rect().centery
# 将渲染的surface对象更新到屏幕上
surface.blit(text,textRect)

pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
Copy after login

Drawing polygon

polygon to draw polygons, the first parameter is the screen object, the second is the color, and the third is a point string A connected tuple, the last point is consistent with the first one

import pygame,sys
from pygame.locals import *


pygame.init()

surface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption("我的pygame游戏")
surface.fill((255, 255, 255))

pygame.draw.polygon(surface, (0, 0, 255), ((50, 40), (100, 100), (120, 80), (50, 40)))

pygame.display.update()
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
Copy after login

Draw a straight line

line method, the first parameter is the screen Object, followed by color and two points, the last parameter is the line width

pygame.draw.line(surface, (0, 0, 255), (50, 40), (100, 100), 10)
Copy after login

Drawing a circle

circle is used to draw a circle, the first The first parameter and the second parameter are the screen object and color, followed by the circle center and radius, and the last one represents the width. If set to 0, it is a real circle.

pygame.draw.circle(surface, (0, 0, 255), (50, 40), 20, 10)
Copy after login

Draw an ellipse

The first parameter and the second parameter are the same as above, and the third parameter specifies the upper left corner of the x and y axes respectively, and then is the radius of x and y, and the last one is the width

pygame.draw.ellipse(surface, (0, 0, 255), (50, 40, 20, 10), 2)
Copy after login

Draw a rectangle

rect to draw a rectangle, the first and second parameters are the same as above, The third parameter specifies the upper left corner and lower right corner respectively

pygame.draw.rect(surface, (0, 0, 255), (50, 40, 20, 10))
Copy after login

[Related recommendations: Python3 video tutorial]

The above is the detailed content of Basic tutorial for getting started with Python pygame. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!