星际争霸之小霸王之小蜜蜂(十五)--剧将终场

系列文章目录

星际争霸之小霸王之小蜜蜂(十四)--资本家的眼泪

星际争霸之小霸王之小蜜蜂(十三)--接着奏乐接着舞

星际争霸之小霸王之小蜜蜂(十二)--猫有九条命

 星际争霸之小霸王之小蜜蜂(十一)--杀杀杀

 星际争霸之小霸王之小蜜蜂(十)--鼠道

星际争霸之小霸王之小蜜蜂(九)--狂鼠之灾

 星际争霸之小霸王之小蜜蜂(八)--蓝皮鼠和大脸猫

  星际争霸之小霸王之小蜜蜂(七)--消失的子弹

星际争霸之小霸王之小蜜蜂(六)--让子弹飞

 星际争霸之小霸王之小蜜蜂(五)--为小蜜蜂降速

 星际争霸之小霸王之小蜜蜂(四)--事件监听-让小蜜蜂动起来


目录

系列文章目录

文章目录

前言

二、整体分析

 alien_invasion.py

 game_functions.py

 alien.py

 bullet.py

 settings.py

 button.py

 game_stats.py

 scoreboard.py

 ship.py

总结


前言

我们已经基本完成了书上的所有操作,共用了14天学习,今天我们进行整理,将完整代码整理出来,供大家学习,也为今后我自己调阅。


一、心得体会

经过这段时间的学习,可以说浅显了解了pygame这个模块的一些函数使用,确实针对游戏的设计方面提供了很便利的支持。初步掌握了这些方法,我们可以很方便的作出一些其他的游戏。比如扫雷游戏,通过鼠标点击判断,再比如说贪食蛇,主要核心应该是碰撞的监测。至于纸牌或是其他的游戏目前还没有想好。

 除了接触到一些新的函数,还学习了面对对象编程的方法,虽然不在公司上班,只是个人的小打小闹,但是先进的编程理念学起来肯定没有错。同时还感受到了规范的命名规则,对于我来说有点困难,因为英语不行,所以这个命名会大大降低我的编写速度,在调用函数时,要反复的看单词怎么写,太浪费时间了。

二、整体分析

这次我们总共写了9个类,其实还是有规律的,不同的对象各自建一个类,比如说小花猫、小老鼠和子弹。然后需要修改的参数放在一个类,事件监听放在一个类等等,现在将所有的类代码整理如下:

 alien_invasion.py

import pygame
import settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
from alien import Alien
from game_stats import GameStats
from button import Button
from scoreboard import Scoreboard

def run_game():
    pygame.init()
    new_setting=settings.Settings()
    screen = pygame.display.set_mode((new_setting.screen_width,new_setting.screen_height))
    ship = Ship(screen,new_setting)
    alien = Alien(new_setting,screen)
    pygame.display.set_caption("狂敲代码的橘子")
    bullets = Group()
    aliens = Group()
    gf.create_fleet(new_setting,screen,aliens)
    stats = GameStats(new_setting)
    play_button = Button(new_setting,screen,'Play')
    sb = Scoreboard(new_setting,screen,stats)

    while True:
        gf.check_events(new_setting,screen,ship,bullets,stats,play_button,aliens,sb)
        if stats.game_active:
            ship.update()
            gf.update_bullets(new_setting,screen,bullets,aliens,stats,sb)
            gf.update_aliens(new_setting,stats,screen,ship,aliens,bullets)
        gf.update_screen(new_setting,screen,stats,ship,bullets,aliens,play_button,sb)


run_game()

 game_functions.py

import sys
import pygame
import time
from bullet import Bullet
from alien import Alien


