【问题标题】:Pygame sprite assistancePygame 精灵辅助
【发布时间】:2013-06-02 02:21:26
【问题描述】:

我基本上刚刚开始使用 PyGame 进行开发,但我在整个 Sprite 概念方面遇到了麻烦。我一直在到处寻找有关如何使用它的指南,但似乎找不到任何东西。我想知道这一切如何运作的基本概念。这是我一直在处理的代码:

#!/usr/bin/python

import pygame, sys
from pygame.locals import *

size = width, height = 320, 320
clock = pygame.time.Clock()

xDirection = 0
yDirection = 0
xPosition = 32
yPosition = 256

blockAmount = width/32

pygame.init()
screen = pygame.display.set_mode(size)
screen.fill([0, 155, 255])
pygame.display.set_caption("Mario Test")
background = pygame.Surface(screen.get_size())

mainCharacter = pygame.sprite.Sprite()
mainCharacter.image = pygame.image.load("data/character.png").convert()
mainCharacter.rect = mainCharacter.image.get_rect()
mainCharacter.rect.topleft = [xPosition, yPosition]
screen.blit(mainCharacter.image, mainCharacter.rect)

grass = pygame.sprite.Sprite()
grass.image = pygame.image.load("data/grass.png").convert()
grass.rect = grass.image.get_rect()
for i in range(blockAmount):
    blockX = i * 32
    blockY = 288
    grass.rect.topleft = [blockX, blockY]
    screen.blit(grass.image, grass.rect)

grass.rect.topleft = [64, 256]  
screen.blit(grass.image, grass.rect.topleft )

running = False
jumping = False 
falling = False
standing = True

jumpvel = 22
gravity = -1

while True:

    for event in pygame.event.get():
        if event.type == KEYDOWN and event.key == K_ESCAPE:
            pygame.quit()
            sys.exit()
        elif event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                running = True
                xRun = -5
            elif event.key == K_RIGHT:
                running = True
                xRun = 5
            elif event.key == K_UP or event.key == K_SPACE:
                jumping = True
        elif event.type == KEYUP:
            if event.key == K_LEFT or event.key == K_RIGHT:
                running = False


    if running == True:
        xPosition += xRun

    if mainCharacter.rect.right >= width:
        xPosition = xPosition - 10
        print "hit"
        running = False
    elif mainCharacter.rect.left <= 0:
        xPosition = xPosition + 10
        print "hit"
        running = False

    screen.fill([0, 155, 255])

    for i in range(blockAmount):
        blockX = i * 32
        blockY = 288
        grass.rect.topleft = [blockX, blockY]
        screen.blit(grass.image, grass.rect)

    grass.rect.topleft = [64, 64]   
    screen.blit(grass.image, grass.rect.topleft )


    if jumping:
        yPosition -= jumpvel
        print jumpvel
        jumpvel += gravity
        if jumpvel < -22:
            jumping = False
        if mainCharacter.rect.bottom == grass.rect.top:
            jumping = False

    if not jumping:
        jumpvel = 22


    mainCharacter.rect.topleft = [xPosition, yPosition]
    screen.blit(mainCharacter.image,mainCharacter.rect)

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

基本上我只是想知道如何将这些草块制作成一组精灵,这样当我添加我的玩家(也是精灵)时,我可以通过碰撞系统确定他是否在空中.有人可以向我解释我将如何做到这一点。我所寻找的基本上只是一个启动器,因为在我看来,有些文档非常糟糕,因为它并没有完全告诉你如何使用它。非常感谢任何帮助:)

【问题讨论】:

    标签: python python-2.7 pygame


    【解决方案1】:

    在 Pygame 中,精灵非常少。它们由两个主要部分组成:图像和矩形。图像是屏幕上显示的内容,矩形用于定位和碰撞检测。下面是如何将您的草图像制作成 Sprite 的示例:

    grass = pygame.image.load("grass.png")
    grass = grass.convert_alpha()
    grassSprite = new pygame.sprite.Sprite()
    grassSprite.image = grass
    #This automatically sets the rect to be the same size as your image.
    grassSprite.rect = grass.get_rect()
    

    Sprite 本身毫无意义,因为您始终可以自己跟踪图像和位置。优点是使用组。以下是如何使用组的示例:

    myGroup = pygame.sprite.Group()
    myGroup.add([sprite1,sprite2,sprite3])
    myGroup.update()
    myGroup.draw()
    if myGroup.has(sprite2):
        myGroup.remove(sprite2)
    

    这段代码创建了一个组,将三个精灵添加到组中,更新精灵,绘制它们,检查 sprite2 是否在组中,然后删除精灵。它大多是直截了当的,但有一些事情需要注意: 1) Group.add() 可以采用单个精灵或任何可迭代的精灵集合,例如列表或元组。 2) Group.update() 调用它包含的所有精灵的更新方法。 Pygame 的 Sprite 在其 update 方法被调用时不做任何事情;但是,如果您创建 Sprite 的子类,则可以覆盖 update 方法以使其执行某些操作。 3) Group.draw() 将所有 Group 的 Sprites 的图像在其 rects 的 x 和 y 位置上blit到屏幕上。如果你想移动一个精灵,你可以改变它的 rect 的 x 和 y 位置,如下所示:mySprite.rect.x = 4mySprite.rect.y -= 7

    使用组的一种方法是为每个级别创建不同的组。然后调用代表当前级别的任何组的更新和绘制方法。由于除非调用这些方法,否则不会发生或显示任何内容,因此所有其他级别将保持“暂停”状态,直到您切换回它们。这是一个例子:

    levels = [pygame.sprite.Group(),pygame.sprite.Group(),pygame.sprite.Group()]
    levels[0].add(listOfLevel0Sprites)
    levels[1].add(listOfLevel1Sprites)
    levels[2].add(listOfLevel2Sprites)
    currentLevel = 0
    while(True):
        levels[currentLevel].update()
        levels[currentLevel].draw()
    

    我知道这个问题有点老了,但我希望你仍然觉得它有帮助!

    【讨论】:

    • groups 用于组碰撞检测,而不是用于级别管理。
    • 没错,它们可以用于碰撞检测,尽管我相信它们使用一种简单的算法来检查每个精灵与其他精灵。尽管如此,它们还是提供了对更新和绘图以及碰撞的控制——我只是想举一个例子来说明它们可以应用的广泛应用范围。诚然,您可能想要更多地控制关卡管理,但您也可能想要更多地控制碰撞检测。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2023-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多