簡體   English   中英

如何更新pygame中的精靈位置?

[英]How to update sprite position in 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()

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)

我建議您檢查一下此鏈接 它對pygamesprites的工作方式給出了很好的解釋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM