本文实例为大家分享了python实现飞机大战的具体代码,供大家参考,具体内容如下

初学Python,写了一个简单的Python小游戏。

师出bilibili某前辈

pycharm自带了第三方库pygame,安装一下就好了,很方便。

虽然很多大佬已经给出了步骤,我这里还是啰嗦一下,也为了自己巩固一下。

上图:

这里再给出代码的逻辑架构

plane_main.py

import pygame

from plane_sprites import *

class PlaneGame(object):

"""飞机大战主游戏"""

def __init__(self):

print("游戏初始化")

#1.创建游戏窗口

self.screen=pygame.display.set_mode(SCREEN_RECT.size)

#2.创建游戏的时钟

self.clock=pygame.time.Clock()

#3.调用私有方法,精灵和精灵组的创建

self.__creat_sprites()

#4.设置定时器事件-1s敌机出现

pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)

pygame.time.set_timer(HERO_FIRE_EVENT, 500)

def __creat_sprites(self):

# 创建精灵

bg1 = Background()

bg2 = Background(True)

# 创建精灵组

self.back_group = pygame.sprite.Group(bg1,bg2)

#创建敌机的精灵组

self.enemy_group=pygame.sprite.Group()

#创建英雄的精灵和精灵组

self.hero=Hero()

self.hero_group=pygame.sprite.Group(self.hero)

self.enemy_hit_group=pygame.sprite.Group();

def start_game(self):

print("游戏开始.....")

while True:

#1.设置刷新帧率

self.clock.tick(FRAME_PER_SEC)

#2.事件监听

self.__event_handler()

#3.碰撞检测

self.__check_collide()

#4.更新绘制精灵组

self.__update_sprites()

#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:

print("敌机出场")

#创建敌机精灵

enemy=Enemy()

#将敌机精灵添加到敌机精灵组

self.enemy_group.add(enemy)

elif event.type==HERO_FIRE_EVENT:

self.hero.fire()

#elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:

#print("向右移动....")

#使用键盘模块

keys_pressed=pygame.key.get_pressed()

#判断元组中对应的按键索引值

if keys_pressed[pygame.K_RIGHT]:

self.hero.speed=3

elif keys_pressed[pygame.K_LEFT]:

self.hero.speed = -3

else:

self.hero.speed = 0

def __check_collide(self):

#1.子弹摧毁敌机

enemy_hit = pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)

#2.敌机撞毁英雄

enemies=pygame.sprite.spritecollide(self.hero,self.enemy_group,True)

if len(enemies)>0:

#让英雄牺牲

# self.hero.explode()

self.hero.kill()

#结束游戏

PlaneGame.__game_over()

def __update_sprites(self):

self.back_group.update()

self.back_group.draw(self.screen)

self.enemy_group.update()

self.enemy_group.draw(self.screen)

self.hero_group.update()

self.hero_group.draw(self.screen)

self.hero.bullets.update()

self.hero.bullets.draw(self.screen)

@staticmethod

def __game_over():

print("游戏结束")

pygame.quit()

exit()

if __name__ == '__main__':

#创建游戏对象

game=PlaneGame()

#启动游戏

game.start_game()

plane_sprites.py

import random

import pygame

#屏幕大小常量

SCREEN_RECT=pygame.Rect(0,0,480,700)

#刷新帧率

FRAME_PER_SEC=60

explode_index=0

#创建定时器常量

CREATE_ENEMY_EVENT=pygame.USEREVENT

#创建发射子弹事件

HERO_FIRE_EVENT=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):

"""游戏背景精灵"""

def __init__(self,is_alt=False):

#1.调用父类方法,实现精灵创建(image/rect/speed)

super().__init__("./images/background.png")

#2.判断是否是交替图像

if is_alt:

self.rect.y = -self.rect.height

def update(self):

#1.调用父类方法实现

super().update()

#2.判断图像是否移除屏幕,若移除,则设置到屏幕上面

