python游戏编程入门p_Pygame - Python游戏编程入门(4)

1 #-*- coding = utf-8 -*-

2 """

3 @author: Will Wu4

5 增加功能:6 1. 玩家碰到敌人会坠毁7 2. 游戏结束8 """

9

10 import pygame #导入pygame库

11 from pygame.locals import * #导入pygame库中的一些常量

12 from sys import exit #导入sys库中的exit函数

13 from random importrandint14

15 #定义窗口的分辨率

16 SCREEN_WIDTH = 480

17 SCREEN_HEIGHT = 640

18

19 #子弹类

20 classBullet(pygame.sprite.Sprite):21

22 def __init__(self, bullet_surface, bullet_init_pos):23 pygame.sprite.Sprite.__init__(self)24 self.image =bullet_surface25 self.rect =self.image.get_rect()26 self.rect.topleft =bullet_init_pos27 self.speed = 8

28

29 #控制子弹移动

30 defupdate(self):31 self.rect.top -=self.speed32 if self.rect.bottom <0:33 self.kill()34

35

36 #玩家类

37 classHero(pygame.sprite.Sprite):38

39 def __init__(self, hero_surface, hero_init_pos):40 pygame.sprite.Sprite.__init__(self)41 self.image =hero_surface42 self.rect =self.image.get_rect()43 self.rect.topleft =hero_init_pos44 self.speed = 6

45 self.is_hit = False #new

46

47 #子弹1的Group

48 self.bullets1 =pygame.sprite.Group()49

50 #控制射击行为

51 defsingle_shoot(self, bullet1_surface):52 bullet1 =Bullet(bullet1_surface, self.rect.midtop)53 self.bullets1.add(bullet1)54

55 #控制飞机移动

56 defmove(self, offset):57 x = self.rect.left + offset[pygame.K_RIGHT] -offset[pygame.K_LEFT]58 y = self.rect.top + offset[pygame.K_DOWN] -offset[pygame.K_UP]59 if x <0:60 self.rect.left =061 elif x > SCREEN_WIDTH -self.rect.width:62 self.rect.left = SCREEN_WIDTH -self.rect.width63 else:64 self.rect.left =x65

66 if y <0:67 self.rect.top =068 elif y > SCREEN_HEIGHT -self.rect.height:69 self.rect.top = SCREEN_HEIGHT -self.rect.height70 else:71 self.rect.top =y72

73 #敌人类

74 classEnemy(pygame.sprite.Sprite):75 def __init__(self, enemy_surface, enemy_init_pos):76 pygame.sprite.Sprite.__init__(self)77 self.image =enemy_surface78 self.rect =self.image.get_rect()79 self.rect.topleft =enemy_init_pos80 self.speed = 2

81

82 #爆炸动画画面索引

83 self.down_index =084

85 defupdate(self):86 self.rect.top +=self.speed87 if self.rect.top >SCREEN_HEIGHT:88 self.kill()89

90 ###########################################################################

91

92 #定义画面帧率

93 FRAME_RATE = 60

94

95 #定义动画周期(帧数)

96 ANIMATE_CYCLE = 30

97

98 ticks =099 clock =pygame.time.Clock()100 offset ={pygame.K_LEFT:0, pygame.K_RIGHT:0, pygame.K_UP:0, pygame.K_DOWN:0}101

102 #玩家坠毁图片索引

103 hero_down_index = 1 #new

104

105 #初始化游戏

106 pygame.init() #初始化pygame

107 screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT]) #初始化窗口

108 pygame.display.set_caption('This is my first pygame-program') #设置窗口标题

109

110 #载入背景图

111 background = pygame.image.load('resources/image/background.png')112 #游戏结束图

113 gameover = pygame.image.load('resources/image/gameover.png') #new

114

115 #载入资源图片

116 shoot_img = pygame.image.load('resources/image/shoot.png')117

118 #用subsurface剪切读入的图片

119 #Hero图片

120 hero_surface =[]121 hero_surface.append(shoot_img.subsurface(pygame.Rect(0, 99, 102, 126)))122 hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 360, 102, 126)))123 hero_surface.append(shoot_img.subsurface(pygame.Rect(165, 234, 102, 126)))124 hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 624, 102, 126)))125 hero_surface.append(shoot_img.subsurface(pygame.Rect(330, 498, 102, 126)))126 hero_surface.append(shoot_img.subsurface(pygame.Rect(432, 624, 102, 126)))127 hero_pos = [200, 500]128

