Python基础——Task6

任务:
完成飞机大战编写
具体参考:https://www.cnblogs.com/Jansens520/p/8469899.html

实现步骤:
1.创建窗口
2.创建一个玩家飞机,按方向键可以左右移动
3.给玩家飞机添加按空格键发射子弹功能
4.创建一个敌机
5.敌机自动左右移动
6.敌机自动发射子弹

一、创建窗口

import pygame
import time

def main():
    #1.创建窗口
    screen = pygame.display.set_mode((480,852))
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景.jpg')

    while True:
        #3 显示到屏幕
        screen.blit(background,(0,0))
        pygame.display.update()
        time.sleep(0.05)

if __name__ == '__main__':
    main()

二、显示玩家飞机,检测键盘,控制飞机左右移动

import pygame
import time
from pygame.locals import *

class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700               
        self.screen = screen_temp  #游戏窗口
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\大飞机.jpg')   

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))   #加载玩家飞机到窗口

    def move_left(self):
        self.x -= 5

    def move_right(self):
        self.x += 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')
def main():
    #1.创建窗口
    screen = pygame.display.set_mode((480,852),0,0)
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景.jpg')
    # 创建一个飞机对象
    hero = HeroPlane(screen)

    while True:
        #把背景图片放到窗口显示
        screen.blit(background,(0,0))
        #显示玩家飞机到定义的(x,y)坐标位置
        hero.display()
        pygame.display.update()
        #检测键盘,控制玩家飞机移动
        key_control(hero)
        time.sleep(0.05)

if __name__ == '__main__':
    main()

三、玩家发射子弹

import pygame
import time
from pygame.locals import *

class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp  #游戏窗口
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\子弹.jpg')
        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()

    def move_left(self):
        self.x -= 5

    def move_right(self):
        self.x += 5

    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('C:\Users\llw\Desktop\图片\小飞机.jpg')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        self.y -= 20

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,0)
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景.jpg')
    # 创建一个飞机对象
    hero = HeroPlane(screen)

    while True:
        #把背景图片放到窗口显示
        screen.blit(background,(0,0))
        #显示玩家飞机到定义的(x,y)坐标位置
        hero.display()
        pygame.display.update()
        #检测键盘,控制玩家飞机移动
        key_control(hero)
        time.sleep(0.05)

if __name__ == '__main__':
    main()

四、创建一个敌机

--snip--
class EnemyPlane(object):
    def __init__(self,screen_temp):
        self.x = 0
        self.y = 0
        self.screen = screen_temp
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\小飞机.jpg')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

--snip--

def main():
    --snip--
    #4 创建一个敌机
    enemy = EnemyPlane(screen)

    while True:
        --snip--
        enemy.display()
        --snip

五、控制敌机左右移动

import pygame
import time
from pygame.locals import *

class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp  #游戏窗口
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\子弹.jpg')
        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()

    def move_left(self):
        self.x -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(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('C:\Users\llw\Desktop\图片\小飞机.jpg')
        self.direction = 'right'     #定义敌机默认往右移动

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        if self.direction == 'right':
            self.x += 8
        elif self.direction == 'left':
            self.x -= 8

        if self.x > 430:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'

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('C:\Users\llw\Desktop\图片\大飞机.jpg')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        self.y -= 20

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,0)
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景.jpg')
    #3 创建一个飞机对象
    hero = HeroPlane(screen)
    #4 创建一个敌机
    enemy = EnemyPlane(screen)

    while True:
        #把背景图片放到窗口显示
        screen.blit(background,(0,0))
        #显示玩家飞机到定义的(x,y)坐标位置
        hero.display()
        enemy.display()
        enemy.move()
        pygame.display.update()
        #检测键盘,控制玩家飞机移动
        key_control(hero)
        time.sleep(0.05)

if __name__ == '__main__':
    main()

六、删除子弹,只要发射出去的子弹超过边界就删掉

import pygame
import time
from pygame.locals import *

class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp  #游戏窗口
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\子弹.jpg')
        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 -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(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('C:\Users\llw\Desktop\图片\小飞机.jpg')
        self.direction = 'right'     #定义敌机默认往右移动

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        if self.direction == 'right':
            self.x += 8
        elif self.direction == 'left':
            self.x -= 8

        if self.x > 430:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'

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('C:\Users\llw\Desktop\图片\大飞机.jpg')

    def display(self):
        self.screen.blit(self.image,(self.x,self.y))

    def move(self):
        self.y -= 20

    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,852),0,0)
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景.jpg')
    #3 创建一个飞机对象
    hero = HeroPlane(screen)
    #4 创建一个敌机
    enemy = EnemyPlane(screen)

    while True:
        #把背景图片放到窗口显示
        screen.blit(background,(0,0))
        #显示玩家飞机到定义的(x,y)坐标位置
        hero.display()
        enemy.display()
        enemy.move()
        pygame.display.update()
        #检测键盘,控制玩家飞机移动
        key_control(hero)
        time.sleep(0.05)

if __name__ == '__main__':
    main()

七、添加敌机放射子弹功能

import pygame
import time
from pygame.locals import *
import random

class HeroPlane(object):
    def __init__(self,screen_temp):
        self.x = 210
        self.y = 700
        self.screen = screen_temp  #游戏窗口
        self.image = pygame.image.load('C:\Users\llw\Desktop\图片\子弹.jpg')
        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 -= 5

    def move_right(self):
        self.x += 5

    def fire(self):
        self.bullet_list.append(Bullet(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('C:\Users\llw\Desktop\图片\子弹.jpg')
        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()
            if bullet.judge():   #判断子弹是否越界
                self.bullet_list.remove(bullet)

    def move(self):
        if self.direction == 'right':
            self.x += 8
        elif self.direction == 'left':
            self.x -= 8

        if self.x > 430:
            self.direction = 'left'
        elif self.x < 0:
            self.direction = 'right'

    def fire(self):
        random_num = random.randint(1,80)
        if random_num == 10 or random_num == 40:
            self.bullet_list.append(EnemyBullet(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('C:\Users\llw\Desktop\图片\大飞机.jpg')

    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('C:\Users\llw\Desktop\图片\大飞机.jpg')

    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,0)
    #2 创建一个背景图片
    background = pygame.image.load('C:\Users\llw\Desktop\图片\背景\jpg')
    #3 创建一个飞机对象
    hero = HeroPlane(screen)
    #4 创建一个敌机
    enemy = EnemyPlane(screen)

    while True:
        #把背景图片放到窗口显示
        screen.blit(background,(0,0))
        #显示玩家飞机到定义的(x,y)坐标位置
        hero.display()
        enemy.display()
        enemy.move()
        enemy.fire()
        pygame.display.update()
        #检测键盘,控制玩家飞机移动
        key_control(hero)
        time.sleep(0.05)

if __name__ == '__main__':
    main()

结果:
Python基础——Task6_第1张图片

你可能感兴趣的:(python)