当前位置: 首页>编程语言>正文

[python从入门到实践]外星人入侵源码与知识点分享

一、 知识点

python中如何定义类

class 类名:
#
    def __init__(self):
        初始化代码
    def 其他模块1(self):
        代码1
    def 其他模块2(self):
        代码2

模块/类的导入

  • 导入整个模块
import 模块名/库名
  • 导入模块中的类
from 模块 import 类名

rect对象

1. 基础知识
  • 在pygame中,我们将把飞船和屏幕都作为矩形(rect)处理
  • 原点(0,0)位于屏幕左上角,向右下方移动时,坐标值将增大,如下图所示


    [python从入门到实践]外星人入侵源码与知识点分享,第1张
    rect示意.PNG
  • rect只存储整数部分
2. 常见操作
  1. 获取一张图片的矩形
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
  1. 获取游戏屏幕的矩形
self.screen_rect = screen.get_rect()
  1. 通过游戏屏幕的矩形来规定图片矩形放置的位置
self.rect.midbottom = self.screen_rect.midbottom

精灵与精灵组

精灵:pygame.sprite.Sprite

可以把精灵理解成一个对象,在一个精灵对象中有两个重要的属性,一个是精灵要显示的图像数据,一个是精灵要把这个图像显示在屏幕上的位置

精灵组:pygame.sprite.Group

精灵组是一个包含了多个精灵的对象,在创建精灵组的时候可以使用多值参数的方式,一次性把精灵组中包含的所有精灵传入到精灵组内部.

其他

  • 一个名为settings的模块,在其中包含一个名为Settings的类,要修改游戏,只需修改(接下来将创建的)settings.py中的一些值,而无须查找散布在项目中的各种设置。
  • 选择图像时,要特别注意背景色。请尽可能选择背景为透明或纯色的图像
  • 图像的背景色与游戏的背景色匹配时,游戏看起来最漂亮

二、源码

[python从入门到实践]外星人入侵源码与知识点分享,第2张
外星人入侵整体框架.png

alien.py

import pygame
from pygame.sprite import Sprite

