簡體   English   中英

PyGame:精靈不會移動

[英]PyGame: Sprites won't move

我正在用PyGame制作一個小型游戲,其中相機聚焦在代表角色的精靈上,按下箭頭鍵時地圖圍繞該角色移動。 問題是地圖不動。 我之前曾多次遇到此問題,但我想將其范圍縮小到可能的程度,但是我不知道如何實際解決該問題。

這是代碼:

import sys, pygame
from pygame.locals import *

pygame.init()
size = screen_width, screen_height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

game_is_running = True

class Character(object):
    """ Defines player's metadata """
    def __init__(self):
        self.x = 350
        self.y = 250 # at about the center of the screen
        self.dx = 0
        self.dy = 0
        self.width = 50
        self.height = 50
        self.sprite = pygame.image.load("./../Images/main-char-sprite.png") #the pic representing the character
        self.dead_sprite = None #the pic representing the character when dead (doesn't exist yet)

player = Character()


class Map():
    """
    Defines environment.
    Will be drawn around static arrays for now.
    """
    def __init__(self):
        self.wall = pygame.image.load("./../Images/dungeon/wall.jpg")
        self.floor = pygame.image.load("./../Images/dungeon/floor.jpg")
        self.door = pygame.image.load("./../Images/dungeon/door.jpg")
        self.null_tile = pygame.image.load("./../Images/blank.png")

        self.x = 0
        self.y = 0

        self.tile_height = 50
        self.tile_width = 50

        self.center_x = 400
        self.center_y = 300
        #self.center_of_room = (self.center_x, self.center_y)

        self.town = [
            [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]

    def draw_floor(self): #will use arrays
        for j in range(24): #self.town is 24x24 elements
            for k in range(24):
                if self.town[j][k]==0: 
                    screen.blit(self.null_tile, (j*player.height, k*player.width))
                elif self.town[j][k]==1: 
                    screen.blit(self.wall, (j*player.height, k*player.width))
                elif self.town[j][k]==2: 
                    screen.blit(self.floor, (j*player.height, k*player.width))
                elif self.town[j][k]==3: 
                    screen.blit(self.door, (j*player.height, k*player.width))
                else: 
                    screen.blit(self.null_tile, (j*player.height, k*player.width))

    def is_traversable(self):
    # if where the player is about to walk is not a floor,
    # the land is not traversable and character will not move
        if self.town[(player.x + player.dx)//50][(player.y + player.dy)//50]==2:
            return True
        return False

map = Map()

while game_is_running:
    clock.tick(60)
    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT or key[K_ESCAPE] or key[K_RETURN]:
            game_is_running = False
        if key[K_UP]:
            player.dy = player.height-(2*player.height) #turns player.height negative
            if map.is_traversable():
                map.y -= player.height
        elif key[K_DOWN]:
            player.dy = player.height
            if map.is_traversable():
                map.y += player.height
        elif key[K_LEFT]:
            player.dx = player.width-(2*player.width)
            if map.is_traversable():
                map.x -= player.width
        elif key[K_RIGHT] and map.is_traversable():
            player.dx = player.width
            if map.is_traversable():
                map.x += player.width
        else: pass  


        """ Drawing routines: """
        screen.fill((0, 0, 0))
        map.draw_floor()
        screen.blit(player.sprite, (player.x, player.y))

        """ Graphics cleanup: """
        pygame.display.update()
        pygame.display.flip()


pygame.quit()
sys.exit()

根據我過去的PyGame程序,我總是遇到一些問題,例如,使用for循環將繪制到屏幕上的sprite集移動到屏幕上,例如我在這里所做的。 甚至平庸而簡單的例子也都失敗了。 我想知道的是為什么會這樣,如何才能簡潔地繪制大型地圖並仍然移動它們? range()創建一個列表,據我所知,列表元素是可變的,那么為什么地圖的位置沒有變化?

  1. 事件輪詢輸入中具有輸入狀態。 這會引起問題。
  2. 您正在遮蓋名稱map
  3. is_traversable始終為false
  4. 渲染精靈為x = world_x + camera_xy = world_y + camera_y 更改camera_x / y(或將其視為offset)會導致地圖滾動。 但是對象將其位置存儲為全局坐標。 (無論是基於像素還是基於圖塊)

修改后的代碼如下。 您可以看到它輪詢向上箭頭 ,但從不打印“ true!”。

import pygame
from pygame.locals import *

pygame.init()
size = screen_width, screen_height = 800, 600
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()

game_is_running = True

def temp_sprite(color, size=50):
    # gen Surface, since I don't have your sprites
    surf = pygame.Surface([size, size])
    surf.fill(Color(color))
    return surf

class Character(object):
    """ Defines player's metadata """
    def __init__(self):
        self.x = 350
        self.y = 250 # at about the center of the screen
        self.dx = 0
        self.dy = 0
        self.width = 50
        self.height = 50
        #self.sprite = pygame.image.load("./../Images/main-char-sprite.png") #the pic representing the character
        self.sprite = temp_sprite("red")


        self.dead_sprite = None #the pic representing the character when dead (doesn't exist yet)

player = Character()


class Map():
    """
    Defines environment.
    Will be drawn around static arrays for now.
    """
    def __init__(self):
        self.wall = temp_sprite("blue") #pygame.image.load("./../Images/dungeon/wall.jpg")
        self.floor = temp_sprite("green") #pygame.image.load("./../Images/dungeon/floor.jpg")
        self.door = temp_sprite("brown") #pygame.image.load("./../Images/dungeon/door.jpg")
        self.null_tile = temp_sprite("magenta") # pygame.image.load("./../Images/blank.png")

        self.x = 0
        self.y = 0

        self.tile_height = 50
        self.tile_width = 50

        self.center_x = 400
        self.center_y = 300
        #self.center_of_room = (self.center_x, self.center_y)

        self.town = [
            [0, 0, 0, 0, 0, 0, 0, 1, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0],
            [0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
        ]

    def draw_floor(self): #will use arrays
        for j in range(24): #self.town is 24x24 elements
            for k in range(24):
                if self.town[j][k]==0:
                    screen.blit(self.null_tile, (gamemap.y + j*player.height, gamemap.x + k*player.width))
                elif self.town[j][k]==1:
                    screen.blit(self.wall, (gamemap.y + j*player.height, gamemap.x + k*player.width))
                elif self.town[j][k]==2:
                    screen.blit(self.floor, (gamemap.y + j*player.height, gamemap.x + k*player.width))
                elif self.town[j][k]==3:
                    screen.blit(self.door, (gamemap.y + j*player.height, gamemap.x + k*player.width))
                else:
                    screen.blit(self.null_tile, (gamemap.y + j*player.height, gamemap.x + k*player.width))

    def is_traversable(self):
    # if where the player is about to walk is not a floor,
    # the land is not traversable and character will not move
        if self.town[(player.x + player.dx)//50][(player.y + player.dy)//50]!=2:
            print "true!"
            return True
        return False

gamemap = Map()

while game_is_running:
    clock.tick(60)

    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT or key[K_ESCAPE] or key[K_RETURN]:
            game_is_running = False

    if key[K_UP]:
        print '.'
        player.dy = player.height-(2*player.height) #turns player.height negative
        if gamemap.is_traversable():
            gamemap.y -= player.height
    elif key[K_DOWN]:
        player.dy = player.height
        if gamemap.is_traversable():
            gamemap.y += player.height
    elif key[K_LEFT]:
        player.dx = player.width-(2*player.width)
        if gamemap.is_traversable():
            gamemap.x -= player.width
    elif key[K_RIGHT] and gamemap.is_traversable():
        player.dx = player.width
        if gamemap.is_traversable():
            gamemap.x += player.width
    else: pass


    """ Drawing routines: """
    screen.fill((0, 0, 0))
    gamemap.draw_floor()
    screen.blit(player.sprite, (player.x, player.y))

    """ Graphics cleanup: """
    pygame.display.update()
    pygame.display.flip()


pygame.quit()
sys.exit()

暫無
暫無

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

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