用python做游戏的细节详解

PyGame是一个Python的库,能够让你更容易的写出一个游戏。它提供的功能包括图片处理和声音重放的功能,并且它们能很容易的整合进你的游戏里。去官网点击这里下载适合你的PyGame安装包。

大家可以参阅:Python中pygame安装方法图文详解

我们就拿打飞机来做个例子

1 .创建游戏框架以及游戏背景

?
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
106
107
108
109
110
111
112
113
#这个模块放一些常用的工具和基础类和精灵类
 
#在其他模块调用
 
import pygame
 
import random
 
#设置游戏屏幕大小 这是一个常量
 
SCREEN_RECT = pygame.Rect( 0 , 0 , 580 , 700 )
 
#敌机的定时器事件常量
 
CREATE_ENEMY_EVENT = pygame.USEREVENT
 
  
 
#定制一个精灵类,需要继承pygame提供的精灵类
 
#需要定义的属性有:
 
#image图片
 
#rect坐标
 
#speed速度
 
  
 
#接下来开始写敌机方面的内容 产生敌机
 
#先定义一个事件常量
 
CREATE_ENEMY_EVENT = pygame.USEREVENT
 
#我们还可以定义一个事件常量(发射子弹)
 
HERO_FIRE_EVENT = pygame.USEREVENT + 1
 
  
 
class GameSprite(pygame.sprite.Sprite):
 
   def __init__( self ,new_image,new_speed = 1 ):
 
     super ().__init__()
 
     #图片
 
     self .image = pygame.image.load(new_image)
 
     #速度
 
     self .speed = new_speed
 
     #位置 获取图片的宽和高 get_rect()(0,0,宽,高)
 
     self .rect = self .image.get_rect()
 
     #精灵移动的速度 包括英雄精灵 背景精灵 敌机精灵 子弹精灵
 
     self .speed = new_speed
 
  
 
   def update( self ):
 
     #默认垂直方向移动 y轴控制垂直方向
 
     self .rect.y + = self .speed
 
     #self.rect.x += 1
 
#以上是游戏的基础类,接下来设置背景类
 
#明确背景类继承自游戏的精灵类
 
class Background(GameSprite):
 
   def __init__( self ,is_alt = False ):
 
     #is_alt判断是否为另一张图像
 
     #False表示第一张图像
 
     #Ture表示另外一张图像
 
     #两张图像交替循环
 
     #传图片
 
     super ().__init__( "/home/zhangyuan/下载/beijing.png" )
 
     if is_alt:
 
       #如果是第二张图片 初始位置为-self.rect.height
 
       self .rect.y = - self .rect.height
 
   #def __init__(self,new_image):
 
   #  super().init__(new_image)
 
   def update( self ):
 
     #调用父类方法
 
     super ().update()
 
     if self .rect.y > = SCREEN_RECT.height:
 
       self .rect.y = - self .rect.height

2 .创建敌机精灵

?
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
class Enemy(GameSprite):
 
   def __init__( self ):
 
  
 
   super ().__init__( "/home/zhangyuan/images/enemy1.png" )
 
   #随机速度
 
   self .speed = random.randint( 10 , 15 )
 
   #设置敌机的初始位置
 
   self .rect.left = SCREEN_RECT.width
 
   max_ = SCREEN_RECT.height - self .rect.height
 
   self .rect.bottom = random.randint( 0 , max_)
 
  
 
def update( self ):
 
    
 
   panduan = random.randint( 0 , 1 )
 
   if panduan = = 0 :
 
     self .rect.y - = self .speed
 
     self .rect.x - = self .speed
 
   else :
 
     self .rect.y + = self .speed
 
     self .rect.x - = self .speed
 
   #判断敌机是否飞出屏幕 如果飞出屏幕将敌机从精灵组删除
 
   if self .rect.y > = SCREEN_RECT.height or self .rect.right < = 0 or self .rect.bottom < = 0 :
 
     self .kill()

3 .创建英雄精灵

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Bullet(GameSprite):
 
  
 
     def __init__( self ):
 
       super ().__init__( "/home/zhangyuan/images/bullet1.png" , - 5 )
 
     def update( self ):
 
       super ().update()
 
  
 
    #判断是否超出屏幕 如果是 从精灵组删除
 
    if self .rect.bottom < 0 :
 
      self .kill()

5 .碰撞检测

?
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
#第一个参数和第二个参数是要参与碰撞检测的精灵
 
#第三个参数为Ture的时候 就是当碰撞的时候被碰撞的精灵从精灵组移除
 
pygame.sprite.groupcollide( self .enemy_group, self .hero.bullet, True , True ) #子弹
 
#判断列表时候有内容
 
   if len (enemies)> 0 :
 
     #让英雄牺牲
 
     self .hero.kill()
 
     #结束游戏
 
     PlaneGame.__game_over()
 
@staticmethod
 
def __game_over():
 
   print ( "游戏结束" )
 
   #这是pygame提供的卸载模块功能
 
   pygame.quit()
 
   #这是pygame本身提供的退出脚本的功能
 
   exit()
 
   #需要先卸载pygame模块 然后退出脚本
 
  
 
   if __name__ = = "__main__" :
 
    game = PlaneGame()
 
    game.star_game()

猜你喜欢

转载自www.cnblogs.com/orzhangz/p/11088070.html