《Python快速编程入门》飞机大战篇详细代码

如题,因《Python快速编程入门》中飞机大战篇本身存在一些代码错误(p.258的父类重写覆盖、p.254的拼写错误等),且课本附赠的教材答案中并未给出完整代码,故上传此文,供同学们学习参考。

1.main.py

import pygame
import time
from pygame.locals import *
from plane import *

def start():
    screen = pygame.display.set_mode((480, 890), 0 ,32)
    #图片调用
    image_file_path = './feiji/background.png'
    background = pygame.image.load(image_file_path).convert()
    #创建一个飞机对象
    hero_plane = HeroPlane(screen, "hero")
    #创建一个敌机对象
    enemy_plane = EnemyPlane(screen, "enemy")
    while True:
        screen.blit(background, (0, 0))
        hero_plane.display()
        enemy_plane.display()
        enemy_plane.move()
        enemy_plane.launch_bullet()
        #监听用户事件
        for event in pygame.event.get():
            if event.type == QUIT:
                print("exit")
                exit()
            elif event.type == KEYDOWN:
                if event.key == K_a or event.key == K_LEFT:
                    print('left')
                    hero_plane.move_left()
                elif event.key == K_d or event.key == K_RIGHT:
                    print('right')
                    hero_plane.move_right()
                elif event.key == K_SPACE:
                    print('space')
                    hero_plane.launch_bullet()
        time.sleep(0.01)
        pygame.display.update()

if __name__ == '__main__':
        start()

2.plane.py

import pygame
import random
from bullet import *

class Plane(object):

    def __init__(self, screen, name):
        # 设置默认飞机的位置
        self.x = 230
        self.y = 600
        #飞机的名称
        self.name = name
        # 设置要显示内容的窗口
        self.screen = screen
        self.image_name = "./feiji/hero.png"
        self.image = pygame.image.load(self.image_name).convert()
        # 用来储存英雄飞机发射的所有子弹
        self.bullet_list = []

    def display(self):
        #更新飞机的位置
        self.screen.blit(self.image, (self.x, self.y))
        #存放需要删除的信息
        need_del_list = []
        #历遍bullet_list经行判断
        for i in self.bullet_list: 
            if i.judge():
                need_del_list.append(i)
        #删除self.bullet_list中需要删除的对象
        for i in need_del_list:
            self.bullet_list.remove(i)
        for bullet in self.bullet_list:
            bullet.display()
            bullet.move()

    def launch_bullet(self):
        new_bullet = PublicBullet(self.x, self.y, self.screen, self.name)
        self.bullet_list.append(new_bullet)

class HeroPlane(Plane):

    def __init__(self, screen, name):
        super().__init__(screen, name)
        self.x = 230
        self.y = 600
        self.image_name = "./feiji/hero.png"
        self.image = pygame.image.load(self.image_name).convert()


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

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

class EnemyPlane(Plane):
        #重写父类的__init__方法
    def __init__(self, screen, name):
        # 设置飞机默认位置
        super().__init__(screen, name)
        self.x = 0
        self.y = 0
        self.image_name = "./feiji/enemy-1.png"
        self.image = pygame.image.load(self.image_name).convert()
         #将自动引导设为右
        self.direction = "right"

    def move(self):
        if self.direction == "right":
            self.x += 2
        elif self.direction == "left":
            self.x -= 2
        if self.x > 480 - 50:
            self.direction = "left"
        elif self.x < 0:
            self.direction = "right"

    def launch_bullet(self):
        #随机数字
        number = random.randint(1,100)
        if number == 88:
            #调用launch_bullet()
            super().launch_bullet()

3.bullet.py

import pygame

class PublicBullet(object):

    def __init__(self, x, y, screen, plane_name):
        self.screen = screen
        self.plane_name = plane_name
        if plane_name == "hero":
            self.x = x + 10 
            self.y = y - 20
            image_name = "./feiji/bullet-3.png" 
        elif plane_name == "enemy":
            self.x = x + 10 
            self.y = y + 40
            image_name = "./feiji/bullet-1.png" 
        self.image = pygame.image.load(image_name).convert() 

    def move(self):
        if self.plane_name == "hero":
            self.y -= 2
        elif self.plane_name == "enemy":
            self.y += 2

    def display(self):
        #根据名字赋予图片初始位置
        self.screen.blit(self.image, (self.x, self.y))

    def judge(self):
        #子弹位置判断
        if self.y > 890 or self.y < 0: 
            return True
        else:
            return False

此外值得一提的是,虽然课本上给出了PyCharm的安装及PyGame的导入方法,但本人却遇到了并未提及的“无法导入”的情况
《Python快速编程入门》飞机大战篇详细代码_第1张图片
处理方法是选择“Show All…”,重新选择有效的py路径即可
《Python快速编程入门》飞机大战篇详细代码_第2张图片
《Python快速编程入门》飞机大战篇详细代码_第3张图片

你可能感兴趣的:(python)