Python肥鸡大战开发记录(十一):敌鸡出现

一、添加Q键结束游戏功能
增添:\game_functions.py - def check_keydown_events(event, fcw_settings, screen, chicken, bullets):

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

二、创建敌鸡(enemy)类
\images\enemy.png:
在这里插入图片描述
新建:\enemy.py:

import pygame
from pygame.sprite import Sprite


class Enemy(Sprite):
    """敌鸡类"""

    def __init__(self, fcw_settings, screen):
        """初始化敌鸡并设置其起始位置"""
        super(Enemy, self).__init__()
        self.screen = screen
        self.fcw_settings = fcw_settings

        # 加载敌鸡图像,并设置rect属性
        self.image = pygame.image.load('images/enemy.png')
        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)

三、创建敌鸡实例
更新:\FatChickenWars.py:

...
from enemy import Enemy
...
	 # 创建一只肥鸡
    chicken = Chicken(screen, fcw_settings)
    # 创建子弹编组
    bullets = Group()

    # 创建一只敌鸡
    enemy = Enemy(fcw_settings, screen)
...
		# 更新画面
        gf.update_screen(fcw_settings, screen, chicken, enemy, bullets)
...

四、让敌鸡出现在屏幕上
更新:\game_functions.py - def update_screen():

def update_screen(fcw_settings, screen, chicken, enemy, bullets):
    """更新画面"""
    # 绘制
    screen.fill(fcw_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_bullet()
    chicken.blitme()
    enemy.blitme()

    # 显示
    pygame.display.flip()

效果极佳
单对单来独打独,我才动手他就哭。(手动滑鸡)

2021.1.22

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 游动-白 设计师:白松林 返回首页