如何在pygame中更新精灵位置?
我一直在尝试让这个功能正常运作,已经看了很多其他的问题,但还是搞不定。
我想让精灵在按下箭头键时移动,但什么都没有发生。
精灵确实被画出来了,但就只有这一点。
这是我的代码。
import pygame, sys
from pygame.locals import *
from data import *
def main():
def draw_map(Map):
for row in range(MAPHEIGHT):
#loop through each column in the row
for column in range(MAPWIDTH):
#draw the resource at that position in the tilemap, using the correct colour
pygame.draw.rect(screen, colours[Map[row][column]], (column*TILESIZE,row*TILESIZE,TILESIZE,TILESIZE))
def stop():
pygame.quit()
sys.exit()
tilemap = [
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[white, white, white, white, white, blue, blue, white, white, white],
[white, white, white, white, white, white, white, white, white, white],
[green, green, green, green, green, green, green, green, green, green]
]
entities = pygame.sprite.Group()
class Entity(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
class Player(Entity):
def __init__(self):
Entity.__init__(self)
self.playerx = 50
self.playery = 50
self.image = pygame.image.load("p3_front.png")
self.rect = Rect(self.playerx, self.playery, 66, 92)
player = Player()
pygame.init()
# Set the width and height of the screen
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Game")
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
entities.add(player)
# -------- Main Program Loop -----------
while True:
# ALL EVENT PROCESSING SHOULD GO BELOW THIS COMMENT
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
stop()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_UP]: player.playery -= 3
if pressed[pygame.K_DOWN]: player.playery += 3
if pressed[pygame.K_LEFT]: player.playerx -= 3
if pressed[pygame.K_RIGHT]: player.playerx += 3
# ALL EVENT PROCESSING SHOULD GO ABOVE THIS COMMENT
# ALL GAME LOGIC SHOULD GO BELOW THIS COMMENT
# ALL GAME LOGIC SHOULD GO ABOVE THIS COMMENT
# ALL CODE TO DRAW SHOULD GO BELOW THIS COMMENT
# First, clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
screen.fill(white)
draw_map(tilemap)
entities.update()
entities.draw(screen)
# ALL CODE TO DRAW SHOULD GO ABOVE THIS COMMENT
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Limit to 20 frames per second
clock.tick(20)
stop()
if __name__ == "__main__":
main()
1 个回答
2
entities.update()
这个方法其实就是在调用它里面每个类的 update
方法。所以,你需要在你的 Player
类里创建一个方法,用来更新你想要的角色图像。对于 Player
类,这个方法可能会像这样:
class Player(Entity):
def __init__(self):
Entity.__init__(self)
self.playerx = 50
self.playery = 50
self.image = pygame.image.load("p3_front.png")
self.rect = Rect(self.playerx, self.playery, 66, 92)
def update(self):
self.rect = Rect(self.playerx, self.playery, 66, 92)
我建议你看看 这个链接。里面对 pygame
中 sprites
的工作原理有很好的解释。