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.飞机左右移动

    day 6 敌机-LMLPHP

####   飞机不动了

 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没有删除,漏掉了

  day 6 敌机-LMLPHP

###   找个空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

05-08 08:02