def check_keydown_events(event,new_setting,screen,ship,bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True

    elif event.key == pygame.K_LEFT:
        ship.moving_left = True

    elif event.key == pygame.K_SPACE:
        fire_bullet(new_setting, screen, ship, bullets)

    elif event.key == pygame.K_q:
        sys.exit()


def check_keyup_events(event,ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False

    if event.key == pygame.K_LEFT:
        ship.moving_left = False


def check_events(new_setting,screen,ship,bullets,stats,play_button,aliens,sb):
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type==pygame.KEYDOWN:
            check_keydown_events(event,new_setting,screen,ship,bullets)

        elif event.type==pygame.KEYUP:
            check_keyup_events(event, ship)

        elif event.type == pygame.MOUSEBUTTONDOWN:
            mouse_x,mouse_y = pygame.mouse.get_pos()
            check_play_button(stats,play_button,mouse_x,mouse_y,new_setting,aliens,bullets,ship,screen,sb)

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

        aliens.empty()
        bullets.empty()
        sb.prep_score()
        new_setting.initialize_dynamic_settings()

        create_fleet(new_setting,screen,aliens)
        ship.center_ship()

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


def update_bullets(new_setting,screen,bullets,aliens,stats,sb):
    check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb)
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)

def check_bullet_alien_collisions(new_setting,screen,bullets,aliens,stats,sb):
    collisions = pygame.sprite.groupcollide(bullets, aliens, True, True)
    if collisions:
        for aliens in collisions.values():
            stats.score += len(aliens)
            sb.prep_score()
    if len(aliens) == 0:
        bullets.empty()
        new_setting.increase_speed()
        create_fleet(new_setting, screen, aliens)


def create_fleet(new_setting,screen,aliens):
    for row_number in range(3):
        for alien_number in range(6):
            alien = Alien(new_setting,screen)
            alien.x = alien.rect.width+2*alien.rect.width*alien_number
            alien.rect.x = alien.x
            alien.rect.y = alien.rect.height+2*alien.rect.height*row_number
            aliens.add(alien)

def update_screen(new_setting,screen,stats,ship,bullets,aliens,play_button,sb):
    screen.fill(new_setting.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    ship.blitme()
    aliens.draw(screen)
    sb.show_score()
    if not stats.game_active:
        play_button.draw_button()
    pygame.display.flip()

def ship_hit(new_setting,stats,screen,ship,aliens,bullets):
    stats.ships_left -= 1
    aliens.empty()
    bullets.empty()
    create_fleet(new_setting, screen, aliens)
    ship.center_ship()
    if stats.ships_left > 0:
        stats.ships_left -= 1
        time.sleep(0.5)
    else:
        stats.game_active = False
        pygame.mouse.set_visible(True)


def check_aliens_bottom(new_setting,stats,screen,ship,aliens,bullets):
    screen_rect = screen.get_rect()
    for alien in aliens.sprites():
        if alien.rect.bottom >= screen_rect.bottom:
            ship_hit(new_setting,stats,screen,ship,aliens,bullets)
            break


def update_aliens(new_setting,stats,screen,ship,aliens,bullets):
    check_fleet_edges(new_setting,aliens)
    aliens.update()
    if pygame.sprite.spritecollideany(ship,aliens):
        ship_hit(new_setting, stats, screen, ship, aliens, bullets)
    check_aliens_bottom(new_setting, stats, screen, ship, aliens, bullets)


def change_fleet_direction(new_setting,aliens):
    for alien in aliens.sprites():
        alien.rect.y += new_setting.fleet_drop_speed
    new_setting.fleet_direction *=-1


def check_fleet_edges(new_setting,aliens):
    for alien in aliens.sprites():
        if alien.check_edges():
            change_fleet_direction(new_setting,aliens)
            break

 alien.py

import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    def __init__(self,new_setting,screen):
        super(Alien,self).__init__()
        self.screen = screen
        self.new_setting = new_setting

        self.image = pygame.image.load('mouse.jpg')
        self.rect = self.image.get_rect()

        self.rect.x = self.rect.width
        self.rect.y = self.rect.height

        self.x = float(self.rect.x)

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


    def update(self):
        self.x += (self.new_setting.alien_speed_factor*self.new_setting.fleet_direction)
        self.rect.x = self.x


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

 bullet.py

import pygame
from pygame.sprite import Sprite

class Bullet(Sprite):

    def __init__(self,new_setting,screen,ship):
        super(Bullet,self).__init__()
        self.screen = screen

        self.rect = pygame.Rect(0,0,new_setting.bullet_width,new_setting.bullet_hight)
        self.rect.centerx = ship.rect.centerx
        self.rect.top = ship.rect.top
        self.y = float(self.rect.y)
        self.color =  new_setting.bullet_color
        self.speed_factor = new_setting.bullet_speed_factor

    def update(self):
        self.y -=self.speed_factor
        self.rect.y =self.y

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

 settings.py

class Settings():

    def __init__(self):

        self.screen_width = 800
        self.screen_height = 600
        self.bg_color = (255,255,255)
        self.ship_limit = 2


        self.bullet_width = 2
        self.bullet_hight = 5
        self.bullet_color = 60,60,60
        self.bullets_allowed = 20

        self.fleet_drop_speed = 30


        self.speedup_scale = 1.2

        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.ship_speed_factor = 0.1
        self.bullet_speed_factor = 0.5
        self.alien_speed_factor = 0.5
        self.fleet_direction = 1


    def increase_speed(self):
        self.ship_speed_factor *= self.speedup_scale
        self.bullet_speed_factor *= self.speedup_scale
        self.alien_speed_factor *= self.speedup_scale

 button.py

import pygame.font

class Button():
    def __init__(self,new_setting,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)

        #创建按钮的rect对象
        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)

 game_stats.py

