点击蓝字 关注我们
Python是一门非常简单的语言,快速入门之后可以做很多事情!比如爬虫啊,数据分析啊,自动化运维啊,机器学习,量化分析等等!但是入门到进阶的过程有时会非常痛苦,如果有一些好玩有趣的例子就好了。
比如通过游戏来学编程是一个非常好的途径,今天强哥就来写一个非常好玩的打飞机游戏,大概就1000多行,非常不错!
1、打飞机的游戏
打飞机的游戏估计很多人都玩过,雷霆战机相信很多80后的小伙伴都玩过!
Python是一门非常简单的语言,快速入门之后可以做很多事情!比如爬虫啊,数据分析啊,自动化运维啊,机器学习,量化分析等等!
但是入门到进阶的过程有时会非常痛苦,如果有一些好玩有趣的例子就好了。比如通过游戏来学编程是一个非常好的途径
2、代码讲解
1.代码的结构
2.游戏的角色文件
gameRole 整个游戏分三个角色,下面我一一来解释一下,思路其实非常清晰的。
1)一个是子弹
初始化子弹的图片,然后得到它在画布上的坐标,并控制它的移动速度
2)敌机
会随机出一堆敌人的飞机,直管往前冲,从屏幕的上方往下方蜂拥而至,不需要考虑其他的行为!
敌机有几个重要的属性,比如它的飞行图片和击落的图片,然后获取的屏幕上的坐标。敌机的行为就一个飞,而且是只会往前飞。
3)我方战机
玩家类class Player(pygame.sprite.Sprite):` `def __init__(self, plane_img, player_rect, init_pos):` `pygame.sprite.Sprite.__init__(self)` `self.image = [] # 用来存储玩家对象精灵图片的列表` `for i in range(len(player_rect)):` `self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())` `self.rect = player_rect[0] # 初始化图片所在的矩形` `self.rect.topleft = init_pos # 初始化矩形的左上角坐标` `self.speed = 8 # 初始化玩家速度,这里是一个确定的值` `self.bullets = pygame.sprite.Group() # 玩家飞机所发射的子弹的集合` `self.img_index = 0 # 玩家精灵图片索引` `self.is_hit = False # 玩家是否被击中 def shoot(self, bullet_img): bullet = Bullet(bullet_img, self.rect.midtop) self.bullets.add(bullet) ` `def moveUp(self):` `if self.rect.top = SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed ` `def moveLeft(self):` `if self.rect.left = SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed
我方的战机稍微复杂一点,它有这么几个主要的属性,飞行的图片,被击落的图片,屏幕坐标,它的子弹等等!然后我们需要控制它的飞机方向,向上,向下,左边和右边,发射子弹。
完整代码请添加小助手获取哦,有什么不懂的问题也可以随时咨询~
请长按扫码添加小助手
3.主游戏部分文件mainGame
1)先是初始化游戏的界面大小,字体等等,读取声音和图片和基本配置:
class HeroPlane(BasePlane): global supply_size ` `def __init__(self, screen_temp):` `BasePlane.__init__(self, 3, screen_temp, 210, 728, "./feiji/hero1.png", 4, HP_list[3]) # super().__init__()` `BasePlane.crate_images(self, "hero_blowup_n")` `self.key_down_list = [] # 用来存储键盘上下左右移动键` `self.space_key_list = [] # 保存space键` `self.is_three_bullet = False` `self.barrel_2 = [] # 2号炮管(左)` `self.barrel_3 = [] # 3号炮管(右)` `self.three_bullet_stock = 50 # 三管齐发子弹初始值为50 # 单键移动方向 def move_left(self): self.x -= 7 ` `def move_right(self):` `self.x += 7 def move_up(self): self.y -= 6 ` `def move_down(self):` `self.y += 6 # 双键移动方向 def move_left_and_up(self): self.x -= 5 self.y -= 6 ` `def move_right_and_up(self):` `self.x += 5` `self.y -= 6 def move_lef_and_down(self): self.x -= 5 self.y += 6 ` `def move_right_and_down(self):` `self.x += 5` `self.y += 6 # 控制飞机左右移动范围s def move_limit(self): if self.x 480: self.x = 380 if self.y > 728: self.y = 728 elif self.y self.x + 0.05 * width and supply_temp_right_x self.y + 0.1 * height: if supply_temp.supply_type == 0: # 0为血量补给,吃到血量补给 self.HP -= supply_temp.supply_HP # 血量-(-3) if self.HP > 41: # 血量最大值为41 self.HP = 41 show_score_HP() else: # 吃到弹药补给 self.is_three_bullet = True self.three_bullet_stock += 20 # 三管炮弹余量+20 del_supply(supply_temp)
2)游戏的逻辑部分
函数def del_outWindow_bullet(plane):` `"""删除plane的越界子弹"""` `bullet_list_out = [] # 越界子弹` `for bullet in plane.bullet_list:` `bullet.display()` `bullet.move()` `if bullet.judge(): # 判断子弹是否越界` `bullet_list_out.append(bullet)` `# 删除越界子弹` `if bullet_list_out:` `for bullet in bullet_list_out:` `plane.bullet_list.remove(bullet)` `# 如果为hero并且为三管齐发则判断炮管23的子弹是否越界` `if plane.plane_type == 3 and (plane.barrel_2 or plane.barrel_3):` `barrel2_bullet_out = [] # 越界子弹` `barrel3_bullet_out = [] # 越界子弹` `# 判断炮管2` `for bullet in plane.barrel_2:` `bullet.display()` `bullet.move()` `if bullet.judge(): # 判断子弹是否越界` `barrel2_bullet_out.append(bullet)` `# 删除越界子弹` `if barrel2_bullet_out:` `for bullet in barrel2_bullet_out:` `plane.barrel_2.remove(bullet)` `# 判断炮管3` `for bullet in plane.barrel_3:` `bullet.display()` `bullet.move()` `if bullet.judge(): # 判断子弹是否越界` `barrel3_bullet_out.append(bullet)` `# 删除越界子弹` `if barrel3_bullet_out:` `for bullet in barrel3_bullet_out:` `plane.barrel_3.remove(bullet)` `def del_plane(plane):` `"""回收被击中的敌机的对象"""` `global hero` `global hit_score` `global enemy0_list` `global enemy1_list` `global enemy2_list` `if plane in enemy0_list: # 回收对象为enemy0` `enemy0_list.remove(plane)` `elif plane in enemy1_list:` `enemy1_list.remove(plane)` `elif plane in enemy2_list:` `enemy2_list.remove(plane)` `elif plane == hero: # 回收对象为hero` `hit_score = 0` `show_score_HP()` `hero = None` `def del_supply(supply):` `"""回收补给"""` `global blood_supply` `global bullet_supply` `if supply == blood_supply: # 回收对象为血量补给` `blood_supply = None` `elif supply == bullet_supply:` `bullet_supply = None` `def reborn():` `"""Hero重生"""` `global hero` `global window_screen` `global hit_score` `hero = HeroPlane(window_screen)` `show_score_HP()` `hit_score = 0` `# 将最高分写入到文件def max_score_2_file(): global hit_score file = None try: file = open(“./飞机大战得分榜.txt”, ‘r+’) except Exception: file = open(“./飞机大战得分榜.txt”, ‘w+’) finally: if file.read(): # 判断文件是否为空 file.seek(0, 0) # 定位到文件开头 file_score = eval(file.read()) if hit_score > file_score: file.seek(0, 0) # 定位到文件开头 file.truncate() # 清空文件内容 file.write(str(hit_score)) else: file.write(str(hit_score)) file.close() def create_enemy_plane(): “”“生成敌机”“” global window_screen global hit_score global enemy0_list global enemy0_maximum global enemy1_list global enemy1_maximum global enemy2_list global enemy2_maximum global HP_list if hit_score = random_appear_boss1 and (hit_score % random_appear_boss1) == 0) and len( enemy1_list) = random_appear_boss2 and (hit_score % random_appear_boss2) == 0) and len( enemy2_list)
` `
上面这一堆代码其实就是干下面几个事情:
-
先绘制出背景幕布
-
再绘制出玩家的战机,敌机
-
绑定战机和敌机的鼠标和键盘响应事件
-
发射子弹,通过坐标来判断子弹和敌机的碰撞,以及敌机和玩家战机的碰撞
-
最后还要计算得分
点击下方安全链接前往获取
CSDN大礼包:《Python入门&进阶学习资源包》免费分享
👉Python实战案例👈
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
👉Python书籍和视频合集👈
观看零基础学习视频,看视频学习是最快捷也是最有效果的方式,跟着视频中老师的思路,从基础到深入,还是很容易入门的。
👉Python副业创收路线👈
这些资料都是非常不错的,朋友们如果有需要《Python学习路线&学习资料》,点击下方安全链接前往获取
CSDN大礼包:《Python入门&进阶学习资源包》免费分享
本文转自网络,如有侵权,请联系删除。