飞机大战

#coding=utf-8
import pygame
from pygame.locals import *
import time
import random
class Base(object):
def __init__(self,x,y,screen,image_name):
self.x=x
self.y=y
self.screen=screen
self.image=pygame.image.load(image_name).convert()
class BaseBullet(Base):
def __init__(self,x,y,screen,image_name):
Base.__init__(self,x,y,screen,image_name)
def display(self):
self.screen.blit(self.image,(self.x,self.y))
class Bullet(BaseBullet):
def __init__(self,x,y,screen):
BaseBullet.__init__(self,x+40, y-20, screen,"C:/Users/lenovo/Desktop/feiji/bullet-3.gif")
def move(self):
self.y-=10
def judge(self):
if self.y<0:
return True
else:
return False
#谁发射 谁创建
class EnemyBullet(BaseBullet):
def __init__(self,x,y,screen):
BaseBullet.__init__(self,x+25, y+40, screen,"C:/Users/lenovo/Desktop/feiji/bullet1.png")
def move(self):
self.y+=5
def judge(self):
if self.y>600:
return True
else:
return False
class BasePlane(Base):
def __init__(self,x,y,screen,image_name):
Base.__init__(self,x,y,screen,image_name)
self.bullet_list=[]#存储子弹
def display(self):
#更新飞机的位置
self.screen.blit(self.image,(self.x,self.y))
#存放需要删除的对象信息
needDelItemList=[]
for i in self.bullet_list:
if i.judge():
needDelItemList.append(i)
for i in needDelItemList:
self.bullet_list.remove(i)
#更新及这架飞机发射出的所有子弹的位置
#子弹移动了 判断每一颗子弹和子弹的位置
for bullet in self.bullet_list:
bullet.display()
bullet.move()
class HeroPlane(BasePlane):
def __init__(self,screen):#默认有照片 默认有位置
BasePlane.__init__(self,200,500,screen,"C:/Users/lenovo/Desktop/feiji/hero.gif")
self.hit=False #表示是否要爆炸
self.bomb_list=[]#用来存储爆炸时需要的图片
self.__create_images()
self.image_num = 0
# 用来记录while True的次数,当次数达到一定值时才显示一张爆炸的图,然后清空,,当这个次数再次达到时,再显示下一个爆炸效果的图片
self.image_index = 0#用来记录当前要显示的爆炸效果的图片的序号
def __create_images(self):
self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n1.png"))
self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n2.png"))
self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/feiji/hero_blowup_n3.png"))
self.bomb_list.append(pygame.image.load("C:/Users/lenovo/Desktop/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 == 5:
self.image_num=0
self.image_index+=1
if self.image_index>3:
time.sleep(0.1)
print("failure")
exit()
else:
self.screen.blit(self.image,(self.x, self.y))
#存放需要删除的对象信息
needDelItemList=[]
for i in self.bullet_list:
if i.judge():
needDelItemList.append(i)
for i in needDelItemList:
self.bullet_list.remove(i)
#更新及这架飞机发射出的所有子弹的位置
#子弹移动了 判断每一颗子弹和子弹的位置
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def bomb(self):
self.hit = True def moveLeft(self):
self.x-=20
def moveRight(self):
self.x+=20
def moveUp(self):
self.y-=20
def moveDown(self):
self.y+=20
def fire(self):
newBullet=Bullet(self.x,self.y,self.screen)
self.bullet_list.append(newBullet)
class EnemyPlane(BasePlane):
def __init__(self,screen):
BasePlane.__init__(self,0,0,screen,"C:/Users/lenovo/Desktop/feiji/enemy0.png")
self.direction="right"#用来存储飞机默认的显示方向
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 fire(self):
random_num=random.randint(1,200)
if random_num==7 or random_num==20:
self.bullet_list.append(EnemyBullet(self.x, self.y,self.screen))
def key_control(heroPlane):
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key==K_LEFT:
print('left')
heroPlane.moveLeft()
elif event.key == K_d or event.key==K_RIGHT:
print('right')
heroPlane.moveRight()
elif event.key == K_w or event.key==K_UP:
print('up')
heroPlane.moveUp()
elif event.key ==K_s or event.key==K_DOWN:
print('down')
heroPlane.moveDown()
elif event.key == K_SPACE:
heroPlane.fire()
elif event.key == K_b:
print('b')
heroPlane.bomb() if __name__=="__main__":
screen=pygame.display.set_mode((480,600),0, 32)
background=pygame.image.load("C:/Users/lenovo/Desktop/feiji/background.png").convert()
heroPlane=HeroPlane(screen)
enemy=EnemyPlane(screen)
while True:
screen.blit(background,(0,0))
heroPlane.display()
enemy.display()#让敌机显示
enemy.move()#调用敌机的move方法
enemy.fire()#让敌机开火
pygame.display.update()
key_control(heroPlane)
time.sleep(0.01)

for循环的坑

(防止列表循环的时候删自己列表元素出现bug)

不能边遍历边删

是指不能删自己循环的列表,可以删其他人

for 循环遍历一个列表的时候删除一个元素是有坑的

刚好指向下一个元素

11 22 33 删除了 33 ,44刚好进一位(补上),所以44没有删掉

把谁要删的记下来

a=[11,22,33,44,55]
b=[]
for i in a:
if i=33 or i=44:
b.append(i)
for i in b:
a.remove(i)
print(a)

python 飞机大战 实例的更多相关文章

  1. Python飞机大战实例有感——pygame如何实现“切歌”以及多曲重奏?

    目录 pygame如何实现"切歌"以及多曲重奏? 一.pygame实现切歌 初始化路径 尝试一 尝试二 尝试三 成功 总结 二.如何在python多线程顺序执行的情况下实现音乐和音 ...

  2. python飞机大战

    '''新手刚学python,仿着老师敲的代码.1.敌方飞机只能左右徘徊(不会往下跑)并且不会发射子弹.2.正在研究怎么写计分.3.也参考了不少大佬的代码,但也仅仅只是参考了.加油!''' import ...

  3. python飞机大战简单实现

    小游戏飞机大战的简单代码实现: # 定义敌机类 class Enemy: def restart(self): # 重置敌机的位置和速度 self.x = random.randint(50, 400 ...

  4. python飞机大战代码

    import pygame from pygame.locals import * from pygame.sprite import Sprite import random import time ...

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

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

  6. Python版飞机大战

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

  7. js实例--飞机大战

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title> ...

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

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

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

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

随机推荐

  1. ROS: Ubuntu16.04安装ROS-kinetic

    参考连接:SLAM: Ubuntu14.04_Kylin安装ROS-Indigo第一步: 软件源配置 1. 增加下载源(增加ubuntu版的ros数据仓库,即下载源)(通用指令适合任何版本的ros) ...

  2. 【sqli-labs】 less25a GET- Blind based -All you OR&AND belong to us -Intiger based(GET型基于盲注的去除了or和and的整型注入)

    因为过滤是针对输入的字符串进行的过滤,所以如果过滤了or and的话,提交id=1和id=and1结果应该相同 http://localhost/sqli-labs-master/Less-25a/? ...

  3. servlet向浏览器输出信息

    package com.aaa.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebS ...

  4. java8方式日期比较

    static ZoneId ZONEID_BJ = ZoneId.of("GMT+08:00"); private boolean sameDate(Date d1, Date d ...

  5. 简单说基于JWT和appkey、sercurtyKey的SSO、身份认证方案

    环境介绍, 一个大的系统由多个子系统组成.典型地,假设有一个平台,其上接入了多个应用.则有几个常见的问题需要处理, 1.SSO(包括单个应用退出时,需要处理为整个系统退出): 2.平台跳转到应用.及应 ...

  6. 继续聊WPF——设置网格控件列标题的样式

    我很奇怪的是,微软那厮是怎么搞的,Blend里面居然不能编辑GridView的样式,十万般无奈之下,只好手写XAML来处理了. 要想知道一个控件的样式是如何设置,看控件类的定义很重要,我们来看看Gri ...

  7. JAVA关键技术

    通用技术方面 MVC 1)概念 MVC是一个架构模式,它分离了表现与交互.它被分为三个核心部件:模型-model.视图-view.控制器-controller 2)工作原理 所有的终端用户请求被发送到 ...

  8. 2.3 SVN在myeclipse中的使用

    一.将svn插件文件夹复制到myeclipse的dropins目录下,并重启myeclipse 二.从SVN中检查项目到myeclipse  2.打开myeclipse,点击window的show v ...

  9. cxdbtreelist的处理点滴

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuAAAAE8CAIAAAAOqJOXAAAgAElEQVR4nOy9eXAcV37n+bwzf21sbO ...

  10. DJANGO里让用户自助修改邮箱地址

    因为在部署过程中会涉及用户邮件发送,如果有的同事不愿意收到太多邮件,则可以自己定义为不存在的邮箱. 我们在注册的时候,也不会写用户邮箱地址,那么他们也可以在这里自己更改. changeemail.ht ...