小游戏飞机大战的简单代码实现:

# 定义敌机类
class Enemy:
def restart(self): # 重置敌机的位置和速度
self.x = random.randint(50, 400)
self.y = random.randint(-200, -50)
self.speed = random.random()*Level_Simpie_enemy
def __init__(self): # 初始化
self.restart()
self.image = pygame.image.load('enemy.png').convert_alpha()
def move(self):
if self.y < 800:
self.y += self.speed
else: # 重置
self.restart()
# 定义飞机类
class Plane:
def restart(self):
self.x = 200
self.y = 300
def __init__(self):
self.restart()
self.image = pygame.image.load('plant.png').convert_alpha()
def move(self):
x, y = pygame.mouse.get_pos()
x -= self.image.get_width() / 2
y -= self.image.get_height() / 2
self.x = x
self.y = y
# 定义Bullet类
class Bullet:
def __init__(self):
self.x = 0
self.y = -1
self.image = pygame.image.load('dian.jpg').convert_alpha()
self.active = False def move(self):
if self.active:
self.y -= Level_Simpie_bullet
if self.y < 0:
self.active = False def restart(self):
mouseX, mouseY = pygame.mouse.get_pos()
self.x = mouseX - self.image.get_width() / 2
self.y = mouseY - self.image.get_height() / 2
self.active = True
#定义碰撞
def checkHit(enemy, bullet):
if (bullet.x > enemy.x and bullet.x < enemy.x + enemy.image.get_width()) and \
(bullet.y > enemy.y and bullet.y < enemy.y + enemy.image.get_height()):
enemy.restart()
bullet.active = False
return True
return False
def checkCrash(enemy, plane):
if (plane.x + 0.7 * plane.image.get_width() > enemy.x) and (
plane.x + 0.3 * plane.image.get_width() < enemy.x + enemy.image.get_width()) and \
(plane.y + 0.7 * plane.image.get_height() > enemy.y) and (
plane.y + 0.3 * plane.image.get_height() < enemy.y + enemy.image.get_height()):
return True
return False
#总体实现
pygame.init()
screen = pygame.display.set_mode(window_SIZE, 0, 32) # 制作窗口
pygame.display.set_caption('飞机大战') # 窗口名称
background = pygame.image.load('bg.jpg').convert() # 加载背景图
plane = Plane() # 创建飞机对象
enemies = [] # 创建敌机list
for i in range(5):
enemies.append(Enemy())
bullets = [] # 创建子弹list
for i in range(20): # 5发子弹
bullets.append(Bullet())
count_b = len(bullets) # 子弹总数
index_b = 0 # 即将激活的子弹序号
interval_b = 0 # 发射子弹间隔
gameover = False
score = 0
font = pygame.font.Font(None, 32)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if gameover and event.type == pygame.MOUSEBUTTONUP: #鼠标监听
plane.restart()
for e in enemies:
e.restart()
for b in bullets:
b.active = False
score = 0
gameover = False
index_b = 0
interval_b = 0
if event.type == pygame.KEYDOWN: #键盘监听
if event.key == pygame.K_LEFT:
move_x =move_x -10
elif event.key == pygame.K_RIGHT:
move_x =move_x+10
elif event.key == pygame.K_UP:
move_y =move_y+10
elif event.key == pygame.K_DOWN:
move_y = move_y-10
elif event.type == pygame.KEYUP:
move_y=0
move_x=0
screen.blit(background, (0, 0)) # 画背景图像,从最左上角开始即(0,0)
if not gameover:
interval_b -= 2 # !!!发射间隔递减,实际发射间隔受cpu影响
if interval_b < 0: # 间隔小于0时,激发一个子弹
bullets[index_b].restart()
interval_b = 100 # 重置时间间隔
index_b = (index_b + 1) % count_b # 子弹序号周期性递减
for b in bullets: # 绘制激活的子弹
if b.active:
b.move()
screen.blit(b.image, (b.x+move_x, b.y+move_y))
for e in enemies:
if checkHit(e, b):
score += 10
for e in enemies: # 敌机的移动和描述
if checkCrash(e, plane):
gameover = True
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x+move_x, plane.y+move_y)) # 画飞机
text = font.render("Score:%d" % score, 1, (0, 0, 0))
screen.blit(text, (0, 0)) # 在屏幕左上角显示分数
else:
text1 = font.render("Score:%d" % score, 1, (0, 0, 0))
text2 = font.render("click restart", 1, (0, 0, 0))
screen.blit(text1, (190, 400)) # 在屏幕中间显示分数
screen.blit(text2, (170, 420))
pygame.display.update() # 刷新

python飞机大战简单实现的更多相关文章

  1. Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?

    目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...

  2. python飞机大战

    '''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...

  3. python 飞机大战 实例

    飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...

  4. python飞机大战代码

    import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...

  5. 小甲鱼python基础教程飞机大战源码及素材

    百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载

  6. 一、利用Python编写飞机大战游戏-面向对象设计思想

    相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...

  7. Python版飞机大战

    前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...

  8. 500行代码,教你用python写个微信飞机大战

    这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...

  9. python之基础总结(飞机大战)

    一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...

随机推荐

  1. 关于文件下载Header设置

    常见的媒体格式类型如下: text/html : HTML格式text/plain :纯文本格式 text/xml : XML格式image/gif :gif图片格式 image/jpeg :jpg图 ...

  2. CF_Edu.#51_Div.2_1051F_The Shortest Statement

    F. The Shortest Statement time limit per test:4 seconds memory limit per test:256 megabytes input:st ...

  3. Harbor快速搭建企业级Docker仓库

    Github: https://github.com/goharbor/harbor 改端口安装: https://www.cnblogs.com/huangjc/p/6420355.html 相关博 ...

  4. zookeeper 客户端连接报: Will not attempt to authenticate using SASL

    解决方法:我在学习zk的时候,用客户端连接zk,发现接收不到watch通知,并且报 如图所示错误: 后查看服务没问题:图示 后查看防火墙状态,关闭防火墙 连接后正常: 如果查看防火墙状态是dead,s ...

  5. APP内计费规范出台 手游乱收费现象能被遏制?

    手游乱收费现象能被遏制?" title="APP内计费规范出台 手游乱收费现象能被遏制?"> 在一个混乱.无秩序的环境中竞争,虽然有可能不择手段地获取更多的利益,但 ...

  6. 从846家初创倒下 看A轮融资后的悬崖

    看A轮融资后的悬崖" title="从846家初创倒下 看A轮融资后的悬崖"> 相比往年,今年的寒冷冬天来得更早.在互联网行业,今年的"大雪"更 ...

  7. Android中Intent的各种常见作用。

    Android开发之Intent.Action  1 Intent.ACTION_MAIN String: android.intent.action.MAIN 标识Activity为一个程序的开始. ...

  8. coreseek 在gcc 4.9+ 上编译不通过 [sphinxexpr.o] Error 1 错误解决方案

    这几天玩hhvm,把gcc环境都装到4.9了,然后编译coreseek的时候就出问题,google一大圈,貌似捕风捉影看到一些信息说是gcc4.7+的c++作用域必须用this->去引用,这里整 ...

  9. IP不是万能药 为何有蜘蛛侠等大片的索尼要放弃电影

    ​ 为何有蜘蛛侠等大片的索尼要放弃电影"> 近年来,国内狂炒"IP"这一概念,比如动漫.网络文学.小说.游戏等,甚至围绕IP制造出内容矩阵.从一个IP延伸至多个领域 ...

  10. Coding Interviews 20 包含min函数的栈

    题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 思路 We need another data structure to sotre ...