我基本上刚开始用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()
基本上,我只想知道如何将这些草块组成一组精灵,这样当我添加我的玩家(也是一个精灵)时,我可以通过碰撞系统来确定他是否在空中。有人能给我解释一下我该怎么做吗。我要找的基本上是一个启动程序,因为在我看来,有些文档是相当糟糕的,因为它没有完全告诉你如何使用它。非常感谢您的帮助:)
在Pygame中,精灵是非常小的。它们由两个主要部分组成:图像和矩形。图像是显示在屏幕上的内容,rect用于定位和碰撞检测。下面是一个如何将你的草图像制作成精灵的示例:
精灵本身是非常无意义的,因为你可以随时跟踪自己的图像和位置。优势在于使用组。下面是一个如何使用组的示例:
^{pr2}$这段代码创建一个组,向组中添加三个精灵,更新精灵,绘制它们,检查精灵2是否在组中,然后移除精灵。它基本上是直截了当的,但有一些事情需要注意: (一)组.添加()可以接受一个sprite或任何iterable的sprite集合,例如list或tuple。 (二)组.更新()调用它包含的所有精灵的更新方法。Pygame的Sprite在其update方法被调用时不执行任何操作;但是,如果创建Sprite的子类,则可以重写update方法使其执行某些操作。 (三)分组.绘图()将该组所有精灵的图像在矩形的x和y位置快速显示到屏幕上。如果你想移动一个精灵,你可以改变它矩形的x和y的位置如下:}
mySprite.rect.x = 4
或{使用组的一种方法是为每个级别创建不同的组。然后调用表示当前级别的组的update and draw方法。因为除非调用这些方法,否则不会发生任何事情或显示任何内容,所以所有其他级别都将保持“暂停”,直到您切换回它们为止。下面是一个例子:
我知道这个问题有点老,但我希望你还是觉得有用!在
相关问题 更多 >
编程相关推荐