"Python programming from entry to practice" exercises 14-2 Chapter XIV answer

Title: Create a rectangle, it moves down at a constant speed in the right edge of the screen. Then, create a spaceship on the left edge of the screen, the player can move the ship up and down, and the aforementioned rectangular target shooting. Play for adding a button to start the game, the game ends when the player did not hit the target three times, and re-display the Play button, allows players by clicking on the button to re-start the game.

Complete results:

Python: spaceship shooting

Advantage: with one of the few online solutions for comparison, the program is divided into several functional files, code structure and thinking more clearly.

Experience: function for loop increments the variable can not be considered to build a class (such as Settings), this property of a class variable (self.fail_times = 0), then instantiate the class (such as ai_settings). Finally, the function call in the for loop (ai_settings.fail_times).

followed by the ship's original :( download the following source code into the same folder and then run the program)

Source:

File 1: space_war.py

import pygame
from pygame.sprite import Group
from settings import Settings
from ship import Ship
import game_functions as gf
from game_stats import GameStats
from button import Button
from target import Target

def run_game():
    pygame.init()
    ai_settings=Settings()
    screen=pygame.display.set_mode((ai_settings.screen_width,ai_settings.screen_height))
    pygame.display.set_caption("Space War")
    play_button=Button(ai_settings,screen,"Play")
    stats=GameStats(ai_settings)
    ship=Ship(ai_settings,screen)
    bullets=Group()
    target=Target(ai_settings,screen)
    while True:
        gf.check_events(ai_settings,screen,stats,play_button,ship,target,bullets)
        if stats.game_active:
            ship.update()
            target.update()
            gf.update_bullets(ai_settings,screen,ship,target,bullets)
            gf.update_target(ai_settings,stats,screen,ship,target,bullets)
            gf.check_fail(ai_settings,bullets,stats,target,screen)
        gf.update_screen(ai_settings,screen,stats,ship,target,bullets,play_button)

run_game()

File 2: ship.py

import pygame

class Ship():
    def __init__(self,ai_settings,screen):
        self.screen=screen
        self.ai_settings=ai_settings
        self.image=pygame.image.load('ship.bmp')
        self.rect=self.image.get_rect()
        self.screen_rect=screen.get_rect()
        self.rect.centery=self.screen_rect.centery
        self.rect.left=self.screen_rect.left
        self.center=float(self.rect.centery)
        self.moving_up=False
        self.moving_down=False

    def update(self):
        if self.moving_up and self.rect.top>self.screen.get_rect().top:
            self.center-=self.ai_settings.ship_speed_factor
        if self.moving_down and self.rect.bottom<self.screen.get_rect().bottom:
            self.center+=self.ai_settings.ship_speed_factor
        self.rect.centery=self.center

    def blitme(self):
        self.screen.blit(self.image,self.rect)

    def center_ship(self):
        self.center=self.screen_rect.centery

File 3: target.py

import pygame
from pygame.sprite import Sprite

class Target(Sprite):
    def __init__(self,ai_settings,screen):
        super(Target,self).__init__()
        self.screen=screen
        self.ai_settings=ai_settings
        self.rect=pygame.Rect(0,0,ai_settings.target_width,ai_settings.target_height)
        self.rect.right=self.screen.get_rect().right
        self.rect.centery=self.screen.get_rect().centery

        self.y=float(self.rect.y)
        self.color=ai_settings.target_color
        self.speed_factor=ai_settings.target_speed_factor

    def check_edges(self):
        screen_rect=self.screen.get_rect()
        if self.rect.top<0:
            return True
        elif self.rect.bottom>screen_rect.bottom:
            return True

    def update(self):
        self.y+=(self.speed_factor*self.ai_settings.fleet_direction)
        self.rect.y=self.y

    def draw_target(self):
        pygame.draw.rect(self.screen,self.color,self.rect)

File 4: settings.py

class Settings():
    def __init__(self):
        self.screen_width=1200
        self.screen_height=800
        self.bg_color=(230,230,230)
        self.ship_speed_factor=5
        self.bullet_speed_factor=10
        self.bullet_width=30
        self.bullet_height=15
        self.bullet_color=60,60,60
        self.bullets_allowed=3
        self.target_speed_factor=1
        self.target_direction=1
        self.target_width=300
        self.target_height=100
        self.target_color=180
        self.fleet_direction=1
        self.ship_limit=3
        self.bullets_left=3
        self.fail_times = 0

 File 5: game_stats.py

class GameStats():

    def __init__(self,ai_settings):
        self.ai_settings=ai_settings
        self.reset_stats()
        self.game_active=False

    def reset_stats(self):
        self.ships_left=self.ai_settings.ship_limit

