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()

 

posted @ 2017-11-27 13:26  venicid  阅读(293)  评论(0编辑  收藏  举报