《python编程从入门到实践》第十二章习题答案

12.1

import pygame

def run_game():
    #初始化创立一个屏幕对象
    pygame.init()
    screen = pygame.display.set_mode((1600,1200))
    pygame.display.set_caption('blue sky')
    bg_color = (0,0,255)
    #背景色设置成蓝色

    while True:
        screen.fill(bg_color)
        pygame.display.flip()
        #开始循环,不断刷新背景色,让最近绘制的屏幕可见
run_game()

12.2

先创立类
character.py

import pygame
    
        class Character():
            def __init__(self,screen):
                self.screen = screen
                #初始化人物并设置初始位置
        
                self.image = pygame.image.load('caocao/caocao.bmp')
                self.rect = self.image.get_rect()
                self.screen_rect = screen.get_rect()
                #加载人物图像获取其外接矩形
        
                self.rect.centerx = self.screen_rect.centerx
                self.rect.centery = self.screen_rect.centery
                #将人物放到屏幕中间
        
            def blitme(self):
                self.screen.blit(self.image,self.rect)
                #在指定位置绘制人物

再运行程序

import pygame
from character import Character

def run_game():
    #初始话创立一个屏幕对象
    pygame.init()
    screen = pygame.display.set_mode((1600,1200))
    pygame.display.set_caption('cao cao')
    bg_color = (255,255,255)
    #背景色为白色

    character=Character(screen)
    #创立角色

    while True:
        screen.fill(bg_color)
        character.blitme()
        #角色显示于屏幕中

        pygame.display.flip()
run_game()

12.3

创立设置类型
setting.py

class Setting():
    #这里放有《火箭》所有的类
    def __init__(self):
        #屏幕设置
        #初始化游戏设置
        self.screen_width = 1600
        self.screen_height = 1200
        self.bg_color = (255,255,255)

        self.huojian_speed_factor = 1.5

创立火箭的类型
huojian.py

import pygame

class Huojian():
    def __init__(self,ai_settings,screen):
        self.screen = screen
        self.ai_settings = ai_settings
        #初始化火箭图像加载位置

        self.image = pygame.image.load('huojian/huojian.bmp')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        # 加载火箭图像并且获取其外接矩形

        self.rect.centerx = self.screen_rect.centerx
        self.rect.centery = self.screen_rect.centery
        #将火箭放到屏幕中间

        self.centerx = float(self.rect.centerx)
        self.centery = float(self.rect.centery)
        # 在火箭的属性center中存储小数值

        self.moving_right = False
        self.moving_left = False
        self.moving_top = False
        self.moving_bottom = False

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.centerx += self.ai_settings.huojian_speed_factor

        if self.moving_left and self.rect.left > 0:
            self.centerx -= self.ai_settings.huojian_speed_factor

        if self.moving_top and self.rect.bottom > self.screen_rect.top and self.rect.bottom < 1200:
            self.centery += self.ai_settings.huojian_speed_factor

        if self.moving_bottom and self.rect.top > 0:
            self.centery -= self.ai_settings.huojian_speed_factor
            #根据标志调整火箭位置
        self.rect.centerx = self.centerx
        self.rect.centery = self.centery
        #根据self.center更新rect对象

    def blitme(self):
        self.screen.blit(self.image,self.rect)
        #在指定位置绘制火箭

再写好火箭的左右上下移动功能。
game_function.py

import sys

import pygame

def check_keydown_events(event,huojian):
    if event.key == pygame.K_RIGHT:
        huojian.moving_right = True
    elif event.key == pygame.K_LEFT:
        huojian.moving_left = True
    elif event.key == pygame.K_DOWN:
        huojian.moving_top = True
    elif event.key == pygame.K_UP:
        huojian.moving_bottom = True
#按键触发移动事件

def check_keyup_events(event,huojian):
    if event.key == pygame.K_RIGHT:
        huojian.moving_right = False
    elif event.key == pygame.K_LEFT:
        huojian.moving_left = False
    elif event.key == pygame.K_DOWN:
        huojian.moving_top = False
    elif event.key == pygame.K_UP:
        huojian.moving_bottom = False
#按键松开触发事件

def check_events(huojian):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,huojian)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event,huojian)
#总领触发事件

def update_screen(ai_settings,screen,huojian):
    screen.fill(ai_settings.bg_color)
    huojian.blitme()

    pygame.display.flip()
#使得最近的绘制体现在屏幕上

最后重构好主程序,并且运行这个主程序。
12.3.py

import pygame

from huojian import Huojian

from setting import Setting

import game_function as gf

def run_game():
    #初始化游戏并创立一个屏幕对象
    pygame.init()
    ai_settings = Setting()
    screen = pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Huojian fei")

    huojian = Huojian(ai_settings,screen)
    #创立火箭

    while True:
        # 监视键盘和鼠标事件,有方向按键就往这个方向移动并且实施
        gf.check_events(huojian)
        huojian.update()

        # 每次循环时都会重绘屏幕
        gf.update_screen(ai_settings, screen, huojian)


run_game()

12.4.py

import pygame

def run_game():
    pygame.init()
    screen = pygame.display.set_mode((1200,800))
    #建立一个屏幕

    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                print(event.key)
            #当按下键盘时,都会打印相应按键的值

run_game()

程序运行后终端会显示按键的数值

12.5题

12.5.py

import pygame

