Pygame (a) game programming, real starting to point different Hello World

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/suoyue_py/article/details/98985099

1.Pygame Profile

Pygame is a cross-platform Python module, the original place of sudden stop pySDL, established on the basis of the SDL, allows real-time video game developed without being bound by low-level language. Pygame relevant basic resources: the source code, documentation, and other information can be viewed at the official website
pygame official website: https://www.pygame.org/news
pygame Documentation: https://www.pygame.org/docs/
pygame functions:
graphics rendering, display pictures, animation, play a sound, collision detection, and a keyboard or a mouse or other peripherals interactive gaming handle
pygame modules:
Here Insert Picture Description

2.Pygame installation

Pygame program at the beginning of the game, install it pygame, enter the command line window:
PIP install pygame
Here Insert Picture Description
display the installation is complete. Cipian python-based foundation, unfinished foundation into the pit of Python see:
basic introduction into the pit with the Python environment settings

3.Pygame first game

Accustomed to begin programming are starting to say hello to the world, following on with pygame play different Hello World ~~~

HW1:

Code:

import pygame       #导入pygame库
from pygame.locals import*      #导入一些常用的函数和变量
pygame.init()       #初始化pygame
pygame.display.set_caption("锁钥 の Hello World !!!")       #设置窗口标题 
BG = (0,0,255)        #定义背景的RGB颜色
screen = pygame.display.set_mode((640,480))       #创建窗口大小为:640*480  
screen.fill(BG)     #填充背景
pygame.display.flip()   #更新界面

的输入方式:键盘E键(不行则先输入键D+E,然后再输入键E)
运行界面: 窗口标题+蓝色界面
从python的Shell(命令解析器)窗口可见程序一运行就结束,但程序界面并没有立即关闭,这是pygame的事件循环与IDLE的事件循环发生冲突,使界面一直存在。若直接双击保存的 .py文件打开程序,程序的正常现象是一闪而过。
Here Insert Picture Description
窗口是出来了,标题也显示了Hello World 。这就结束了?
Here Insert Picture Description
进阶版来定义一下手动关闭程序,避免双击启动程序时出现一闪而过&_&,并更换一下单调的颜色界面,找张视野宽阔的炫酷图片来当界面 ~~~

HW2:

代码:

import pygame       #导入pygame库
from pygame.locals import*      #导入一些常用的函数和变量
import sys 		    #使用sys模块的exit函数来退出程序
pygame.init()       #初始化pygame
pygame.display.set_caption("锁钥 の Hello World !!!")       #设置窗口标题
SCREEN_SIZE = (640,480)    #定义窗口大小的变量
background_image = 'bg.jpg'   #指定背景图像文件名称
screen = pygame.display.set_mode(SCREEN_SIZE)       #创建窗口
background = pygame.image.load(background_image).convert()     #加载背景图并用convert函数将图像数据转换为Surface对象返回
while True:     #主循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:      #接收到退出事件后退出程序
            sys.exit()        
    screen.blit(background,(0,0))   #将背景图画上去
    pygame.display.update()     #刷新页面    

将代码和背景图放在同一个文件夹下
库的导入方式: import +库名 或 from +库名 + import*,*代表全部函数和变量;若知道所需要库的模块可直接导入该模块,例如上面使用sys库的exit模块也可直接写成 from sys import exit
blit函数: 第一个参数为一个Surface对象,第二个为左上角位置坐标,画完后要用update或flip刷新一下,否则画面一片漆黑。
运行界面:
在Sheel窗口可见程序一直还没有结束,需要手动点击结束
Here Insert Picture Description
这就没了,怎么可能,程序都还没动起来,怎么会没了呢 >_<
最终版让程序动起来
Here Insert Picture Description

HW3:

运行界面:
Here Insert Picture Description
将代码、背景图和角色图放在同一个文件夹下
完整代码:

import pygame       #导入pygame库
from pygame.locals import*      #导入一些常用的函数和变量
from sys import exit    #使用sys模块的exit函数来退出程序

pygame.init()       #初始化pygame
pygame.display.set_caption("锁钥 の Hello World !!!")       #设置窗口标题

SCREEN_SIZE = width,height = 640,480    #定义窗口大小变量
SPEED = [-2,1]      #定义移动速度
background_image = 'bg.jpg'   #指定背景图像文件名称,使用RGB颜色来填充背景也行

screen = pygame.display.set_mode(SCREEN_SIZE,0,32)       #创建窗口

role = pygame.image.load("role.png")    	#加载图片并转换为Surface对象返回
background = pygame.image.load(background_image).convert()     #加载背景图并用convert函数将图像数据转换为Surface对象返回
position = role.get_rect()  	#获得图像的位置矩形(左上角和右下角坐标)

while True:     #游戏主循环
    for event in pygame.event.get():
        if event.type == pygame.QUIT:      #接收到退出事件后退出程序
            exit()

    position = position.move(SPEED)     #移动图像
    
    if position.left<0 or position.right>width:     #左右边界判断,避免出界
        role = pygame.transform.flip(role,True,False)   #翻转图像 变量1:返回surface对象;变量2:是否水平翻转;变量3:是否垂直翻转
        SPEED[0] = -SPEED[0]    #反方向移动
    if position.top<0 or position.bottom>height:    #上下边界判断,避免出界
        SPEED[1] = -SPEED[1]
        
    screen.blit(background,(0,0))   #将背景图画上去
    screen.blit(role,position)      #更新图像

    pygame.display.flip()       #更新界面  或用pygame.display.update()   #刷新页面
    pygame.time.delay(5)   	#延迟10毫秒

核心代码分析:
set_mode(resolution=(0,0),flags=0,depth=0): 返回一个Surface对象,代表桌面上出现的那个窗口,包含三个参数。
参数1:一个元组,必须填写,代表分辨率;
参数2:一个标志位,有如下表6种特性,若不用什么特性,就指定为0;
参数3:色深,一般填写 32。
Here Insert Picture Description
convert函数: 是将图像数据都转化为 Surface 对象,每次加载完图像后就要做这个事件(十分常用了,即使不写 pygame也会默认已写)。
rect()函数: 返回图像的位置矩形(左上角和右下角坐标),指定方法可以用一个四元素的元组,或者两个二元素的元组,前两个数为左上坐标,后两位为右下坐标;一般来说在制定一个区域的时候,矩形是必须的,比如在屏幕的一部分画东西。
程序的刷新: HW1的pygame.display .flip() 和这里的 pygame.display .update() 都是刷新。但当标志位使用双缓冲(更快)DOUBLEBUF 的游戏显示了,记得要使用pygame.display.flip()来刷新显示,pygame.display.update()是将数据画到前面显示,而这个是交替显示的意思。

注意:
  • python程序是依靠代码块的缩进来体现代码之间的逻辑关系的,缩进结束就表示一个代码块结束了。
  • 类定义、函数定义、选择结构、循环结构、with块,行尾的冒号表示缩进的开始。
  • 同一个级别的代码块的缩进量必须相同
  • 一般而言,以4个空格为基本缩进单位
  • Indent and Backspace shortcut keys:
    Indent -> tab key or Ctrl +]
    Backspace -> Ctrl + [

Please pay attention to timely update micro letter public, " Barry lock and key ," No public reply back " pygame episode " fetch the source code, images and the packaged executable file ( how to package? Portal )
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/suoyue_py/article/details/98985099