Pygame基础2-精灵类 Sprite

简介: Pygame基础2-精灵类 Sprite

2. 精灵类

原理

精灵(sprite) 是一个古老的术语,用来描述游戏中的各种图像

PyGame中,精灵类(Sprite) 是一个常用的类。精灵类有以下好处/用处:

  • 更好地表示游戏中的角色
  • 通过Group同时管理游戏中的多个精灵。
  • 更容易进行碰撞检测

在Pygame中,我们使用surface表示图形,用rect 移动图形。精灵类将两者结合到一个类中,这样我们就可以方便地绘制和移动图形。

在Pygame中,想要创建一个精灵类,只需要

  • 继承pygame.sprite.Sprite
  • 初始化函数中定义self.imageself.rect
  • 推荐编写update方法实现对象的更新

下面是一个例子,实现了一个随着鼠标移动的准星对象。

# 射击的准星
class Crosshair(pygame.sprite.Sprite):
    def __init__(self, img_path):
        super().__init__()
        self.image = pygame.image.load(img_path)
        self.rect = self.image.get_rect()
        self.gun_shot = pygame.mixer.Sound('gunshot.wav')
    def shoot(self):
        self.gun_shot.play()
        # 碰撞检测
        pygame.sprite.spritecollide(crosshair, target_group, True)
    def update(self):
        self.rect.center = pygame.mouse.get_pos()

当然,Crosshair作为一个类,除了上面说的必须的属性和方法,也可以添加任何你需要的属性和方法。我们添加了self.gun_shot表示射击声音,添加了shoot()方法表示射击动作。


注意在shoot()方法中,我们首先播放了射击声音,然后调用

pygame.sprite.spritecollide(crosshair, target_group, True) 进行碰撞检测,清除了射击到的靶子。


检测一个group中的一个精灵和另一个group的所有精灵的碰撞:

pygame.sprite.spritecollide(sprite, group, dokill, collided = None) -> Sprite_list

创建出精灵后,我们需要用Group 管理精灵:

# 为每个类别创建一个Group
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)

精灵必须属于某个Group。即使Group里只有一个精灵。

在主函数中,我们通过Group 更新和绘制 其中的所有精灵。

while True:
  ...
  crosshair_group.update()
    crosshair_group.draw(screen)


案例

使用精灵类创建一个射击游戏。

  1. 创建射击的准星Crosshair
  2. 创建射击的靶子Target
  3. 为每个类别创建一个Group
  4. 利用精灵类的碰撞检测实现射击效果。

使用的图片素材是在opengameart.org中下载的。

# 使用pygame SPrite实现射击游戏
import pygame
import random

# 射击的准星
class Crosshair(pygame.sprite.Sprite):
    def __init__(self, img_path):
        super().__init__()
        self.image = pygame.image.load(img_path)
        self.rect = self.image.get_rect()
        self.gun_shot = pygame.mixer.Sound('gunshot.wav')
    def shoot(self):
        self.gun_shot.play()
        # 碰撞检测
        pygame.sprite.spritecollide(crosshair, target_group, True)
    def update(self):
        self.rect.center = pygame.mouse.get_pos()
# 射击的靶子
class Target(pygame.sprite.Sprite):
    def __init__(self, img_path, x, y):
        super().__init__()
        self.image = pygame.image.load(img_path)
        self.rect = self.image.get_rect()
        self.rect.center = (x, y)
        
    def update(self):
        pass

# init 
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.mouse.set_visible(False)
clock = pygame.time.Clock()
crosshair = Crosshair(r'PNG\HUD\crosshair_blue_small.png')

# 为每个类别创建一个Group
crosshair_group = pygame.sprite.Group()
crosshair_group.add(crosshair)

target_group = pygame.sprite.Group()
for i in range(20):
    x,y = random.randint(0, screen_width), random.randint(0, screen_height)
    target = Target(r'PNG\Objects\duck_target_yellow.png', x, y)
    target_group.add(target)

bg_img = pygame.image.load(r'PNG\Stall\bg_wood.png')
bg_img = pygame.transform.scale(bg_img, (screen_width, screen_height))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            exit()
        if event.type == pygame.MOUSEBUTTONDOWN:
            crosshair.shoot()

    screen.blit(bg_img, (0, 0))

    target_group.update()
    target_group.draw(screen)
    crosshair_group.update()
    crosshair_group.draw(screen)

    pygame.display.flip()
    clock.tick(60)


相关文章
|
13天前
|
Python 容器
Pygame入门 2022 (4) 使用精灵类重构
Pygame入门 2022 (4) 使用精灵类重构
|
12天前
|
Python
使用Pygame做一个乒乓球游戏(2)使用精灵重构
使用Pygame做一个乒乓球游戏(2)使用精灵重构
|
Python
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏06之死亡后游戏重新开始
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏06之死亡后游戏重新开始
147 0
|
Python 容器
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏05之滚动屏幕
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏05之滚动屏幕
118 0
|
Python
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏04之跳跃
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏04之跳跃
118 0
|
Python
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏03之重力及碰撞检测
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏03之重力及碰撞检测
147 0
|
Python
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏02之物体运动
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏02之物体运动
101 0
|
Python
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏01之Pygame游戏模板
通过游戏学Python系列之小兔要上天---手把手教你使用Pygame开发平台跳跃类游戏01之Pygame游戏模板
85 0
|
Python
python及pygame雷霆战机游戏项目实战02 敌人精灵
python及pygame雷霆战机游戏项目实战02 敌人精灵
90 0
|
Python Windows
Pygame第1-3课:图片精灵
Pygame第1-3课:图片精灵
108 0

热门文章

最新文章