File 6: bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):
    def __init__(self,ai_settings,screen,ship):
        super(Bullet,self).__init__()
        self.screen=screen
        self.rect=pygame.Rect(0,0,ai_settings.bullet_width,ai_settings.bullet_height)
        self.rect.centery=ship.rect.centery
        self.x=float(self.rect.x)
        self.color=ai_settings.bullet_color
        self.speed_factor=ai_settings.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)

File 7: button.py

import pygame.font
class Button():
    def __init__(self,ai_settings,screen,msg):
        self.screen=screen
        self.screen_rect=screen.get_rect()
        self.width,self.height=200,50
        self.button_color = (0, 255, 0)
        self.text_color = (255, 255, 255)
        self.font=pygame.font.SysFont(None,48)
        self.rect=pygame.Rect(0,0,self.width,self.height)
        self.rect.center=self.screen_rect.center
        self.prep_msg(msg)

    def prep_msg(self,msg):
        self.msg_image=self.font.render(msg,True,self.text_color,self.button_color)
        self.msg_image_rect=self.msg_image.get_rect()
        self.msg_image_rect.center=self.rect.center

    def draw_button(self):
        self.screen.fill(self.button_color,self.rect)
        self.screen.blit(self.msg_image,self.msg_image_rect)

File 8: game_functions.py

import sys
import pygame
from bullet import Bullet
from target import Target
from time import sleep

def check_keydown_events(event,ai_settings,screen,ship,bullets):
    if event.key==pygame.K_DOWN:
        ship.moving_down=True
    elif event.key==pygame.K_UP:
        ship.moving_up=True
    elif event.key==pygame.K_SPACE:
        fire_bullet(ai_settings,screen,ship,bullets)
    elif event.key==pygame.K_q:
        sys.exit()

def fire_bullet(ai_settings,screen,ship,bullets):
    if len(bullets)<ai_settings.bullets_allowed:
        new_bullet=Bullet(ai_settings, screen,ship)
        bullets.add(new_bullet)

def check_fail(ai_settings,bullets,stats,target,screen):   #定义一个函数,在主函数中被调用
    for bullet in bullets.copy():   #bullets为其他程序其他地方定义的一个编组
        if bullet.rect.right>=screen.get_rect().right:   #判断bullet对象是否飞出屏幕右侧
            bullets.remove(bullet)    #若bullet飞出右侧,则销毁该bullet
            ai_settings.fail_times+=1    #fail_times自增1
            print(ai_settings.fail_times)    #控制台输出fail_times,用于监控不碰撞的次数
        elif pygame.sprite.spritecollideany(target,bullets):
            ai_settings.fail_times=0    #失败清零``
        elif ai_settings.fail_times>2:
            ai_settings.fail_times=0    #失败清零
            stats.game_active=False    #游戏终止
            pygame.mouse.set_visible(True)    #鼠标可见


def check_keyup_events(event,ship):
    if event.key==pygame.K_DOWN:
        ship.moving_down=False
    elif event.key==pygame.K_UP:
        ship.moving_up=False


def check_events(ai_settings,screen,stats,play_button,ship,target,bullets):
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            sys.exit()
        elif event.type==pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y=pygame.mouse.get_pos()
            check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y)
        elif event.type==pygame.KEYDOWN:
            check_keydown_events(event,ai_settings,screen,ship,bullets)
        elif event.type==pygame.KEYUP:
            check_keyup_events(event,ship)

def check_play_button(ai_settings,screen,stats,play_button,ship,target,bullets,mouse_x,mouse_y):
    button_clicked=play_button.rect.collidepoint(mouse_x,mouse_y)
    if button_clicked and not stats.game_active:
        pygame.mouse.set_visible(False)
        stats.reset_stats()
        stats.game_active=True

        bullets.empty()
        create_fleet(ai_settings,screen,ship,target)
        ship.center_ship()

def update_screen(ai_settings, screen, stats, ship, target,bullets, play_button):
    screen.fill(ai_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    target.draw_target()
    if not stats.game_active:
        play_button.draw_button()
    pygame.display.flip()

def update_bullets(ai_settings, screen, ship, target,bullets):
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.right>screen.get_rect().right:
            bullets.remove(bullet)
    check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets)

def check_bullet_alien_collisions(ai_settings,screen,ship,target,bullets):
    collisions=pygame.sprite.spritecollideany(target,bullets,False)

def create_fleet(ai_settings,screen,ship,target):
    target=Target(ai_settings,screen)

def check_fleet_edges(ai_settings,target):
    if target.check_edges():
        change_fleet_direction(ai_settings,target)

def change_fleet_direction(ai_settings,target):
    ai_settings.fleet_direction*=-1

def update_target(ai_settings,stats,screen,ship,target,bullets):
    check_fleet_edges(ai_settings,target)
    target.update()






 

Released three original articles · won praise 4 · Views 1259

Guess you like

Origin blog.csdn.net/wokaoyan1981/article/details/104030391