class GameStats():
    def __init__(self,new_setting):
        self.new_setting = new_setting
        self.reset_stats()
        self.game_active = False


    def reset_stats(self):
        self.ships_left = self.new_setting.ship_limit
        self.score = 0

 scoreboard.py

import pygame.font

class Scoreboard():

    def __init__(self,new_setting,screen,stats):
        self.screen = screen
        self.screen_rect = screen.get_rect()
        self.new_setting  = new_setting
        self.stats = stats

        self.text_color = (30,30,30)
        self.font = pygame.font.SysFont(None,48)

        self.prep_score()

    def prep_score(self):
        score_str = str(self.stats.score)
        self.score_image =  self.font.render(score_str,True,self.text_color,self.new_setting.bg_color)

        self.score_rect = self.score_image.get_rect()
        self.score_rect.right = self.screen_rect.right-20
        self.score_rect.top = 20

    def show_score(self):
        self.screen.blit(self.score_image,self.score_rect)

 ship.py

import pygame

class Ship():
    def __init__(self,screen,new_settings):
        self.screen = screen
        self.image = pygame.image.load('cat.png')
        self.rect = self.image.get_rect()
        self.screen_rect = screen.get_rect()
        self.rect.centerx = self.screen_rect.centerx
        self.rect.bottom=self.screen_rect.bottom
        self.new_settings = new_settings

        self.moving_right = False
        self.moving_left = False
        self.center = float(self.rect.centerx)

    def update(self):
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.center += self.new_settings.ship_speed_factor

        if self.moving_left and self.rect.left > 0:
            self.center -= self.new_settings.ship_speed_factor

        self.rect.centerx = self.center

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


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

总结

后面我准备将游戏在改一改,时代在进步,游戏画面我是无能为力,但是游戏机制可以改一改,这种老土的游戏机制我实在玩不下去,不符合我们现在玩的飞机大战,将小花猫的行动轨迹改一改,可以全屏移动,然后将小老鼠也改成不规则的,而不是集体向下 向右,如果有可能的话,改成躲避类,最后小老鼠也可以放子弹。

你可能感兴趣的:(python游戏之猫鼠大战,pygame,python,开发语言,实例,笔记)