129 #bullet1图片

130 bullet1_surface = shoot_img.subsurface(pygame.Rect(1004, 987, 9, 21))131

132 #enemy1图片

133 enemy1_surface = shoot_img.subsurface(pygame.Rect(534, 612, 57, 43))134 enemy1_down_surface =[]135 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 347, 57, 43)))136 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(873, 697, 57, 43)))137 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(267, 296, 57, 43)))138 enemy1_down_surface.append(shoot_img.subsurface(pygame.Rect(930, 697, 57, 43)))139

140 #创建玩家

141 hero =Hero(hero_surface[0], hero_pos)142

143 #创建敌人组

144 enemy1_group =pygame.sprite.Group()145

146 #创建击毁敌人组

147 enemy1_down_group =pygame.sprite.Group()148

149 #事件循环(main loop)

150 whileTrue:151

152 #控制游戏最大帧率

153 clock.tick(FRAME_RATE)154

155 #绘制背景

156 screen.blit(background, (0, 0))157

158 #改变飞机图片制造动画

159 if ticks >=ANIMATE_CYCLE:160 ticks =0161

162 #制造飞机动画 ######################################

163 #更新的代码段

164 ifhero.is_hit:165 if ticks%(ANIMATE_CYCLE//2) ==0:166 hero_down_index += 1

167 hero.image =hero_surface[hero_down_index]168 if hero_down_index == 5:169 break

170 else:171 hero.image = hero_surface[ticks//(ANIMATE_CYCLE//2)]172 ##################################################

173

174 #射击

175 if ticks % 10 ==0:176 hero.single_shoot(bullet1_surface)177 #控制子弹

178 hero.bullets1.update()179 #绘制子弹

180 hero.bullets1.draw(screen)181

182 #产生敌机

183 if ticks % 30 ==0:184 enemy = Enemy(enemy1_surface, [randint(0, SCREEN_WIDTH - enemy1_surface.get_width()), -enemy1_surface.get_height()])185 enemy1_group.add(enemy)186 #控制敌机

187 enemy1_group.update()188 #绘制敌机

189 enemy1_group.draw(screen)190

191 #检测敌机与子弹的碰撞

192 enemy1_down_group.add(pygame.sprite.groupcollide(enemy1_group, hero.bullets1, True, True))193

194 for enemy1_down inenemy1_down_group:195 screen.blit(enemy1_down_surface[enemy1_down.down_index], enemy1_down.rect)196 if ticks % (ANIMATE_CYCLE//2) ==0:197 if enemy1_down.down_index < 3:198 enemy1_down.down_index += 1

199 else:200 enemy1_down_group.remove(enemy1_down)201

202 #检测敌机与玩家的碰撞 #################################

203 #更新的代码段

204 enemy1_down_list =pygame.sprite.spritecollide(hero, enemy1_group, True)205 if len(enemy1_down_list) > 0: #不空

206 enemy1_down_group.add(enemy1_down_list)207 hero.is_hit =True208 ####################################################

209

210 #绘制飞机

211 screen.blit(hero.image, hero.rect)212 ticks += 1 #python已略去自增运算符

213

214 #更新屏幕

215 pygame.display.update()216

217 #处理游戏退出

218 #从消息队列中循环取

219 for event inpygame.event.get():220 if event.type ==pygame.QUIT:221 pygame.quit()222 exit()223

224 #※ Python中没有switch-case 多用字典类型替代

225 #控制方向

226 if event.type ==pygame.KEYDOWN:227 if event.key inoffset:228 offset[event.key] =hero.speed229 elif event.type ==pygame.KEYUP:230 if event.key inoffset:231 offset[event.key] =0232

233 #移动飞机

234 hero.move(offset)235

236 #更新的代码段 ###############################

237 #跳出主循环

238 screen.blit(gameover, (0, 0))239 #玩家坠毁后退出游戏

240 whileTrue:241 pygame.display.update()242 for event inpygame.event.get():243 if event.type ==pygame.QUIT:244 pygame.quit()245 exit()246 ############################################

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值