day_5.14 py 飞机大战Demo
飞机未完,继续做 2018-5-14 21:05:45 明天继续

循环里面的坑;
删除列表元素后循环了打印的不一样,主要是比如相邻的删除了,33,44 删除33 循环一次后44跑到33位置, 试一下就知道了dd

#!/usr/bin/env/python
#-*-coding:utf-8-*-
'''
2018-5-13 19:53:46 完善成功
一个打飞机的游戏
其实就是面向对象那个
有个主方法. 然后有飞机然后又子弹,逐步的迭代 2018-5-14 18:40:15
继续开干 '''
import pygame
import time
from pygame.locals import *
import random
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.bullet_list = [] #储存发射出去子弹对象引用
self.direction ="right" #用来存储飞机默认的显式方向 def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
def move(self):
if self.direction =="right":
self.x+= 10
elif self.direction =="left":
self.x -= 10
if self.x>480-50:
self.direction ="left"
elif self.x<0:
self.direction ="right" def fire(self):
random_num = random.randint(1,100)
if random_num ==8 or random_num ==20:
self.bullet_list.append(EnemyBullet(self.screen,self.x,self.y)) class HeroPlane(object):
'''玩家飞机'''
def __init__(self,screen_temp):
self.x=210
self.y=700
self.screen =screen_temp
self.image =pygame.image.load("./feiji/hero1.png")
self.bullet_list = [] #储存发射出去子弹对象引用
def display(self):
self.screen.blit(self.image,(self.x,self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
if bullet.judge(): #判断子弹是否越界
self.bullet_list.remove(bullet)
def move_left(self):
self.x -=10
def move_right(self):
self.x +=10
def fire(self):
self.bullet_list.append(Bullet(self.screen,self.x,self.y)) class Bullet(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-=20
def judge(self): #判断是否越界
if self.y <0:
return True
else:
return False class EnemyBullet(object):
def __init__(self,screen_temp,x,y):
self.x=x+25
self.y=y+40
self.screen =screen_temp
self.image =pygame.image.load("./feiji/bullet1.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 >852:
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,852),0,32)
#2. 创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3 创建一个飞机图片
hero = HeroPlane(screen)
#4.创建一个敌机
enemy = EnemyPlane(screen)
while True:
screen.blit(background,(0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机的移动方法
enemy.fire()#敌机开火
pygame.display.update()
key_control(hero)
time.sleep(0.1) if __name__ =="__main__":
main()
day_5.14 py 飞机大战Demo的更多相关文章
- Html飞机大战(四):状态的切换(界面加载类的编辑)
好家伙,接着写 既然我们涉及到状态了,那么我们也会涉及到状态的切换 那么我们怎样切换状态呢? 想象一下,如果我玩的游戏暂停了,那么我们肯定是通过点击或者按下某个按键来让游戏继续 这里我们选 ...
- [U3D Demo] 手机飞机大战
游戏截图 使用插件 DOTween NGUI 游戏介绍 游戏使用C#开发,素材是<全民飞机大战>中提取出来的,该游戏最早是去年由Flash Air+Starling开发的Demo,后来我修 ...
- 微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js)
微信小游戏 demo 飞机大战 代码分析(四)(enemy.js, bullet.js, index.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞 ...
- 微信小游戏 demo 飞机大战 代码分析 (三)(spirit.js, animation.js)
微信小游戏 demo 飞机大战 代码分析(三)(spirit.js, animation.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码 ...
- 微信小游戏 demo 飞机大战 代码分析 (二)(databus.js)
微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- 微信小游戏 demo 飞机大战 代码分析 (一)(game.js, main.js)
微信小游戏 demo 飞机大战 代码分析(一)(main.js) 微信小游戏 demo 飞机大战 代码分析(二)(databus.js) 微信小游戏 demo 飞机大战 代码分析(三)(spirit. ...
- 微信demo小游戏:飞机大战从无到有
微信demo游戏飞机大战从无到有 现在创建新项目会默认给飞机大战的demo,这里给大家从基础开始讲解游戏的从无到有是怎么实现的. 具体实现步骤: 创建背景图->背景图运动起来->创建飞机并 ...
- day_5.17 飞机大战
ps:2018-7-24 20:58:11 重新整理这个飞机大战源码,我虽然这个时候没看源码,但是知道思路的话用其他语言还是可以写出来的! ''' 2018-5-13 19:53:46 完善成功 一个 ...
- 飞机大战-面向对象-pygame
飞机大战 最近学习了python的面向对象,对面向对象的理解不是很深刻. 面向对象是数据和函数的'打包整理',将相关数据和处理数据的方法集中在一个地方,方便使用和管理. 本着学习的目的,在网上找了这个 ...
随机推荐
- Chrome网页性能分析工具
performance-analyser https://chrome.google.com/webstore/detail/performance-analyser/djgfmlohefpomchf ...
- 阿里云服务器Centos7.4开放80端口的记录
问题: 阿里云服务器安装的是centos7, 搭建网站安装lnmp1.5后发现访问不了, 不明所以, 在一论坛找到关于80端口未开放的原因. 需求: 开放80端口.于是有了下面第一,二,三部分关于开放 ...
- ASP.NET MVC 自定义处理JSON ActionResult类
1.统一JSON格式处理方式,同时指定ContentType类型,解决低版本浏览器获取json时ContentType为application/json提示下载的问题. public abstract ...
- CentOS下如何查看并杀死僵尸进程
昨天服务器到期,之前的服务器由于空间小,不能满足现在的服务要求,就新购买了一个服务器,目前正在调试安装中! 在调试过程中,发现系统中有很多僵尸进程,现在就是找出这些僵尸进程,并将其杀死. 用top查看 ...
- syslog之三:建立Windows下面的syslog日志服务器
目录: <syslog之一:Linux syslog日志系统详解> <syslog之二:syslog协议及rsyslog服务全解析> <syslog之三:建立Window ...
- 2018年末--积极拥抱h5.转载 大前端时代来临,我们何去何从?
1.大前端时代是什么? 大前端时代是WEB统一的时代,利用html5或者6甚至7,不但可以开发传统的网站,做炫酷的网页动态效果,更可以采用BS架构应用程序.开发手机端web应用.移动端Native应用 ...
- 全面理解Javascript闭包和闭包的几种写法及用途【转】
一.什么是闭包和闭包的几种写法和用法 1.什么是闭包 闭包,官方对闭包的解释是:一个拥有许多变量和绑定了这些变量的环境的表达式(通常是一个函数),因而这些变量也是该表达式的一部分.闭包的特点: 1. ...
- [Big Data - ZooKeeper] ZooKeeper: A Distributed Coordination Service for Distributed Applications
ZooKeeper ZooKeeper: A Distributed Coordination Service for Distributed Applications Design Goals Da ...
- linux每日命令(30):Linux 用户及用户组相关文件、命令详解
一. 用户.用户组概念及其文件结构详解 Linux用户只有两个等级:root及非root.Linux中还有一部分用户,如:apache.mysql.nobody.ftp等,这些也都是非root用户,即 ...
- PHP更改自动加载的顺序
composer的锅 自从PHPer们用上了composer后,对于传统的加载方式倒是不会用了,可谓是"收之东隅,失之桑榆". 下面说一下怎么改变加载顺序来覆写Laravel中的h ...