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

# 定义敌机类
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. labview相关内容

    索引数组的用法:https://jingyan.baidu.com/album/90808022e6d0f7fd91c80fd2.html?picindex=1 定时顺序结构用法:http://zon ...

  2. SpringMVC学习笔记三:Controller的返回值

    springMVC的返回值有ModelAndView,String,void,Object类型 项目目录树: 该项目是在前面项目的基础上修改的,这里的pom.xml文件需要加入使用到的包,应为@Res ...

  3. 转: zabbix3.2.1安装graphtrees插件

    转自 : http://blog.csdn.net/liang_baikai/article/details/53542317 graphtree介绍 由于zabbix的图像显示一块不太友好,图像没法 ...

  4. unicode编码与解码

    unicode编码与解码,代码如下 package com.fenqiguanjia.api.services; /** * Created by daixianjun on 2017/9/3. */ ...

  5. 吴裕雄--天生自然KITTEN编程:翻译机

  6. Python Mock 的入门

    Mock是什么 Mock这个词在英语中有模拟的这个意思,因此我们可以猜测出这个库的主要功能是模拟一些东西.准确的说,Mock是Python中一个用于支持单元测试的库,它的主要功能是使用mock对象替代 ...

  7. Welcome to Giyber Blog - LC的博客

    "You can be the best! " 一切才刚开始 "不知道行不行,试试吧."抱着这样的理由,一个小白的成长记录,由此开始. 在 Mr.锤 的&quo ...

  8. Vue数据绑定(一)

    Contents Vue作为当下炙手可热的前端三大框架之一,一直都想深入研究一下其内部的实现原理,去学习MVVM模式的精髓.如果说MVVM是当下最流行的图形用户界面开发模式,那么数据绑定则是这一模式的 ...

  9. 网络编程之C10K

    网络编程之C10K 虽然在过去的十几年里C10K问题已经可以很好的解决,但学习网络编程时研究C10K问题仍然价值巨大,因为技术的发展都是有规律和线索可循的,了解C10K问题及其解决思路,通过举一反三, ...

  10. Hexo+github如何搭建博客

    前言 博客有第三方平台,也可以自建,比较早的有博客园.CSDN,近几年新兴的也比较多诸如:WordPress.segmentFault.简书.掘金.知乎专栏.Github Page 等等. 这次我要说 ...