我一直试图让这个工作多年并且看了其他问题,但我仍然无法做到。 我按下箭头键时试图使精灵移动,但没有发生任何事情。 精灵确实被绘制但是就是这样。 这是代码。
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()
答案 0 :(得分:0)
entities.update()
方法只调用其各个类的update
方法,因此,您实际上必须在Player
类中创建一个方法,以便根据需要更新sprite。 。对于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)
我建议您查看this link。它对sprites
中pygame
的工作原理给出了很好的解释。