Pygame class 坐标(位置)和动画(pyganim)
Pygame class coordinates (position) and animation (pyganim)
太棒了,我正在编写游戏。我遇到了一个问题,两三天都找不到答案:/
我的问题是我正在尝试应用动画,但我无法获得播放器的坐标或位置 (x, y) class...
我的播放器代码:(抱歉,空间不足,直接从我的 .py 文件粘贴:/)
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface = pygame.image.load("PlayerModels\Sprites\PlayerStill.gif")
self.rect = Rect(x, y, 32, 55)
def update(self, up, down, left, right, running, platforms):
if up:
# Pasokti tik ant zemes
if self.onGround: self.yvel -= 10
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -5
if right:
self.xvel = 5
BegimoAnim.play()
if not self.onGround:
# gravitacija + acceleracija
self.yvel += 0.3
# Max kritimo greitis
if self.yvel > 100: self.yvel = 100
if not(left or right):
self.xvel = 0
# Prieaugis X direkcijoje
self.rect.left += self.xvel
# daryti X axis collision
self.collide(self.xvel, 0, platforms)
# Prieaugis Y direkcijoje
self.rect.top += self.yvel
# Ar ore?
self.onGround = False;
# daryti Y axis collision
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
pygame.event.post(pygame.event.Event(QUIT))
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
我需要 blitting 方面的帮助(顺便说一句,我应该 blit 吗?):
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
camera.update(player)
player.update(up, down, left, right, running, platforms)
for e in entities:
screen.blit(e.image, camera.apply(e))
BegimoAnim.blit(screen, (xPlayer, yPlayer)) #help in here, how to?? (BegimoAnim means RunningAnim)
pygame.display.update()
编辑:如果您需要我添加代码的其他部分,请告诉我...
好吧,在更多地探索我的代码并尝试进行各种编码以找到解决我问题的方法之后,我发现我已经制作了一个 pygame 具有位置的玩家值...
太棒了,我正在编写游戏。我遇到了一个问题,两三天都找不到答案:/
我的问题是我正在尝试应用动画,但我无法获得播放器的坐标或位置 (x, y) class...
我的播放器代码:(抱歉,空间不足,直接从我的 .py 文件粘贴:/)
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self, x, y):
Entity.__init__(self)
self.xvel = 0
self.yvel = 0
self.onGround = False
self.image = Surface = pygame.image.load("PlayerModels\Sprites\PlayerStill.gif")
self.rect = Rect(x, y, 32, 55)
def update(self, up, down, left, right, running, platforms):
if up:
# Pasokti tik ant zemes
if self.onGround: self.yvel -= 10
if down:
pass
if running:
self.xvel = 12
if left:
self.xvel = -5
if right:
self.xvel = 5
BegimoAnim.play()
if not self.onGround:
# gravitacija + acceleracija
self.yvel += 0.3
# Max kritimo greitis
if self.yvel > 100: self.yvel = 100
if not(left or right):
self.xvel = 0
# Prieaugis X direkcijoje
self.rect.left += self.xvel
# daryti X axis collision
self.collide(self.xvel, 0, platforms)
# Prieaugis Y direkcijoje
self.rect.top += self.yvel
# Ar ore?
self.onGround = False;
# daryti Y axis collision
self.collide(0, self.yvel, platforms)
def collide(self, xvel, yvel, platforms):
for p in platforms:
if pygame.sprite.collide_rect(self, p):
if isinstance(p, ExitBlock):
pygame.event.post(pygame.event.Event(QUIT))
if xvel > 0:
self.rect.right = p.rect.left
if xvel < 0:
self.rect.left = p.rect.right
if yvel > 0:
self.rect.bottom = p.rect.top
self.onGround = True
self.yvel = 0
if yvel < 0:
self.rect.top = p.rect.bottom
我需要 blitting 方面的帮助(顺便说一句,我应该 blit 吗?):
for y in range(32):
for x in range(32):
screen.blit(bg, (x * 32, y * 32))
camera.update(player)
player.update(up, down, left, right, running, platforms)
for e in entities:
screen.blit(e.image, camera.apply(e))
BegimoAnim.blit(screen, (xPlayer, yPlayer)) #help in here, how to?? (BegimoAnim means RunningAnim)
pygame.display.update()
编辑:如果您需要我添加代码的其他部分,请告诉我...
好吧,在更多地探索我的代码并尝试进行各种编码以找到解决我问题的方法之后,我发现我已经制作了一个 pygame 具有位置的玩家值...