飞机大战

#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. webpack学习(三)

    前篇:webpack学习(二) jquery不需要在项目中自己下载,而是作为一个模块引入.jquery的存放路径是在 node_modules目录下.1.首先给项目安装jquery,npm insta ...

  2. " \t\r\n\f"是什么意思

    空格字符 \t 制表符 \r 回车符 \n 换行符 \f 换页符

  3. count(*)实现原理+两阶段提交总结

    count(*)实现原理 不同引擎的实现: MyISAM引擎把表的总行数存在了磁盘上,执行COUNT(*)就会直接返回,效率很高: InnoDB在count(*)时,需要把数据一行一行的从引擎里面取出 ...

  4. linux系统下安装memcached

    检查libevent 首先检查系统中是否安装了libevent rpm -qa|grep libevent 如果安装了则查看libevent的安装路径,后续安装时需要用到 rpm -ql libeve ...

  5. 使用css设置border从中间向两边的颜色渐进效果

    1.效果图,设置目录的右框线渐进效果 2.代码 .rightCont>div:nth-child(1){ width: 370px; height: 100%; border-right: 2p ...

  6. SQL第二节课

    SQL练习题 一.            设有一数据库,包括四个表:学生表(Student).课程表(Course).成绩表(Score)以及教师信息表(Teacher).四个表的结构分别如表1-1的 ...

  7. 国庆day2

    a[问题描述]你是能看到第一题的 friends呢.—— hja世界上没有什么比卖的这 贵弹丸三还令人绝望事了,所以便么一道题.定义

  8. Ubuntu Server下docker实战 01: 安装docker

    本系列文章主旨在于使用docker来搭建实际可用的基础服务,具体到每一步的操作和设置. 关于docker的原理.前世今生的内容,园子里已经有太多的文章了,此处就不再赘述. 要使用docker,当然第一 ...

  9. 继续聊WPF——Expander控件(2)

    <Window x:Class="Expander_Sample2.Window1" xmlns="http://schemas.microsoft.com/win ...

  10. react 点击事件+父子传值

    接下来要做的效果是,在父组件添加两个按钮,点击后改变父组件传过去的值 父组件 import React, { Component } from 'react'; import Test from '. ...