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

# 定义敌机类
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. EventBus 3.0 的基本使用

    EventBus 3.0 的基本使用 1.什么是EventBus? EventBus 是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间.组件与后台线程间 ...

  2. pycharm全局搜索快捷键无反应

    原因:和搜狗输入法的快捷键冲突

  3. Sublime Text2 使用心得总结

    sublime text2是开发代码编辑的神器 ,编辑器界面优美,操作速度快速.而且Sublime Text2是一款跨平台的编辑器,再也不用为换平台而找不到合适的.熟悉的编辑器担忧了. Sublime ...

  4. python django ORM

    1.在models.py中创创建类 # -*- coding: utf-8 -*- from __future__ import unicode_literals from django.db imp ...

  5. 孙鑫VC视频教程观看记录

    01: 了解了SDK编程,消息队列,消息响应,消息循环,窗口函数等. 02: 可以冒号:父类构造函数和a(1) protected子类可以访问 覆盖:父类子类之间   重载:同一个类中 ::作用域标识 ...

  6. java集合 list与Set、Map区别

      1.List,Set都是继承自Collection接口,Map则不是. 2.List特点:元素有放入顺序,元素可重复 ,Set特点:元素无放入顺序,元素不可重复,重复元素会覆盖掉 ,(注意:元素虽 ...

  7. CentOS7使用firewalld管理防火墙与端口

    firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status fir ...

  8. Python如何让字典保持有序

    问题: Python如何让字典保持有序 ? 解决方案: 使用collections.OrderedDict代替Dict. 验证程序: from collections import OrderedDi ...

  9. linux公社大量免费的在线android资料

    2011年linux数据库的android在线分享 linux公社:开源公社             本文撰写:杨凯专属频道 下载如需密码,详见博客案例:点击我去查看密码 2011年9月12日 21: ...

  10. 借助mkcert签发本地证书

    mkcert 是由 Filippo Valsorda 使用go语言开源的一款零配置搭建本地证书服务的工具,它可以兼容Window, Linux, macOS等多种开发平台,省去了我们自签本地证书的繁琐 ...