# -*- coding: utf-8 -*-

import pygame
from sys import exit
import random pygame.init()
screen = pygame.display.set_mode((450, 600), 0, 32)
background = pygame.image.load("back.jpg").convert()
plane = pygame.image.load("plane.png").convert_alpha()
pygame.display.set_icon(plane)
pygame.display.set_caption("打飞机")
#pygame.mouse.set_visible(False) class Enemy:
def restart(self):
#重置敌机位置和速度
self.x = random.randint(50, 400)
self.y = random.randint(-200, -50)
self.speed = random.random() + 0.1 def __init__(self):
#初始化
self.restart()
self.image = pygame.image.load('enemy.png').convert_alpha() def move(self):
if self.y < 600:
#向下移动
self.y += 0.3
else:
#重置
self.restart()
#enemy = Enemy()
enemies = []
for i in range(3):
enemies.append(Enemy()) class Bullet:
def __init__(self):
#初始化成员变量,x,y,image
self.x = 0
self.y = -1
self.image = pygame.image.load('bullet.png').convert_alpha()
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 move(self):
#处理子弹的运动
if self.y < 0:
self.active = False
if self.active:
self.y -= 3
#创建子弹的list
bullets = []
#向list中添加5发子弹
for i in range(5):
bullets.append(Bullet())
#子弹总数
count_b = len(bullets)
#即将激活的子弹序号
index_b = 0
#发射子弹的间隔
interval_b = 0 class Plane:
def restart(self):
self.x = 200
self.y = 600 def __init__(self):
self.restart()
self.image = pygame.image.load('plane.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 plane = Plane()
bullet = Bullet()
enemy = Enemy()
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 gameover = False
score = 0
Hscore = [0]
font = pygame.font.SysFont('楷体', 16)
text1 = font.render(u"点击鼠标左键重新开始", 1, (0, 0, 0))
text2 = font.render(u"HScore:0", 1, (0, 0, 0)) while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
#判断在gameover状态下点击了鼠标
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
screen.blit(background, (0,0))
if not gameover:
interval_b -= 1
if interval_b < 0:
bullets[index_b].restart()
interval_b = 100
index_b = (index_b+1) % count_b
for b in bullets:
if b.active:
for e in enemies:
if checkHit(e, b):
score += 100
if checkCrash(e, plane):
gameover = True b.move()
screen.blit(b.image, (b.x-23, b.y))
screen.blit(b.image, (b.x+25, b.y))
screen.blit(b.image, (b.x, b.y))
for e in enemies:
e.move()
screen.blit(e.image, (e.x, e.y))
plane.move()
screen.blit(plane.image, (plane.x, plane.y))
text = font.render("Socre:%d" % score, 1, (0, 0, 0))
else:
#screen.blit(text, (0, 0))
screen.blit(text1,(110,40))
Hscore.append(score)
text2 = font.render(u"HScore:%d" % sorted(Hscore)[-1], 1, (0, 0, 0))
pass
screen.blit(text, (0, 0))
screen.blit(text2, (0, 20))
pygame.display.update()

python打飞机pro版的更多相关文章

  1. tushare使用教程:初始化调用PRO版数据示例

    下面介绍两种常用的数据调取方式: 通过tushare python包 使用http协议直接获取 注:pro版数据接口采用语言无关的http协议实现,但也提供了多种语言的SDK数据获取. 前提条件 1. ...

  2. 张小龙在2017微信公开课PRO版讲了什么(附演讲实录和2016微信数据报告)

    今天2017微信公开课PRO版在广州亚运城综合体育馆举行,这次2017微信公开课大会以“下一站”为主题,而此次的微信公开课的看点大家可能就集中在腾讯公司高级副总裁.微信之父——张小龙的演讲上了!今天中 ...

  3. 笨办法学 Python (第三版)(转载)

    笨办法学 Python (第三版) 原文地址:http://blog.sina.com.cn/s/blog_72b8298001019xg8.html   摘自https://learn-python ...

  4. Python之路,Day4 - Python基础4 (new版)

    Python之路,Day4 - Python基础4 (new版)   本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 ...

  5. python核心编程第二版笔记

    python核心编程第二版笔记由网友提供:open168 python核心编程--笔记(很详细,建议收藏) 解释器options:1.1 –d   提供调试输出1.2 –O   生成优化的字节码(生成 ...

  6. [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source …

    [Python 学习]2.5版yield之学习心得 - limodou的学习记录 - limodou是一个程序员,他关心的焦点是Python, DocBook, Open Source - [Pyth ...

  7. 目前比较流行的Python科学计算发行版

    经常有身边的学友问到用什么Python发行版比较好? 其实目前比较流行的Python科学计算发行版,主要有这么几个: Python(x,y) GUI基于PyQt,曾经是功能最全也是最强大的,而且是Wi ...

  8. 《OpenCV3 计算机视觉--Python语言实现 第二版》源代码及纠错

    1.源代码下载地址 <OpenCV3 计算机视觉--Python语言实现 第二版>由我们翻译,英文书名<Learning OpenCV3 Computer Vision with P ...

  9. 【视频教程】一步步将AppBox升级到Pro版

    本系列教程分为上中下三部分,通过视频的形式讲解如何将基于FineUI(开源版)的AppBox v6.0一步一步升级FineUIPro(基础版). [视频教程]一步步将AppBox升级到Pro版(上)主 ...

随机推荐

  1. myeclipse同时部署两个项目-permgen space

    黑色头发:http://heisetoufa.iteye.com/ 使用myeclipse启动两个SSH2的部署在tomcat6下的项目报出java.lang.OutOfMemoryError: Pe ...

  2. POJ - 3494 Largest Submatrix of All 1’s 单调栈求最大子矩阵

    Largest Submatrix of All 1’s Given a m-by-n (0,1)-matrix, of all its submatrices of all 1’s which is ...

  3. Git小白到老鸟的进阶之路

    点"计算机视觉life"关注,置顶更快接收消息! 小白:师兄,师兄,上次你教我的操作,我傻乎乎的执行了一遍,可是那个Git究竟是什么那? 师兄:小白莫慌,Git就是一种版本控制,小 ...

  4. HDU2819【二分匹配与矩阵的秩】

    题意: 给出一个矩阵问能否实现对角线全部是1,能的话输出路径,不能的话输出-1 思路: 首先根据矩阵的性质,这一定是一个满秩矩阵,所以只根据行或列交换就一定能实现. 所以行和列构成二分图,然后跑一发匈 ...

  5. linux命令行挂载NTFS文件系统的移动硬盘

    环境 ubuntu 12.04 桌面版 由于我的ubuntu 是安装在vmware 上,如果接入移动硬盘后,它没有办法自动识别ntfs 格式的文件系统,导致mount 盘失败 从网上找到一个方法 首先 ...

  6. 跳跃表&hash

    汇编刚学跳跃表,发现跳跃表与hash有着数不清的关系 维基百科: 哈希表(哈希映射)是实现关联数组抽象数据类型的数据结构,该结构可以将键映射到值.哈希表使用哈希函数来计算桶或槽阵列的索引,从中可以找到 ...

  7. 机智云连接ESP8266--远程控制点亮RGB灯

    概述 智能灯,是一个简单常见的智能产品,硬件电路简单,程序本身也不复杂:下面我们使用esp8266开发板和机智云云端,实现如何将一个传统的灯泡,改造成可以远程控制开关的智能灯. 1.准备工作 硬件: ...

  8. Mybatis中分页存在的坑1

    站在巨人的肩膀上 https://www.cnblogs.com/esileme/p/7565184.html 环境:Spring 4.2.1 Mybatis 3.2.8 pagehelper 5.1 ...

  9. POI刷题记录

    POI2007 HNOI2018滚粗后,默默来刷POI 先从2007刷起 bzoj1103[POI2007]大都市meg bzoj1098[POI2007]办公楼biu bzoj1102[POI200 ...

  10. Flask (三) 数据迁移

    数据迁移 安装 pip install flask-migrate 初始化 使用app和db进行migrate对象初始化   from flask_migrate import Migrate mig ...