from settings0 import Settings0

from ship0 import Ship0

import game_functions0 as gf

from pygame.sprite import Group

def run_game():
    #初始化游戏并创立一个屏幕对象
    pygame.init()
    ai_settings0 = Settings0()
    screen0 = pygame.display.set_mode((ai_settings0.screen_width,ai_settings0.screen_height))
    pygame.display.set_caption("Alien Invasion")

    ship0=Ship0(ai_settings0,screen0)
    #创建一艘飞船
    bullets = Group()
    #创立子弹编组

    #开始游戏的主循环
    while True:

        #监视键盘和鼠标事件,有方向按键就往这个方向移动并且实施
        gf.check_events(ai_settings0,screen0,ship0,bullets)
        ship0.update()
        gf.update_bullets(bullets)
        #每次循环时都会重绘屏幕
        gf.update_screen(ai_settings0,screen0,ship0,bullets)

run_game()

这个12.5.py是运行游戏的主程序,我们要知道我们需要创立的对象有子弹bullet0和飞船ship0,任何需要有一个统一设定游戏的设置settings0,还需要把运行的主要内容放到文件game_functions0中

bullet0.py

import pygame
from pygame.sprite import Sprite

class Bullet0(Sprite):
    def __init__(self,ai_settings0,screen0,ship0):
        #定义子弹的属性
        super(Bullet0,self).__init__()
        self.screen = screen0
        self.rect = pygame.Rect(0,0,ai_settings0.bullet_width,ai_settings0.bullet_height)
        #在(0,0)处创立一个子弹的矩形,再设置其位置
        self.rect.centery = ship0.rect.centery
        self.rect.right = ship0.rect.right
        self.x = float(self.rect.x)
        #子弹的位置在飞船中间射出

        self.color=ai_settings0.bullet_color
        self.speed_factor = ai_settings0.bullet_speed_factor

    def update(self):
        self.x +=self.speed_factor
        self.rect.x = self.x
        #向右移动子弹

    def draw_bullet(self):
        pygame.draw.rect(self.screen,self.color,self.rect)
        #绘制子弹

ship0.py

import pygame

class Ship0():

    def __init__(self,ai_settings0,screen0):
        self.ai_settings0 = ai_settings0
        self.screen0 = screen0
        #c确立Ship0的一些属性

        self.image = pygame.image.load('ship/ship.bmp')
        self.rect = self.image.get_rect()
        self.screen0_rect = screen0.get_rect()
        #将飞船图像加载并且获取外形

        self.rect.centery = self.screen0_rect.centery
        self.rect.left = self.screen0_rect.left
        #将飞船加载到屏幕的左侧中央

        self.center = float(self.rect.centery)
        #飞船属性center存储小数

        self.moving_top = False
        self.moving_bottom = False
        #移动标志初始化

    def update(self):
        if self.moving_top and self.rect.top > 0 :
            self.center -= self.ai_settings0.ship_speed_factor

        if self.moving_bottom and self.rect.bottom < 800 :
            self.center += self.ai_settings0.ship_speed_factor
        # 根据标志调整飞船位置
        self.rect.centery = self.center
        # 根据self.center更新rect对象
    def blitme(self):
        self.screen0.blit(self.image,self.rect)
        #在指定位置绘制飞船

settings0.py

class Settings0():
    #这里放有《外星人入侵》所有的类
    def __init__(self):
        #屏幕设置
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color = (230,230,230)

        self.ship_speed_factor = 1.5
        #飞船设置
        self.bullet_speed_factor = 1
        self.bullet_width = 15
        self.bullet_height = 3
        self.bullet_color = 60,60,60
        self.bullets_allowed = 3
        #子弹设置

game_functions0.py

import sys

import pygame

from bullet0 import Bullet0

def fire_bullet(ai_settings0,screen0,ship0,bullets):
    if len(bullets) < ai_settings0.bullets_allowed:
        new_bullet = Bullet0(ai_settings0, screen0, ship0)
        bullets.add(new_bullet)
    #重构模块,让模块中子弹的刷新函数单独领出来


def check_keydown_events(event,ai_settings0,screen0,ship0,bullets):
    if event.key == pygame.K_UP:
        ship0.moving_top = True
    elif event.key == pygame.K_DOWN:
        ship0.moving_bottom = True
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings0,screen0,ship0,bullets)
        #创建一个子弹,加到编组bullets中
    #按键相应事件

def check_keyup_events(event,ship0):
    if event.key == pygame.K_UP:
        ship0.moving_top = False
    elif event.key == pygame.K_DOWN:
        ship0.moving_bottom = False
    #按键松开事件

def check_events(ai_settings0,screen0,ship0,bullets):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event,ai_settings0,screen0,ship0,bullets)

        elif event.type == pygame.KEYUP:
            check_keyup_events(event,ship0)
#相应鼠标键盘的事件

def update_screen(ai_settings0,screen0,ship0,bullets):
    screen0.fill(ai_settings0.bg_color)
    ship0.blitme()
    for bullet in bullets.sprites():
        bullet.draw_bullet()

    pygame.display.flip()
    #让最近绘制的屏幕可见

def update_bullets(bullets):
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.left >= 1200:
            bullets.remove(bullet)

需要把这5个文件放到同一个文件夹下面,任何运行12.5.py就可以看到最后的效果了

你可能感兴趣的:(python)