Python-- project - game 3- elf? The Smurfs?

Wait, we just seem to draw a moving Photo

I do not know you have not found, it seems we just drew a moving map, only one! ! ! So much code, if there are 10,000 + 100 sheets? That we are so high?

Do not worry, pygame provides us with solutions ------ Elf Elf group as well

Elves? Elf group? The Smurfs? Pikachu?

Genius

Display images in game development object is the sprites

  1. Do not worry we first look, it really is a class, its class diagram is here

  1. effect:
    • pygame.sprite.Sprite- storing image data image and position rect of objects **
    • pygame.sprite.Group, used to store pygame.sprite.Sprite out before the object is created, unified draw in the main program window
  2. Analyze the constitution of this class
    • Elf need to have two important properties
    • imageThe image to be displayed, rectthe image to be displayed on the screen position
    • The default update()method did not do anything, subclass can override this method, each time the screen is refreshed, the location update wizard
  3. Be careful pit!
    • pygame.sprite.SpriteIt does not provide image, and recttwo properties
    • Programmers need from the pygame.sprite.Spritesubclass
    • And the subclass of the initialization method , set imageand rectproperties

Elf group

  1. A sprite group may comprise a plurality of sprite objects
  2. Calls Elf group object's update()method
    • It can automatically invoke a spirit within each group of update()methods
  3. Calls Elf group object's draw(屏幕对象)method
    • May be each a group of the sprite is imagedrawn rectposition
  4. In general unified command is unity of action, but each item has its own independent movement of the methods and properties
Group(*sprites) -> Group

Example code:

# 1. 新建 `plane_sprites.py` 文件
# 2. 定义 `GameSprite` 继承自 `pygame.sprite.Sprite`
import pygame

# 3.写在括号里的意思是继承父类
class GameSprite(pygame.sprite.Sprite):
    """飞机大战中的游戏精灵,根据设计的UML编写代码"""
#注意这里要重写init方法,我们在init初始化方法(行函数)传参
    def __init__(self, image_name, speed=1):

        # 调用父类的初始化方法,当我们的父类不是object基类的时候一定要调用super()对象来调用父类的初始化inint方法
        super().__init__()

        # 定义对象的属性,它们分别记录着精灵的图片位置速度还有运动方式
        # 加载图像
        self.image = pygame.image.load(image_name)
        # 设置尺寸
        self.rect = self.image.get_rect()
        # 记录速度
        self.speed = speed

    def update(self):

        # 在屏幕的垂直方向上移动
        self.rect.y += self.speed

How to use them in the main program before us?

what? You do not know what our main program? Well, I forgot to tell you, that is our main program window to draw before we write,

In fact, this is also very simple, you just need to import the package in the game master
how to finish adding the "enemy" Wizard, and let it move, then they put the game on the line inside the loop

  1. note:
我们先来明确一下精灵还有精灵组的分工
1. 精灵
    * 封装 **图像 image**、**位置 rect** 和 **速度 speed**
    * 提供 `update()` 方法,根据游戏需求,**更新位置 rect**
2. 精灵组
    * 包含 **多个** **精灵对象**
    * `update` 方法,让精灵组中的所有精灵调用 `update` 方法更新位置
    * `draw(screen)` 方法,在 `screen` 上绘制精灵组中的所有精灵
  1. Complete code examples
import pygame
# 你需要导入 form和import 使用inprom导入的时候需要.来使用 使用form的时候 直接使用模块提供的工具就行了。
# 它们都是实现了导入模块的功能
from plane_sprites import *


# 游戏的初始化
pygame.init()

# 创建游戏的窗口 480 * 700
screen = pygame.display.set_mode((480, 700))

# 绘制背景图像
bg = pygame.image.load("./images/background.png")
screen.blit(bg, (0, 0))
# pygame.display.update()

# 绘制英雄的飞机
hero = pygame.image.load("./images/me1.png")
screen.blit(hero, (150, 300))

# 可以在所有绘制工作完成之后,统一调用update方法
pygame.display.update()

# 创建时钟对象
clock = pygame.time.Clock()

# 1. 定义rect记录飞机的初始位置
hero_rect = pygame.Rect(150, 300, 102, 126)



# 开始我们的业务逻辑
# 创建敌机的精灵
# 敌人的战斗机对象
enemy = GameSprite("./images/enemy1.png")
enemy1 = GameSprite("./images/enemy1.png", 2)
# 创建敌机的精灵组,我们可以使用多值的方式传递精灵组合,有了这个精灵组,我们就可以直接使用精灵的方法了,一次性的绘制所有的图像
enemy_group = pygame.sprite.Group(enemy, enemy1)

    # # 让精灵组调用两个方法
    # # update - 让组中的所有精灵更新位置,这个是更新位置
    # enemy_group.update()

    # # draw - 在screen上绘制所有的精灵,这个是绘制
    # enemy_group.draw(screen)
    # 我们只是调用一次,现在我们把它丢到我们的游戏循环里面去

# 游戏循环 -> 意味着游戏的正式开始!
while True:

    # 可以指定循环体内部的代码执行的频率
    clock.tick(60)

    # 监听事件
    for event in pygame.event.get():

        # 判断事件类型是否是退出事件
        if event.type == pygame.QUIT:
            print("游戏退出...")

            # quit 卸载所有的模块
            pygame.quit()

            # exit() 直接终止当前正在执行的程序
            exit()

    # 2. 修改飞机的位置
    hero_rect.y -= 1

    # 判断飞机的位置
    if hero_rect.y <= 0:
        hero_rect.y = 700

    # 3. 调用blit方法绘制图像
    screen.blit(bg, (0, 0))
    screen.blit(hero, hero_rect)

    # 让精灵组调用两个方法
    # update - 让组中的所有精灵更新位置,这个是更新位置
    enemy_group.update()

    # draw - 在screen上绘制所有的精灵,这个是绘制
    enemy_group.draw(screen)


    # 4. 调用update方法更新显示
    pygame.display.update()

pygame.quit()

Guess you like

Origin www.cnblogs.com/BM-laoli/p/12551439.html