# -*- 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("打飞机") 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 = []
font = pygame.font.SysFont('楷体', 16)
text1 = font.render(u"点击鼠标左键重新开始", 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))
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))
screen.blit(text, (0, 0))
else:
screen.blit(text, (0, 0))
screen.blit(text1,(100,32))
Hscore.append(score)
text2 = font.render(u"HScore:%d" % sorted(Hscore)[-1], 1, (0, 0, 0))
screen.blit(text2, (250, 0))
pass
pygame.display.update()

python实现微信打飞机游戏(by crossin)的更多相关文章

  1. python实现微信打飞机游戏

    环境:Ubuntu 16.04 LTS Python 2.7.11 +  Pygame + Pycharm 代码: # -*- coding: UTF-8 -*- import pygame, ran ...

  2. pygame开发PC端微信打飞机游戏

    pygame开发PC端微信打飞机游戏 一.项目简介 1. 介绍 本项目类似曾经火爆的微信打飞机游戏.游戏将使用Python语言开发,主要用到pygame的API.游戏最终将会以python源文件gam ...

  3. Pygame制作微信打飞机游戏PC版

    使用Pygame制作微信打飞机游戏PC版 转至:http://www.cnblogs.com/dukeleo/p/3339780.html   前一阵子看了一篇文章:青少年如何使用Python开始游戏 ...

  4. 实例源码--IOS高仿微信打飞机游戏(完整功能)

    下载源码 技术要点: 1. IOS游戏开发基础框架 2. 高仿打飞机游戏 3. 游戏背景音频技术 4.源码详细的中文注释 ……. 详细介绍: 1. IOS游戏开发基础框架 此套源码为涉及IOS游戏开发 ...

  5. 利用python实现微信小程序游戏跳一跳详细教程

    利用python实现微信小程序游戏跳一跳详细教程 1 先安装python 然后再安装pip <a href="http://newmiracle.cn/wp-content/uploa ...

  6. 使用Pygame制作微信打飞机游戏PC版

    前一阵子看了一篇文章:青少年如何使用Python开始游戏开发 .看完照葫芦画瓢写了一个,觉得挺好玩儿,相当于简单学了下Pygame库.这篇文章是个12岁小孩儿写的,国外小孩儿真心NB,想我12岁的时候 ...

  7. <Win32_20>纯c语言版的打飞机游戏出炉了^_^

    经过昨天的苦战,终于完成了纯C版的打飞机游戏——使用微信打飞机游戏的素材,不过玩法有些不同,下面会有详述 一.概述游戏的玩法.实现效果 1. 游戏第一步,简单判断一下,给你一个准备的时间: 2.选择& ...

  8. python 之路,200行Python代码写了个打飞机游戏!

    早就知道pygame模块,就是没怎么深入研究过,恰逢这周未没约到妹子,只能自己在家玩自己啦,一时兴起,花了几个小时写了个打飞机程序. 很有意思,跟大家分享下. 先看一下项目结构 "" ...

  9. Android原生游戏开发:使用JustWeEngine开发微信打飞机

    使用JustWeEngine开发微信打飞机: 作者博客: 博客园 引擎地址:JustWeEngine 示例代码:EngineDemo JustWeEngine? JustWeEngine是托管在Git ...

随机推荐

  1. idea2018.2.5版本使用之背景色

    idea 背景色: 写代码区换眼色豆沙色:

  2. nohup不输出日志信息的方法及linux重定向

    最近使用nohup创建了一个后台进程,默认日志输出到了nohup.out文件中,程序跑起来也就没再管,过了大约一周,发现硬盘空间不够了,于是查找原因,发现这个nohup.out文件已经到了70G了,导 ...

  3. docker的安装与卸载

    卸载老版本docker sudo apt-get remove docker docker-engine docker.io /var/lib/docker/目录下存放着 images, contai ...

  4. 【Linux学习】Linux文件系统3—文件操作命令

    Linux文件系统3-文件操作命令 Linux文件操作命令主要有: cd:    改变目录位置 pwd:  显示当前目录的绝对路径 ls:    显示文件名称.属性等 -a 列出全部文件 -l  列出 ...

  5. 技术胖Flutter第四季-20导航的参数传递和接受-1

    技术胖Flutter第四季-20导航的参数传递和接受-1 视频地址:https://www.bilibili.com/video/av35800108/?p=21 先安装一个新的插件: Awesome ...

  6. 通过HTTP请求WEBAPI的方式

    平时工作中长期需要用到通过HTTP调用API进行数据获取以及文件上传下载(C#,JAVA...都会用到).这里获取的是包括网页的所有信息.如果单纯需要某些数据内容.可以自己构造函数甄别抠除出来!一般的 ...

  7. 基于thinkphp5的Excel上传

    涉及知识点: thinkphp5.0: excel上传: mysql建立新表(基本的create语句): mysql ignore(避免重复插入): 主要功能: 通过在视图中上传excel文件,在my ...

  8. win7 mongod不是内部命令

    1.下载MongoDB 1.1 MongoDB下载 1.2 选择Server下面的 Community 2.安装MongoDB 2.1 注意事项:一直下一步就行了,但是遇到下面这个界面,注意一定要去掉 ...

  9. SQL基础培训实战教程[全套]

    学习简介:林枫山根据网上搜索资料进行参考,编写制作的SQL Server实操学习教程,欢迎下载学习. 下载链接目录如下: 进度0-SQL基础语法    下载学习文档 进度1-建数据表-美化版-2018 ...

  10. html中id name class的区别(转)

    HTML 中 id与name 区别 一个name可以同时对应多个控件,比如checkbox和radio 而id必须是全文档中唯一的 id的用途 1) id是HTML元素的Identity,主要是在客户 ...