if self.rect.y>=SCREEN_RECT.height:

self.rect.y= -self.rect.height

class Enemy(GameSprite):

"""敌机精灵"""

def __init__(self):

#1.调用父类方法,创建敌机精灵,指定敌机图片

super().__init__("./images/enemy1.png")

#2.指定敌机的初始随机速度1-3

self.speed=random.randint(1,3)

#3.指定敌机的初始随机位置

self.rect.bottom=0

max_x=SCREEN_RECT.width-self.rect.width

self.rect.x=random.randint(0,max_x)

self.explode_index=0

def update(self):

#1.调用父类方法,保持垂直方向飞行

super().update()

#2.判断是否飞出屏幕,如果是,则删除精灵

if self.rect.y>=SCREEN_RECT.height:

#kill方法可以将精灵从精灵组中移出,精灵就会被自动销毁,然后调用下面方法

self.kill()

#if self.explode_index==5:

#return

if self.explode_index!=0:

new_rect=self.rect

super.__init__("./images/enemy1_down1.png")

self.explode_index=self.explode_index+1

self.rect=new_rect

def __del__(self):

# print("敌机挂了%s"% self.rect)

pass

class Hero(GameSprite):

"""英雄精灵"""

def __init__(self):

#1.调用父类方法

super().__init__("./images/me1.png",0)

#2.设置英雄的初始位置

self.rect.centerx=SCREEN_RECT.centerx

self.rect.bottom=SCREEN_RECT.bottom-120

#创建子弹精灵组

self.bullets=pygame.sprite.Group()

def update(self):

self.rect.x+=self.speed

if self.rect.x<0:

self.rect.x=0

elif self.rect.right>SCREEN_RECT.right:

self.rect.right=SCREEN_RECT.right

def fire(self):

print("发射子弹...")

for i in (0,1,2):

#1.创建精灵组

bullet=Bullet()

#2.设置精灵位置

bullet.rect.bottom=self.rect.y-i*20

bullet.rect.centerx=self.rect.centerx

#3.添加到精灵组

self.bullets.add(bullet)

def explode(self,screen):

new_rect=self.rect

for i in (1,2,3,4):

super.__init__("./images/me_destroy_2.png")

self.rect.centerx=new_rect.centerx

self.rect.centery=new_rect.centery

screen.blit(self.image,(self.rect.x,self.rect.y))

pygame.display.update()

class Bullet(GameSprite):

"""子弹精灵"""

def __init__(self):

#调用父类方法,设置子弹图片,设置初始速度

super().__init__("./images/bullet1.png",-2)

def update(self):

#让子弹垂直运行

super().update()

if self.rect.bottom<0:

self.kill()

def __del__(self):

print("子弹被销毁")

运行结果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

