# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import time '''
说明
1.按下b键,让玩家飞机爆炸
2.爆炸效果的原理是:换图片
''' class Hero(object):
def __init__(self, screen_temp):
self.x = 210
self.y = 700
self.image = pygame.image.load("./feiji/hero1.png")
self.screen = screen_temp
self.bullet_list = []#用来存储子弹对象的引用 #爆炸效果用的如下属性
self.hit = False #表示是否要爆炸
self.bomb_list = [] #用来存储爆炸时需要的图片
self.__crate_images() #调用这个方法向bomb_list中添加图片
self.image_num = 0#用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号 def __crate_images(self):
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("./feiji/hero_blowup_n4.png")) def display(self):
"""显示玩家的飞机"""
#如果被击中,就显示爆炸效果,否则显示普通的飞机效果
if self.hit == True:
self.screen.blit(self.bomb_list[self.image_index], (self.x, self.y))
self.image_num+=1
if self.image_num == 7:
self.image_num=0
self.image_index+=1
if self.image_index>3:
time.sleep(1)
exit()#调用exit让游戏退出
#self.image_index = 0
else:
self.screen.blit(self.image,(self.x, self.y)) #不管玩家飞机是否被击中,都要显示发射出去的子弹
for bullet in self.bullet_list:
bullet.display()
bullet.move() def move_left(self):
self.x -= 8 def move_right(self):
self.x += 8 def fire(self):
"""通过创建一个子弹对象,完成发射子弹"""
print("-----1----")
bullet = Bullet(self.screen, self.x, self.y)#创建一个子弹对象
self.bullet_list.append(bullet) def bomb(self):
self.hit = True class Bullet(object):
def __init__(self, screen_temp, x_temp, y_temp):
self.x = x_temp+40
self.y = y_temp-20
self.image = pygame.image.load("./feiji/bullet.png")
self.screen = screen_temp def display(self):
self.screen.blit(self.image, (self.x, self.y)) def move(self):
self.y -= 4 class EnemyPlane(object):
def __init__(self, screen_temp):
self.x = 0
self.y = 0
self.image = pygame.image.load("./feiji/enemy0.png")
self.screen = screen_temp
#self.bullet_list = []#用来存储子弹对象的引用
self.direction = "right"#用来设置这个飞机默认的移动方向 def display(self):
"""显示敌人的飞机"""
self.screen.blit(self.image,(self.x, self.y)) def move(self): if self.direction == "right":
self.x+=2
elif self.direction == "left":
self.x-=2 if self.x>480-50:
self.direction="left"
elif self.x<0:
self.direction="right" def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get(): #判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left() #检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right() #检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero_temp.fire()
elif event.key == K_b:
print('b')
hero_temp.bomb() def main():
screen = pygame.display.set_mode((480,852),0,32)
background = pygame.image.load("./feiji/background.png") #创建玩家飞机
hero = Hero(screen) #创建敌机
enemy = EnemyPlane(screen) while True:
screen.blit(background,(0,0))
hero.display()
enemy.display()
enemy.move()
pygame.display.update()
key_control(hero) if __name__ == "__main__":
main()

python大法好——飞机大战完整吧的更多相关文章

  1. python大法好——飞机大战

    import pygame from pygame.locals import * import time def key_control(hero_temp): # 获取事件,比如按键等 for e ...

  2. Python+Pygame开发太空大战/飞机大战完整游戏项目(附源代码)

    项目名称:太空大战 开发环境:Python3.6.4 第三方库:Pygame1.9.6 代码编辑器:Sublime Text 先来看一下游戏画面吧!  游戏画面动态且丰富哦!   需求分析 利用Pyt ...

  3. 小甲鱼python基础教程飞机大战源码及素材

    百度了半天小甲鱼python飞机大战的源码和素材,搜出一堆不知道是什么玩意儿的玩意儿. 最终还是自己对着视频一行行代码敲出来. 需要的同学点下面的链接自取. 下载

  4. 用Python做一个飞机大战游戏

    基于pygame的一款小游戏 这是我上半年做的一款小游戏,但是一直忘记了,现在才上传代码. github项目地址:StarMan 代码基于pygame,Python版本3.5.2运行正常. 游戏很简单 ...

  5. javascript 飞机大战完整代码

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. Python小游戏之 - 飞机大战美女 !

    用Python写的"飞机大战美女"小游戏 源代码如下: # coding=utf-8 import os import random import pygame # 用一个常量来存 ...

  7. Python版飞机大战

    前面学了java用java写了飞机大战这次学完python基础后写了个python版的飞机大战,有兴趣的可以看下. 父类是飞行物类是所有对象的父类,setting里面是需要加载的图片,你可以换称自己的 ...

  8. Python小游戏之 - 飞机大战 !

    用Python写的"飞机大战"小游戏 源代码如下: # coding=utf-8 import random import os import pygame # 用一个常量来存储屏 ...

  9. 一、利用Python编写飞机大战游戏-面向对象设计思想

    相信大家看到过网上很多关于飞机大战的项目,但是对其中的模块方法,以及使用和游戏工作原理都不了解,看的也是一脸懵逼,根本看不下去.下面我做个详细讲解,在做此游戏需要用到pygame模块,所以这一章先进行 ...

随机推荐

  1. Google - Find minimum number of coins that make a given value

    Given a value V, if we want to make change for V cents, and we have infinite supply of each of C = { ...

  2. sqlite 使用 cte 及 递归的实现示例

    1.多级 cte 查询示例. with cte as ( select pageid from cm_bookpage ) , cte2 as ( as content from cte ) sele ...

  3. C++——list中erase和remove的区别

    1.之前在做相关的操作的时候,涉及到清除list相关的元素,因此会用到erase和remove,那么二者有什么区别呢? 从官方文档中,我们可以获取以下信息 erase : 说明:Removes fro ...

  4. 使用python调用其他脚本

    cmd = '<command line string>' print(cmd) p = subprocess.Popen(args=cmd, shell=True, stdout=sub ...

  5. HBASE基础知识总结

    HBASE基础知识总结 一,概要说明 文章首先回顾HBase 的数据模型和数据层级结构,对数据的每个层级的作用和架构进行了详细阐述:随后介绍了数据写入和读取的详细流程.先把架构图和流程图来坐镇. 架构 ...

  6. [ZZ] 边缘检测 梯度与Roberts、Prewitt、Sobel、Lapacian算子

    http://blog.csdn.net/swj110119/article/details/51777422 一.学习心得: 学习图像处理的过程中,刚开始遇到图像梯度和一些算子的概念,这两者到底是什 ...

  7. MySql 的SQL执行计划查看,判断是否走索引

    在select窗口中,执行以下语句: set profiling =1; -- 打开profile分析工具show variables like '%profil%'; -- 查看是否生效show p ...

  8. 图片支持get请求访问

    BufferedInputStream in = new BufferedInputStream(doc2.getContent());//读取文件到输入流 OutputStream out = re ...

  9. jumpservice一步一步安装

    一步一步安装 (CentOS) 本文档旨在帮助用户了解各组件之间的关系, 生产环境部署建议参考 进阶安装文档 云服务器快速部署参考 极速安装 安装过程中遇到问题可参考 安装过程中常见的问题 测试推荐环 ...

  10. IIS Express 域认证问题(https://stackoverflow.com/questions/4762538/iis-express-windows-authentication)

    option-1: edit \My Documents\IISExpress\config\applicationhost.config file and enable windowsAuthent ...