作用:pygame一般用来做游戏
注意:1.在使用pygame提供的功能之前,需要调用init方法
2.在游戏结束前需要调用 quit 方法
pygame中的各个函数:
1.pygame.init():该函数在使用pygame时进行初始化,只有引用了该函数才能使用pygame提供的所用功能
2.pygame.Rect():该函数可以设置一张图片的位置以及大小,这是一个特殊的函数,不需要引用init函数都可以使用
3.pygame.display.set_mode(resolution=(0,0),flags=0,depth=0):该函数可以创建游戏的主窗口对象,
参数1:指定屏幕的宽和高,默认与屏幕大小一致
参数2:指定屏幕的附加选项,默认不传递
参数3:颜色的位数,默认自动匹配
4.pygame.display.update():刷新主窗口内容
5.pygame.image.load():加载需要的图片
6.主窗口对象.blit(图像,位置):将图像绘制到指定的位置
7.pygame.time.Clock():创建一个时间对象,用来控制帧率
8.时间对象.tick(数字):控制每秒的帧率
9.pygame.event.get():事件监听,返回的是一个列表
10.pygame.sprite.Sprite:精灵,用来存储图像数据images和rect的对象
11.pygame.sprite.Group(精灵1,精灵2):创建精灵组对象
12.精灵组对象.update():更新精灵组中精灵的位置
13.精灵组对象.draw():把精灵组中的所有精灵绘画到主窗口
14.pygame.time.set_time(eventid,milliseconds):设置每个一段时间程序会执行什么内容,第一个需要基于常量pygame.USEREVENT来指定,第二个参数是毫秒值
15.pygame.key.get_pressed():按键监听,会返回所有按键的元组,通过键盘常量,判断元组中某个按键是否被触发,如果被触发对应数值为1
16.pygame.sprite.groupcollide(精灵组1,精灵组2,bool,bool):检测两个精灵组的碰撞,会返回一个字典,第一个参数和第三个参数关联,第二个参数和第四个参数关联,当bool类型为真时,碰撞会被销毁
17.pygame.sprite.spritecollide(精灵,精灵组,bool):检测精灵和精灵组的碰撞,会返回一个精灵组的列表,当bool类型为真时,精灵组会被销毁
一个飞机大战的游戏总结:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
import pygame import time from plane_Sprite import * class PlaneGame( object ): def __init__( self ): print ( '初始化' ) # 1.创建游戏窗口 self .screem = pygame.display.set_mode(SCREEM_RECT.size) # 这里需要拿到元组类型的数据,使用 .size 可以拿到数组中的数据 # 2.创建游戏时钟 self .clock = pygame.time.Clock() # 3.调用私有方法,精灵和精灵组的创建 self .__create_sprite() # 4.创建敌机定时器 pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000 ) # 5.创建子弹定时器 pygame.time.set_timer(HERO_FIRE_EVENT, 500 ) def __create_sprite( self ): # 创建背景精灵类 # bg1 = BackGround('./images/background.jpg') # bg2 = BackGround('./images/background.jpg') bg1 = BackGround() bg2 = BackGround( True ) self .back_groud = pygame.sprite.Group(bg1, bg2) # 创建背景精灵组 self .enemy_group = pygame.sprite.Group() # 创建敌机精灵组 self .he1 = Hero() self .hero_group = pygame.sprite.Group( self .he1) # 创建英雄精灵组 def StartGame( self ): print ( '开始游戏' ) while True : # 1.设置刷新的帧率 self .clock.tick(SHUA) # 2.事件监听 self .__event_handler() # 3.碰撞检测 self .__check_cllide() # 4.更新/和绘制精灵组图片 self .__update_sprite() # 5.更新显示 pygame.display.update() # 定义私有方法 def __event_handler( self ): """监听事件处理""" for event in pygame.event.get(): if event. type = = pygame.QUIT: # 调用静态方法:使用类名 PlaneGame.__game_over() # 判断定时器事件 elif event. type = = CREATE_ENEMY_EVENT: # 创建敌机对象 enemy = Enemy() # 添加到精灵组中 self .enemy_group.add(enemy) elif event. type = = HERO_FIRE_EVENT: self .he1.fire() # 获取键盘信息 key_pressed = pygame.key.get_pressed() # 判断对应的键盘 if key_pressed[pygame.K_RIGHT]: self .he1.speed = 3 elif key_pressed[pygame.K_LEFT]: self .he1.speed = - 3 elif key_pressed[pygame.K_UP]: self .he1.speed = - 2 elif key_pressed[pygame.K_DOWN]: self .he1.speed = 2 else : self .he1.speed = 0 def __check_cllide( self ): """碰撞检测""" # 子弹摧毁敌机 pygame.sprite.groupcollide( self .he1.bullets, self .enemy_group, True , True ) # 敌机撞毁英雄 enemy_list = pygame.sprite.spritecollide( self .he1, self .enemy_group, True ) if len (enemy_list) > 0 : # 销毁英雄 self .he1.kill() # 结束游戏 PlaneGame.__game_over() def __update_sprite( self ): """更新精灵组""" self .back_groud.update() # 刷新图像数据 self .back_groud.draw( self .screem) # 绘画图像 # 敌机精灵组更新 self .enemy_group.update() self .enemy_group.draw( self .screem) # 英雄精灵组更新 self .hero_group.update() self .hero_group.draw( self .screem) # 子弹精灵组更新 self .he1.bullets.update() self .he1.bullets.draw( self .screem) @staticmethod # 静态方法 def __game_over(): """结束游戏""" print ( '游戏结束' ) pygame.quit() exit() if __name__ = = "__main__" : # 创建游戏对象 plane_start = PlaneGame() # 启动游戏 plane_start.StartGame() |
上面是游戏实现功能的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
import random import pygame # 定义常量,一般使用大写字母' # 屏幕大小常量 SCREEM_RECT = pygame.Rect( 0 , 0 , 591 , 764 ) # 刷新的帧率 SHUA = 60 # 设置敌机定时器事件常量 CREATE_ENEMY_EVENT = pygame.USEREVENT # 设置英雄子弹定时器事件常量 HERO_FIRE_EVENT = pygame.USEREVENT + 1 # 因为 pygame.USEREVENT 被占用,所以需要加1进行区分 class GameSprite(pygame.sprite.Sprite): """飞机大战游戏精灵""" def __init__( self , image_name, speed = 1 ): # 需要调用超级初始化方法 super ().__init__() # 定义属性 self .image = pygame.image.load(image_name) # 加载图像 self .rect = self .image.get_rect() # 获取到图像位置变更的信息 self .speed = speed # 设置变更的速度 def update( self ): # 在屏幕垂直向上移动 self .rect.y + = self .speed # 创建一个子类,完成屏幕需求 class BackGround(GameSprite): """游戏背景精灵""" # is_alt 判断参数 def __init__( self , is_alt = False ): # 调用父类初始化方法设置参数 super ().__init__( './images/background.jpg' ) # 判断是否是叠加图像 if is_alt: self .rect.y = - self .rect.height def update( self ): # 调用父类的方法 super ().update() # 2.判断是否移出屏幕,移出屏幕,重新设置到屏幕上方 if self .rect.y > = SCREEM_RECT.height: self .rect.y = - self .rect.height # 创建敌机类 class Enemy(GameSprite): def __init__( self ): # 1.调用父类方法,创建敌机精灵,同时指定敌机图片 super ().__init__( './images/enemy_2.jpg' ) # 2.指定敌机的初始随机速度 self .speed = random.randint( 1 , 3 ) # 3.指定敌机的初始随机位置 self .rect.bottom = 0 max_x = SCREEM_RECT.width - self .rect.width # 计算x的最大值 self .rect.x = random.randint( 0 , max_x) def update( self ): # 调用父类方法,保持垂直飞行 super ().update() # 判断是否非常屏幕,是,则删除精灵组 if self .rect.y > = SCREEM_RECT.height + self .rect.height: # kill 方法可以将精灵从精灵组中移除,精灵就会被自动销毁 self .kill() def __del__( self ): # print('%s' % self.rect) pass # 创建英雄类 class Hero(GameSprite): def __init__( self ): # 1.调用父类中的初始方法,加载图片 super ().__init__( './images/planeNormal_2.jpg' , 0 ) # 2.重新设置位置 self .rect.centerx = SCREEM_RECT.centerx self .rect.bottom = SCREEM_RECT.bottom - 80 # 创建子弹组 self .bullets = pygame.sprite.Group() def update( self ): self .rect.x + = self .speed # 控制屏幕不会出界 if self .rect.x < = - 30 : self .rect.x = - 30 elif self .rect.right > = SCREEM_RECT.right + 30 : self .rect.right = SCREEM_RECT.right + 30 def fire( self ): # 设置每次发射三枚子弹 for i in ( 0 , 1 , 2 ): # 1.创建子弹精灵 bullet = Bullet() # 2.设置精灵位置 bullet.rect.bottom = self .rect.y - i * 24 bullet.rect.centerx = self .rect.centerx # 3.将精灵添加到精灵组 self .bullets.add(bullet) # 创建子弹类 class Bullet(GameSprite): def __init__( self ): # 调用父类方法 super ().__init__( './images/bullet2.jpg' , - 3 ) def update( self ): super ().update() if self .rect.bottom < 0 : self .kill() |
上面是每个精灵实现的代码,改代码段命名为 plane_Sprite,作为一个模块在功能实现代码中被引用,上面代码用到的图片需要自己找
【代码实现效果】
到此这篇关于python第三方库pygame的使用详解的文章就介绍到这了,更多相关python第三方库pygame内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/weixin_62859191/article/details/126511157