day 5 飞机发射子弹 难点??
1.效果图

2.飞机发出子弹
#-*- 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() def move_left(self):
self.x -= 5 def move_right(self):
self.x += 5 def fire(self):
self.bullent_list.append(Bullent(self.screen)) class Bullent(object):
def __init__(self,screen_temp):
self.x = 0
self.y = 0
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 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) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置 hero.display()
#x += 1
#y -= 1 ## 检测键盘
key_control(hero) pygame.display.update()
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() 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 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) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置 hero.display()
#x += 1
#y -= 1 ## 检测键盘
key_control(hero) pygame.display.update()
time.sleep(0.02) #占用cpu太多,降低点 if __name__ == "__main__":
main()
4.让子弹动起来

#-*- 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 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 -= 5 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) ##3. 把背景图片放到窗口中显示
while True:
screen.blit(background,(0,0))#图片顶点在窗口的位置
hero.display()
## 检测键盘
key_control(hero)
pygame.display.update()
time.sleep(0.02) #占用cpu太多,降低点 if __name__ == "__main__":
main()
day 5 飞机发射子弹 难点??的更多相关文章
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第4讲(创建敌方飞机、敌方飞机发射子弹、玩家子弹击中敌方小飞机,小飞机死亡)
一.创建敌方飞机 1.思考创建思路: 创建敌方飞机思路与创建玩家飞机思路一样: (1)思考敌方飞机具备什么属性: 敌方飞机的图片.坐标.飞行速度.状态(是否被击中) 设置小飞机被击中时消失时间.飞机可 ...
- Egret学习笔记 (Egret打飞机-8.敌机和主角飞机发射子弹)
经过前面几章的准备,我们差不多已经具备了一个基本的框架,这一章我们就开始添砖加瓦了. 敌机定时发射一个子弹,子弹的方向是从上到下,但是发射子弹的代码应该放在哪儿呢? 从面向对象编程的思想来说,子弹是敌 ...
- 用JS制作《飞机大作战》游戏_第4讲(创建敌方飞机、敌方飞机发射子弹、玩家子弹击中敌方小飞机,小飞机死亡)-陈远波
一.创建敌方飞机 1.思考创建思路: 创建敌方飞机思路与创建玩家飞机思路一样: (1)思考敌方飞机具备什么属性: 敌方飞机的图片.坐标.飞行速度.状态(是否被击中) 设置小飞机被击中时消失时间.飞机可 ...
- [知了堂学习笔记]_用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- 用JS制作《飞机大作战》游戏_第3讲(玩家发射子弹)-陈远波
一.公布上一讲中玩家飞机上.下.右移动实现的代码: /*=========================键盘按下事件 keycode为得到键盘相应键对应的数字==================== ...
- unity零基础开始学习做游戏(四)biu~biu~biu发射子弹打飞机
-------小基原创,转载请给我一个面子 主角都能移动了,那不得做点什么伸张正义,守护世界和平的事嘛,拿起家伙biu~biu~biu~ 首先得做一个好人和一个坏人 老规矩,Canvas下创建两个Im ...
- Unity 飞机的子弹轨迹
最近公司在开发一款儿童打飞机游戏. 策划跟我说能在子弹上加上一些轨迹就好了. 比如 旋转 左右移动呀.然后它就很愉快的跑去截其他游戏的图啦... 我看见图的时候, 解决方案: 1. 使用牛逼的 ...
- Egret学习笔记 (Egret打飞机-9.子弹对敌机和主角的碰撞)
运行起来,虽然主角飞机和敌机都在互相发射子弹,但是子弹打中了就和没打中效果是一样的.. 这一章我们就来处理子弹和飞机的碰撞问题. 我们所有的操作都是基于Main这个容器来做的.所以我就把这个处理放到M ...
- android小游戏 飞机躲子弹
最近android老师让每人写一个小东西,因为之前学awt时写过一个java版的飞机躲子弹,所以这次想写成android版的. 文件直接导入就行http://files.cnblogs.com/fil ...
随机推荐
- 刷题防止Time Limit Exceeded(TLE)技巧
1.C++ 不要使用cin,cout,该使用scanf和printf 2.Java 不要使用Scanner,改用BufferedReader 3.Python 在文件开始的地方加入 import ps ...
- Mac 开发配置手册
转自:http://www.cocoachina.com/mac/20141112/10198.html 手册内容为「如何让一部全新的 MacBook 快速完成开发环境配置」,主要面向 Web 开发者 ...
- ASP.NET 跨域请求之jQuery的ajax jsonp的使用解惑 (转载)
前天在项目中写的一个ajax jsonp的使用,出现了问题:可以成功获得请求结果,但没有执行success方法,直接执行了error方法提示错误——ajax jsonp之前并没有用过,对其的理解为跟普 ...
- day 03 --Haproxy 增加, 删除,查询
key 知识点:函数的定义, 函数的递归调用, flag 标志位的使用,eval() 函数 #!C:\Program Files\Python35\bin # -*- conding:utf-8 -* ...
- Jquery 操作 select 的操作指南
这里我们以一个简单的select作为原型来进行说明: <select> <option value="a1">香蕉1</option> < ...
- iPhone 横竖屏切换,全屏播放的三种方式
1. 调用系统自带的强制屏幕旋转不过还得在AppDelegate中重写下面方法 - (UIInterfaceOrientationMask)application:(UIApplication *)a ...
- 用IntelliJ IDEA 配置Maven并部署Maven工程到Tomcat(Windows中)
近几天做一个新项目才接触Intellij IDEA 1.在官网下载了maven 解压并新建一个本地仓库文件夹 2.配置本地仓库路径 3.配置maven环境变量 4.在IntelliJ IDEA中配置m ...
- TCP/IP初识(一)
TCP/IP学习记录,如有错误请指正,谢谢!!! 什么是TCP/IP协议? TCP/IP协议族分为四层(另一个名字是Internet协议族(Internet Protocol Suite)):链路层. ...
- SHELL脚本简单的赋值与递增
Count=`expr $Count + 1`;#可以在各种shell执行,其他类C的写法只能在指定的bash版本执行; 赋值不能带$, 带$相当于字符串常量了;执行脚本参考如下 #!/bin/sh ...
- C++笔记006:关于类的补充
原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 关于类的补充: 类是一个数据类型(固定大小内存块的别名),定义一个类,是一个抽象的概念,不会给你分配内存,用数据类型定义变量的时候,才会分 ...