class Alien(Sprite):
    #每一个Sprite为一个小元素
    def __init__(self,ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.image = pygame.image.load('images/alien.bmp')
        #加载外星人图像
        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 check_edges(self):
        screen_rect = self.screen.get_rect()
        if self.rect.right >= screen_rect.right or self.rect.left <= 0:
            #self.rect为外星人的位置  screen_rect为屏幕位置
            return True

    def update(self):
        self.x += (self.settings.alien_speed * self.settings.fleet_direction)
        #x为左右移动
        self.rect.x = self.x

alien_invasion.py

import sys
#用来退出程序
from time import sleep
import pygame
from settings import Settings
from game_stats import GameStats
from scoreboard import Scoreboard
from button import Button
from ship import Ship
from bullet import Bullet
from alien import Alien
#从(文件名)中提取(模块名)

class AlienInvasion:
#整个大类

    def __init__(self):
        # 初始化游戏并创建资源
        pygame.init()
        self.settings = Settings()
        #把Settings中的设置给到本次的游戏
        # 窗口尺寸为 宽1200 高800
        self.screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
        self.settings.screen_width = self.screen.get_rect().width
        self.settings.screen_height = self.screen.get_rect().height
        #可以根据电脑屏幕大小来特定的改变屏幕大小的设置
        pygame.display.set_caption("Alien Invasion")
        #起标题
        self.stats = GameStats(self)
        self.sb = Scoreboard(self)
        self.ship = Ship(self)
        self.bullets = pygame.sprite.Group()
        self.aliens = pygame.sprite.Group()
        self._create_fleet()
        self.play_button = Button(self,"Play")




    def run_game(self):
        while True:
            self._check_events()
            #重新绘制屏幕
            if self.stats.game_active:
                self.ship.update()
                #更新船
                self._update_bullets()
                #更新子弹
                self._update_aliens()
                #更新外星人
            self._update_screen()
            #更新屏幕

    def _check_events(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
                #退出
            elif event.type == pygame.KEYDOWN:
                self._check_keydown_events(event)
                #触发按下键盘的事件
            elif event.type == pygame.KEYUP:
                self._check_keyup_events(event)
                #触发抬起键盘的事件
            elif event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = pygame.mouse.get_pos()
                self._check_play_button(mouse_pos)

    def _check_play_button(self,mouse_pos):
        button_clicked = self.play_button.rect.collidepoint(mouse_pos)
        if button_clicked and not self.stats.game_active:
            self.settings.initialize_dynamic_settings()
            self.stats.reset_stats()
            self.stats.game_active = True
            self.sb.prep_score()
            self.sb.prep_level()
            self.sb.prep_ships()

            self.aliens.empty()
            self.bullets.empty()

            self._create_fleet()
            self.ship.center_ship()
            pygame.mouse.set_visible(False)


    def _check_keydown_events(self,event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = True
        elif event.key == pygame.K_LEFT:
            self.ship.moving_left = True
        elif event.key == pygame.K_q:
            sys.exit()
        elif event.key == pygame.K_SPACE:
            self._fire_bullet()



    def _check_keyup_events(self, event):
        if event.key == pygame.K_RIGHT:
            self.ship.moving_right = False
        if event.key == pygame.K_LEFT:
            self.ship.moving_left = False



    def _update_bullets(self):
        self.bullets.update()
        #更新子弹的位置
        for bullet in self.bullets.copy():
            if bullet.rect.bottom <= 0:
                self.bullets.remove(bullet)
            #如果超过了屏幕的范围就将子弹移除,以免造成处理空间浪费

        self._check_bullet_alien_collisions()


    def _check_bullet_alien_collisions(self):
        collisons = pygame.sprite.groupcollide(self.bullets, self.aliens, True, True)
        #外星人和子弹碰到一起以后,两个都消失
        if collisons:
            for aliens in collisons.values():
                self.stats.score += self.settings.alien_points*len(aliens)
            self.sb.prep_score()
            self.sb._check_high_score()
        #更新得分
        if not self.aliens:
            #如果外星人被消灭干净
            self.bullets.empty()
            self._create_fleet()
            self.settings.increase_speed()

            self.stats.level += 1
            self.sb.prep_level()

    def _update_aliens(self):
        self._check_fleet_edges()
        self.aliens.update()
        if pygame.sprite.spritecollideany(self.ship,self.aliens):
            self._ship_hit()
        self._check_aliens_bottom()

    def _ship_hit(self):
        if self.stats.ships_left > 0:
            self.stats.ships_left -=1
            self.sb.prep_ships()
            self.aliens.empty()
            self.bullets.empty()
            self._create_fleet()
            self.ship.center_ship()
            sleep(0.5)
        else:
            self.stats.game_active = False
            pygame.mouse.set_visible(True)

    def _create_fleet(self):
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        #外星人的宽度和高度
        available_space_x = self.settings.screen_width - (2*alien_width)
        #x可用空间
        number_aliens_x = available_space_x // (2*alien_width)
        #可放置外星人每行数量
        ship_height = self.ship.rect.height
        #船的高度
        available_space_y = (self.settings.screen_height - (3*alien_height)-ship_height)
        #y可用空间
        number_rows = available_space_y //(2*alien_height)
        #可放置外星人每列数量
        for row_number in range(number_rows):
            for alien_number in range(number_aliens_x):
                self._create_alien(alien_number,row_number)
                #每个_create_alien只能一次性创造一个外星人

    def _create_alien(self,alien_number,row_number):
        #第几行第几个
        alien = Alien(self)
        alien_width, alien_height = alien.rect.size
        alien.x = alien_width + 2 * alien_width * alien_number
        alien.rect.x = alien.x
        alien.rect.y = alien.rect.height + 2*alien_height*row_number
        self.aliens.add(alien)

    def _check_fleet_edges(self):
        for alien in self.aliens.sprites():
            if alien.check_edges():
                self._change_fleet_direction()
                break

    def _change_fleet_direction(self):
        for alien in self.aliens.sprites():
            alien.rect.y += self.settings.fleet_drop_speed
            #每个外星人往下平移
        self.settings.fleet_direction *=-1


    def _update_screen(self):
        self.screen.fill(self.settings.bg_color)
        # 屏幕颜色
        # blitme:在指定位置绘制飞船
        self.ship.blitme()
        for bullet in self.bullets.sprites():
            bullet.draw_bullet()
        self.aliens.draw(self.screen)

        self.sb.show_score()

        if not self.stats.game_active:
            self.play_button.draw_button()

        pygame.display.flip()

    def _fire_bullet(self):
        if len(self.bullets) < self.settings.bullet_allowed:
            new_bullet = Bullet(self)
            self.bullets.add(new_bullet)

    def _check_aliens_bottom(self):
        #检查是否有外星人到达了屏幕底端
        screen_rect = self.screen.get_rect()
        for alien in self .aliens.sprites():
            #挨个看是否达到了屏幕底端
            if alien.rect.bottom >= screen_rect.bottom:
                self._ship_hit()
                break

if __name__ == '__main__':
    ai = AlienInvasion()
    ai.run_game()
    #主程序为进行AlienInvasion中的run_game模块

bullet.py

import pygame
from pygame.sprite import  Sprite

class Bullet(Sprite):
    def __init__(self,ai_game):
        super().__init__()
        self.screen = ai_game.screen
        self.settings = ai_game.settings
        self.color = self.settings.bullet_color

        self.rect = pygame.Rect(0,0,self.settings.bullet_width,self.settings.bullet_height)
        self.rect.midtop = ai_game.ship.rect.midtop
        #这个操作的位置为ship的顶端
        self.y = float(self.rect.y)

    def update(self):
        self.y -= self.settings.bullet_speed
        self.rect.y = self.y
        #更新

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

button.py

import pygame.font

class Button:
    def __init__(self,ai_game,msg):
        self.screen = ai_game.screen
        self.screen_rect = self.screen.get_rect()

        self.width,self.height = 200,50
        self.button_color = (0,250,0)
        #亮绿色
        self.text_color = (250,250,250)
        #白色
        self.font = pygame.font.SysFont(None,48)
        #默认字体,字号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)

game_stats.py

class GameStats:
#跟踪游戏统计信息   记录飞船被撞了多少次
    def __init__(self,ai_game):
        self.settings = ai_game.settings
        self.reset_stats()
        self.game_active = False
        self.high_score = 0


    def reset_stats(self):
        self.ships_left = self.settings.ship_limit
        self.score = 0
        self.level = 1

scoreboard.py

import pygame.font
from pygame.sprite import Group
from ship import Ship

class Scoreboard:
    def __init__(self,ai_game):
        self.ai_game = ai_game
        self.screen = ai_game.screen
        self.screen_rect = self.screen.get_rect()
        self.settings = ai_game.settings
        self.stats = ai_game.stats

        self.text_color = (30,30,30)
        self.font = pygame.font.SysFont(None,48)
        self.prep_score()
        self.prep_high_score()
        self.prep_level()
        self.prep_ships()

    def prep_score(self):
        rounded_score = round(self.stats.score,-1)
        score_str = "{:,}".format(rounded_score)
        self.score_image = self.font.render(score_str,True,self.text_color,self.settings.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 prep_level(self):
        """Turn the level into a rendered image."""
        level_str = str(self.stats.level)
        self.level_image = self.font.render(level_str, True,self.text_color, self.settings.bg_color)

        # Position the level below the score.
        self.level_rect = self.level_image.get_rect()
        self.level_rect.right = self.score_rect.right
        self.level_rect.top = self.score_rect.bottom + 10


    def show_score(self):
        self.screen.blit(self.score_image,self.score_rect)
        self.screen.blit(self.high_score_image,self.high_score_rect)
        self.screen.blit(self.level_image,self.level_rect)
        self.ships.draw(self.screen)

    def prep_high_score(self):
        high_score = round(self.stats.high_score,-1)
        high_score_str = "{:,}".format(high_score)
        self.high_score_image = self.font.render(high_score_str,True,self.text_color,self.settings.bg_color)

        self.high_score_rect = self.high_score_image.get_rect()
        self.high_score_rect.centerx = self.screen_rect.centerx
        self.high_score_rect.top = self.score_rect.top

    def _check_high_score(self):
        if self.stats.score > self.stats.high_score:
            self.stats.high_score = self.stats.score
            self.prep_high_score()

    def prep_ships(self):
        """Show how many ships are left."""
        self.ships = Group()
        for ship_number in range(self.stats.ships_left):
            ship = Ship(self.ai_game)
            ship.rect.x = 10 + ship_number * ship.rect.width
            ship.rect.y = 10
            self.ships.add(ship)

settings.py

class Settings:
    #属于属性设置类
    def __init__(self):
        self.screen_width = 1200
        self.screen_height = 800
        self.bg_color =(230,230,230)
        #屏幕的长宽和背景色(屏幕的属性)


        self.ship_limit = 3
        #(飞船的一些属性)


        self.bullet_width = 3
        self.bullet_height = 15
        self.bullet_color = (60,60,60)
        self.bullet_allowed = 3
        #子弹相关的一些属性


        self.fleet_drop_speed = 10

        self.speedup_scale = 1.1
        self.score_scale = 1.5
        self.initialize_dynamic_settings()

    def initialize_dynamic_settings(self):
        self.ship_speed = 1.5
        self.bullet_speed = 3
        self.alien_speed = 1.0
        # 外星人的一些属性
        self.fleet_direction = 1
        #为1表示向右 为-1表示向左
        self.alien_points = 50

    def increase_speed(self):
        self.ship_speed *= self.speedup_scale
        self.bullet_speed *= self.speedup_scale
        self.alien_speed *= self.speedup_scale

        self.alien_points = int(self.alien_points * self.score_scale)

ship.py

import pygame
from pygame.sprite import Sprite

class Ship(Sprite):
    #船相关的
    def __init__(self,ai_game):
        super().__init__()
        self.screen = ai_game.screen
        #屏幕
        self.settings = ai_game.settings
        #获取设置中的内容
        self.screen_rect = ai_game.screen.get_rect()
        #self.screen_rect获取屏幕信息
        self.image = pygame.image.load('images/ship.bmp')
        #加载图片
        self.rect = self.image.get_rect()
        #获取图像的位置
        self.rect.midbottom = self.screen_rect.midbottom
        #矩形的中下为屏幕的中下
        self.x = float(self.rect.x)
        #操作的x等于矩形的x

        self.moving_right = False
        self.moving_left = False
        #开始没有行动时定义左右移动为False


    def update(self):
        #运动都只是在x轴上进行移动
        if self.moving_right and self.rect.right < self.screen_rect.right:
            self.x += self.settings.ship_speed
        if self.moving_left and self.rect.left > 0:
            self.x -= self.settings.ship_speed
        self.rect.x = self.x
        #更新操作对象

    def blitme(self):
    #在制定位置绘制飞船 self.rect定义住了位置,self.image为图像
        self.screen.blit(self.image,self.rect)

    def center_ship(self):
        #死亡后更新
        self.rect.midbottom = self.screen_rect.midbottom
        self.x = float(self.rect.x)
   

https://www.xamrdz.com/lan/5e41882729.html

相关文章: