python飞机大战简单实现
小游戏飞机大战的简单代码实现:
# 定义敌机类
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飞机大战简单实现的更多相关文章
- Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?
目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...
- python飞机大战
'''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...
- python 飞机大战 实例
飞机大战 #coding=utf-8 import pygame from pygame.locals import * import time import random class Base(ob ...
- python飞机大战代码
import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...
- 小甲鱼python基础教程飞机大战源码及素材
百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载
- 一、利用Python编写飞机大战游戏-面向对象设计思想
相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...
- Python版飞机大战
前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...
- 500行代码,教你用python写个微信飞机大战
这几天在重温微信小游戏的飞机大战,玩着玩着就在思考人生了,这飞机大战怎么就可以做的那么好,操作简单,简单上手. 帮助蹲厕族.YP族.饭圈女孩在无聊之余可以有一样东西让他们振作起来!让他们的左手 / 右 ...
- python之基础总结(飞机大战)
一.学习python有一段时间了,总体上手还是挺好的,但是有些东西还是和Java存在着一定的区别,这里主要是通过学习,然后自己去编写一个案例.从中学习到的一些东西,这里分享出来,如果存在不正确的地方还 ...
随机推荐
- Java IO: FileInputStream
原文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197@gmail.com) FileInputStream可以以字节流的形式读取文件内容.FileInputStream ...
- 吴裕雄--天生自然HTML学习笔记:HTML 框架
通过使用框架,你可以在同一个浏览器窗口中显示不止一个页面. iframe语法: <iframe src="URL"></iframe> 该URL指向不同的网 ...
- 关于JavaScript中的==与!的转换问题
最近遇到了一道很有趣的JavaScript试题,感觉很有趣.记录一下免得以后面试遇到 题目是: console.log([]==![],{}==!{},[]==!{},{}==![]) 这道题考察的主 ...
- django反向解析和正向解析
Django的正向解析和反向解析 先创建一个视图界面 urls.py index.html index页面加载的效果 正向解析 test/?result=1 所谓正向解析就是直接在这里写地址 向url ...
- 2018年宜賓美酒文化節浮空投影舞美特效 / 2018 Yibing Wine Festival Visual Effect Projection
客户 Client:五粮液集团 硬件 Hardware:PC,巴可投影机30,000流明*2 Barco projector 30,000 lumen*2 软件 Software:Resolume, ...
- 腾讯入股Snap,能救“阅后即焚”的命吗?
互联网社交的强大包容性,让各种社交形式都能有着较多的受众群体.普适性极广的QQ.微信."脸谱":专攻陌生人社交的陌陌:让人们发布意见的微博--当然也少不了"阅 ...
- [置顶]
Python 使用itchat 对微信好友数据进行简单分析
人生苦短,我用Python! Python 热度一直很高,我感觉这就是得益于拥有大量的包资源,极大的方便了开发人员的需求. 最近在一个微信公众号上看到一个调用微信 API 可以对微信好友进行简单数据分 ...
- win10安装LoadRunner时,安装.net framwork组件报0x800F081F错误 解决办法
一.报错原因 0x800F081F错误大多数是在安装软件时,系统无法联网自动下载安装. 经过各种排查及搜索解决方案,总结原因无非以下三种: 1.windows update被禁用. 2.电脑没有.ne ...
- SpringBoot图文教程10—模板导出|百万数据Excel导出|图片导出「easypoi」
有天上飞的概念,就要有落地的实现 概念十遍不如代码一遍,朋友,希望你把文中所有的代码案例都敲一遍 先赞后看,养成习惯 SpringBoot 图文教程系列文章目录 SpringBoot图文教程1「概念+ ...
- 20170813-CSRF 跨站请求伪造
CSRF CSRF是Cross Site Request Forgery的缩写,翻译过来就是跨站请求伪造. 跨站:顾名思义,就是从一个网站到另一个网站. 请求:即HTTP请求. 伪造:在这里可以理解为 ...