python实现飞机大战游戏_python实现飞机大战小游戏相关推荐

  1. python编程猜拳小游戏_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

  2. python编写猜大小游戏_python编写猜数字小游戏

    本文实例为大家分享了python编写猜数字小游戏的具体代码,供大家参考,具体内容如下 import random secret = random.randint(1,30) guess = 0 tri ...

  3. python如何使板子移动_python实现移动木板小游戏

    本文实例为大家分享了python实现移动木板小游戏的具体代码,供大家参考,具体内容如下 一.游戏简介 本游戏是通过python编写的小游戏,给初学者熟悉python编程语言抛砖引玉,希望有所帮助. 成 ...

  4. python贪吃蛇小游戏_python开发贪吃蛇小游戏

    3.概要设计 3.1 程序功能模块 由设计应解决的问题可知,本次的设计是使用用方向键来实现一个简易的贪吃蛇小游戏的程序,具体的功能模块如图3-1所示. 图3-1 程序功能模块 Fig.3-1 prog ...

  5. python中成语接龙游戏_python——成语接龙小游戏

    小试牛刀的简易成语接龙. 思路-- 1.网上下载成语字典的txt版本 2.通过python进行处理得到格式化的成语,并整理成字典(python字典查找速度快) 3.python程序,查找 用户输入的最 ...

  6. python成语接龙代码_python——成语接龙小游戏

    小试牛刀的简易成语接龙. 思路-- 1.网上下载成语字典的txt版本 2.通过python进行处理得到格式化的成语,并整理成字典(python字典查找速度快) 3.python程序,查找 用户输入的最 ...

  7. python做飞机大战游戏_python实现飞机大战游戏

    飞机大战(Python)代码分为两个python文件,工具类和主类,需要安装pygame模块,能完美运行(网上好多不完整的,调试得心累.实现出来,成就感还是满满的),如图所示: 完整代码如下: 1.工 ...

  8. Python版儿童识字游戏源代码,结合植物大战僵尸和儿童识字的小游戏,含学习模式和娱乐模式

    Python版儿童识字游戏源代码,结合植物大战僵尸和儿童识字的小游戏,含学习模式和娱乐模式. 娱乐模式下,僵尸会头顶不同的汉字,此时屏幕会提示要消灭的汉字,移动豌豆消灭对应汉字的僵尸,如果攻击非提示汉 ...

  9. python 贪吃蛇大作战_python实现简单贪吃蛇游戏

    本文实例为大家分享了python实现贪吃蛇游戏的具体代码,供大家参考,具体内容如下 代码: from turtle import * from random import randrange from ...

  10. python人机猜拳_python实现人机猜拳小游戏

    今天的这篇文章呢是对人机猜拳小游戏--石头剪刀布的一个描述以及代码展现 石头剪刀布游戏代码的简介:关于石头剪刀布这个小游戏,大致得到思路就是,玩家出一个手势,然后电脑再随机出一个手势,最后再判断是玩家 ...

最新文章

  1. 视频程式化的基于帧差异的时间损失
  2. 【×××系列八】Dynamic Multipoint *** for IPv6 详解
  3. Apple Music 会员免费领啦!
  4. left join 临时表_图解SQL的JOIN
  5. 1270: [BeijingWc2008]雷涛的小猫
  6. 配置 CentOS 7 的网络,及重命名网卡名
  7. java 判断是否是昨天_java判断日期是否是今天
  8. uva-110-没有for循环的排序
  9. gitblit git SERVER window 安装配置 hook post-receive 自动部署
  10. 9.react 从入门到放弃
  11. ansible源码解读
  12. 英语语法基础(适合入门者)--第一章:词、单词
  13. 手把手教你用ArcGIS做张降雨量分布专题图
  14. python有效的变量名有哪些_python变量名有哪些
  15. 离散数学 集合的运算
  16. python如何采集同花顺股票日度历史数据
  17. 中文版ASAM OpenSCENARIO与OpenDRIVE标准正式发布
  18. 一个去除pdf回车符的网页
  19. 如何调整屏显时间_电脑怎么设置自动关闭显示器的时间?
  20. 【CSDN博客频道携手图灵教育】“移动开发之我见”主题征文活动

热门文章

  1. DirectSound 钢琴(1)
  2. 计算机网络 网络层 默认路由
  3. 正交矩阵,(标准)正交基,正交投影,正交分解定理,最佳逼近定理,格拉姆-施密特方法求正交基(手算+MATLAB),QR分解(手算+MATLAB计算、分析)
  4. python中倒背如流_初中生“倒背如流”的3篇文言文,尤其是第3,学渣:so easy!...
  5. 23位子网掩码是多少_23位子网掩码ip范围
  6. c语言课程设计-商场商品信息管理,C语言课程设计商场商品信息管理系统
  7. JAVA面经(SE)
  8. 【模渲大师菜单功能】——13渲染文件
  9. 话剧的一般内容及一份话剧台词
  10. 【AI笔记】刘成林讲座-人工智能发展趋势