# -*- 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. linux 下 mac 地址如何查询

    cat     /sys/class/net/eth0/address

  2. new Date().getTime()和System.currentTimeMillis()对比

    我在工作中,看项目组的代码时,在代码中会发现一个有趣的现象,有使用new Date().getTime()来获取时间戳的, 也有使用System.currentTimeMillis()来获取时间戳的, ...

  3. 计算机图形学----基于3D图形开发技术 (韩正贤 著)

    第1章 游戏模型 第2章 顶点处理机制 第3章 光栅化操作 第4章 片元处理和输出合并 第5章 光照和着色 第6章 参数曲线和表面 第7章 着色器模型 第8章 图像纹理 第9章 凹凸贴图 第10章 高 ...

  4. Java小代码

    1. public class test1 {public static void main(String[] args) { Person P = new Person("gdsgds&q ...

  5. ActiveMQ (二)—发布订阅模式

    ActiveMQ的另一种模式就SUB/HUB即发布订阅模式,是SUB/hub就是一拖N的USB分线器的意思.意思就是一个来源分到N个出口.还是上节的例子,当一个订单产生后,后台N个系统需要联动,但有一 ...

  6. cut字符串截取

    cut字符串截取 -d 按字节截取 [root@slave elk]# ll total 0 drwxr-xr-x. 6 root root 194 Jan 24 16:15 bigdesk 截取前2 ...

  7. Logback动态修改日志级别

    https://blog.csdn.net/totally123/article/details/78931287

  8. Visual Studio安装Visual Assist的办法(兼容VS2010至VS2017)

    Visual Assist可以说是一个码代码的高效帮手,有了它,敲起代码速度杠杠的,但是有时候安装破解老是出问题,这一次,我自己尝试了几次,跟着网上的教程做了做,改了改,结合之前安装的经验,最后总算安 ...

  9. 如何快速开发html5跨平台K12/幼儿交互课件、动画课件、交互游戏

    flash交互课件能生动表达教学内容,也深受广大教育工作者的喜爱,但是目前flash课件只能在pc电脑平台上进行展示,随着移动网络的发展,越来越多的课件产品需要移殖到各种移动平台(手机,pad,智能电 ...

  10. python 云打码 http接口

    import http.client, mimetypes, urllib, json, time, requests ######################################## ...