1.显示敌机

#-*- coding:utf-8 -*-
import pygame
import time
from pygame.locals import * class HeroPlane(object):
'''飞机类''' def __init__(self,screen_temp):
self.x = 210
self.y = 500
self.screen = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullent_list = [] #存放发射出去的子弹的引用 def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullent in self.bullent_list:
bullent.display()
bullent.move() def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class EnemyPlane(object):
'''敌人的飞机类''' def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.screen = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
# self.bullent_list = [] #存放发射出去的子弹的引用 def display(self):
self.screen.blit(self.image,(self.x,self.y))
# for bullent in self.bullent_list:
# bullent.display()
# bullent.move() def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class Bullent(object):
def __init__(self,screen_temp,x,y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./feiji/bullet.png") def display(self):
self.screen.blit(self.image,(self.x,self.y)) def move(self):
self.y -= 10 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() def main():
#1.创建窗口
screen = pygame.display.set_mode((480,652),0,32) #x=480,y=852 #0,32固定参数 #2.创建一个背景图片
background = pygame.image.load("./feiji/background.png").convert() #3.创建飞机图片
hero = HeroPlane(screen) #4。创建一个敌机
enemy = EnemyPlane(screen) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置
hero.display() enemy.display()
## 检测键盘
key_control(hero)
pygame.display.update()
time.sleep(0.02) #占用cpu太多,降低点 if __name__ == "__main__":
main()

2.飞机左右移动

    

####   飞机不动了

 def move(self):
if self.x <480-50:
self.x += 5
elif self == 480-50:
self.x -= 5
#-*- coding:utf-8 -*-
import pygame
import time
from pygame.locals import * class HeroPlane(object):
'''飞机类''' def __init__(self,screen_temp):
self.x = 210
self.y = 500
self.screen = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullent_list = [] #存放发射出去的子弹的引用 def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullent in self.bullent_list:
bullent.display()
bullent.move() def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class EnemyPlane(object):
'''敌人的飞机类''' def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.screen = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
# self.bullent_list = [] #存放发射出去的子弹的引用
self.direct = "right" def display(self):
self.screen.blit(self.image,(self.x,self.y))
# for bullent in self.bullent_list:
# bullent.display()
# bullent.move() def move(self):
# if self.x <480-50:
# self.x += 5
#elif self == 480-50:
# self.x -= 5
if self.direct == "right":
self.x += 5
elif self.direct == "left":
self.x -= 5 if self.x == 480-50:
self.direct = "left"
elif self.x == 0 :
self.direct = "right" def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class Bullent(object):
def __init__(self,screen_temp,x,y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./feiji/bullet.png") def display(self):
self.screen.blit(self.image,(self.x,self.y)) def move(self):
self.y -= 10 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() def main():
#1.创建窗口
screen = pygame.display.set_mode((480,652),0,32) #x=480,y=852 #0,32固定参数 #2.创建一个背景图片
background = pygame.image.load("./feiji/background.png").convert() #3.创建飞机图片
hero = HeroPlane(screen) #4。创建一个敌机
enemy = EnemyPlane(screen) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
## 检测键盘
pygame.display.update()
key_control(hero)
time.sleep(0.02) #占用cpu太多,降低点 if __name__ == "__main__":
main()

3.代码优化:子弹消失

#-*- coding:utf-8 -*-
import pygame
import time
from pygame.locals import * class HeroPlane(object):
'''飞机类''' def __init__(self,screen_temp):
self.x = 210
self.y = 500
self.screen = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullent_list = [] #存放发射出去的子弹的引用 def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullent in self.bullent_list:
bullent.display()
bullent.move()
if bullent.judge(): #判断子弹是否越界
self.bullent_list.remove(bullent) def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class EnemyPlane(object):
'''敌人的飞机类''' def __init__(self,screen_temp):
self.x = 0
self.y = 0
self.screen = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
# self.bullent_list = [] #存放发射出去的子弹的引用
self.direct = "right" def display(self):
self.screen.blit(self.image,(self.x,self.y))
# for bullent in self.bullent_list:
# bullent.display()
# bullent.move() def move(self):
# if self.x <480-50:
# self.x += 5
#elif self == 480-50:
# self.x -= 5
if self.direct == "right":
self.x += 5
elif self.direct == "left":
self.x -= 5 if self.x == 480-50:
self.direct = "left"
elif self.x == 0 :
self.direct = "right" def fire(self):
self.bullent_list.append(Bullent(self.screen,self.x,self.y)) class Bullent(object):
def __init__(self,screen_temp,x,y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./feiji/bullet.png") def display(self):
self.screen.blit(self.image,(self.x,self.y)) def move(self):
self.y -= 10 def judge(self):
if self.y < 200: #让效果提前显示
return True
else:
return False 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() def main():
#1.创建窗口
screen = pygame.display.set_mode((480,652),0,32) #x=480,y=852 #0,32固定参数 #2.创建一个背景图片
background = pygame.image.load("./feiji/background.png").convert() #3.创建飞机图片
hero = HeroPlane(screen) #4。创建一个敌机
enemy = EnemyPlane(screen) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
## 检测键盘
pygame.display.update()
key_control(hero)
time.sleep(0.02) #占用cpu太多,降低点 if __name__ == "__main__":
main()

bug:不要在遍历list列表的时候删除元素

In [1]: a = [11,22,33,44,55,66]

In [2]: for i in a:
...: if i == 33 or i == 44:
...: a.remove(i)
...: In [3]: a
Out[3]: [11, 22, 44, 55, 66] #44没有删除,漏掉了

  

###   找个空list

In [1]: a = [11,22,33,44,55,66]

In [2]: b = []

In [3]: for i in a:
...: if i == 33 or i == 44: #遍历列表时,把要删除的添加到空列表
...: b.append(i)
...: In [4]: b
Out[4]: [33, 44] In [5]: for i in b: #下次遍历list列表b再删除
...: a.remove(i)
...: In [6]: a
Out[6]: [11, 22, 55, 66]

4

day 6 敌机的更多相关文章

  1. [一位菜鸟的COCOS-2D编程之路]打飞机中机种敌机和战机损毁时的爆炸效果

    1.第一步,添加爆炸动画 //添加玩家飞机飞行动画 id _playerFlyAction; id _playerBlowupAnimation; //战机爆炸动画 id _enemyBlowupAn ...

  2. android飞机游戏敌机移动路径

    基础android的飞机类游戏,与前人一样,由surfaceView绘制游戏画面,另起线程控制绘制时间间隔达到动态效果.这里附上最近自己写的敌机自动飞行路径代码.请大家给点意见. 在敌机管理模块,加入 ...

  3. cocos2d-x(十二)Lua开发飞机大战-7-加入敌机

    Lua本是一门面向过程的解释性语言.但在开发过程中有很多人还是习惯于面向对象编程.在Lua中我们能够使用table模拟类.只是写起来不太爽(特别是在继承一些C++类的时候).通过查看演示样例.发现co ...

  4. Egret学习笔记 (Egret打飞机-9.子弹对敌机和主角的碰撞)

    运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的.. 这一章我们就来处理子弹和飞机的碰撞问题. 我们所有的操作都是基于Main这个容器来做的.所以我就把这个处理放到M ...

  5. Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)

    经过前面几章的准备,我们差不多已经具备了一个基本的框架,这一章我们就开始添砖加瓦了. 敌机定时发射一个子弹,子弹的方向是从上到下,但是发射子弹的代码应该放在哪儿呢? 从面向对象编程的思想来说,子弹是敌 ...

  6. Egret学习笔记 (Egret打飞机-7.实现敌机工厂)

    在游戏过程之,敌机是源源不断的冲屏幕上方往下飞,如果我们每一架敌机都直接new的话,在飞机很多的情况下,也许有性能问题. 就像前面子弹对象池一样,我们也要实现一个飞机对象池,也就是标题说的敌机工厂(之 ...

  7. Egret学习笔记 (Egret打飞机-6.实现敌机飞起来)

    有了子弹,总得有敌人来打吧,不然游戏有啥意思呢?今天我们来实现敌机从屏幕上边往下飞 参考微信打飞机游戏里面,敌机分为3种 1是特小飞机,2是小飞机,还有一种就是大飞机 面向对象编程提倡抽象,实现代码复 ...

  8. [Canvas]空战游戏进阶 增加发射子弹 敌机中弹爆炸功能

    点此下载源码. 图例: 源码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv=" ...

  9. javascript飞机大战-----006创建敌机

    先写一个敌机类 /* 创建敌机: */ function Enemy(blood,speed,imgs){ //敌机left this.left = 0; //敌机top this.top = 0; ...

随机推荐

  1. commons dbcp.jar有什么用

    主流数据库连接池之一(DBCP.c3p0.proxool),单独使用DBCP需要使用commons-dbpc.jar.commons-collections.jar.commons-pool.jar三 ...

  2. phonegap 的指南针 api Compass

    一. Compass 介绍方法参数   1.Compass 也就是,常说的指南针,又叫罗盘 2.方法 compass.getCurrentHeading compass.watchHeading co ...

  3. 【[USACO08FEB]酒店Hotel】

    比较基础的线段树了 我们要维护最大连续子串,这个可以说是一个比较套路的操作了 我们在[SHOI2009]会场预约这道题中已经比较深刻的认识到了这个套路了 对于这道题,我们显然要知道一个区间内最大的全为 ...

  4. VC++ MFC工程中中如何将一个工程的资源(如对话框)复制到另外一个工程

    问题的提出:在工程1中用到的资源,在工程2中已有现成的.即工程1中要用到的对话框和工程2的完全相同,而工程2中对该对话框的布局已设计好.控件变量都绑定好了.但由于该对话框的控件特别多,如果在工程1中再 ...

  5. javaWeb中怎么获取提交表单里面的值

    在javaWeb中如何获得html文件中的表单里面的值? <!DOCTYPE html> <html> <head> <meta charset=" ...

  6. 前端面试题(copy)

    前端开发面试知识点大纲: HTML&CSS: 对Web标准的理解.浏览器内核差异.兼容性.hack.CSS基本功:布局.盒子模型.选择器优先级及使用.HTML5.CSS3.移动端适应. Jav ...

  7. 实现一个shell程序

    实现一个自己的shell程序,这个程序有这些功能:解释执行命令,支持输入输出重定向,支持管道,后台运行 程序.当运行该程序后,它支持以下的命令格式: 1.单个命令,如:ls.2.带l到多个参数的命令, ...

  8. 深入理解bit_or和bit_and,bit_count

    bit_or:二进制数按位或,bit_and:二进制数按位与,bit_count:统计二进制数1个个数 下面以一个例子来说明用法:示例要实现的功能就是计算每月有几天有访问,先把示例摘录在这里.1234 ...

  9. 复习宝典之Spring

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第六章:Spring Spring容器是Spring的核心,一切Spring bean都存储在Spring容器内,并由其通过IoC技术管 ...

  10. Linux-帮助的用法

    Linux帮助使用方法 内部命令:help COMMAND 或 man bash外部命令: (1) COMMAND --help   COMMAND -h --help和-h选项显示用法总结和参数列表 ...