猿问

pygame精灵协助

我基本上才刚开始使用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


基本上,我只是想知道如何将这些草块组合成一个精灵,这样当我添加播放器(也是精灵)时,我可以通过碰撞系统确定他是否在空中。有人可以向我解释我将如何做。我所寻找的基本上只是一个启动器,因为我认为某些文档非常糟糕,因为它不能完全告诉您如何使用它。任何帮助是极大的赞赏 :)


LEATH
浏览 229回答 1
1回答
随时随地看视频慕课网APP

相关分类

Python
我要回答