使图像移动每个瓷砖pygame

时间:2016-04-05 14:00:19

标签: python pygame

我是编程的新手,我正在尝试创建一个基于2D平铺的游戏。我已经设法将我的士兵自由地移动到瓷砖上,但是现在我想让它在按键上按照每个瓷砖移动。

我知道当我将self.pos改为self.x或self.y时,我可以在屏幕上自由移动士兵

到目前为止我的代码:

#envoirment variables
ice = 0
forest = 1
desert = 2
swamp = 3
water = 4
island = 5

#envoirement tiles
tiles = {
        ice    : icetile,
        forest : foresttile, 
        desert : deserttile,
        swamp  : swamptile,
        water  : watertile,
        island : islandtile        
    }

#tilemap
tilemap = [
        [ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0, 0],
        [ 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 0, 0, 0, 0, 0],
        [ 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4],
        [ 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4],
        [ 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4],
        [ 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 4, 4, 4, 4, 4, 4, 4],
        [ 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1], 
        [ 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1],
        [ 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1],
        [ 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1],
        [ 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1],
        [ 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1],
        [ 2, 2, 2, 2, 2, 2, 2, 4, 4, 4, 4, 1, 1, 1, 1, 1, 1, 1]
       ]
class Soldier(object):

        def __init__(self):
        self.image = pygame.image.load("soldier.png")
        self.x = 0
        self.y = 0
        self.hp = 1
        self.ap = 1
        self.price = 150
        self.pos = [0, 0]

    def keys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_DOWN]:
            self.pos[1] += 1
        if key[pygame.K_UP]:
            self.pos[1] -= 1
        if key[pygame.K_RIGHT]:
            self.pos[0] += 1
        if key[pygame.K_LEFT]:
            self.pos[0] -= 1

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))

def gameloop():

gamescreen = pygame.display.set_mode((mapwidth*tilesize, mapheight*tilesize))  
gameloop = True
soldier = Soldier()
kasteel = Base()


while gameloop: #Main game loop
    for row in range(mapheight):
        for column in range(mapwidth):
            gamescreen.blit(tiles[tilemap[row][column]], (column*tilesize, row*tilesize))


    for event in pygame.event.get():
        pos = pygame.mouse.get_pos()
        print(pos)
        if event.type == pygame.QUIT:
            pygame.quit()
            gameloop = False

    soldier.keys()
    kasteel.draw(gamescreen)
    soldier.draw(gamescreen)
    pygame.display.update()
    clock.tick(40) 
pygame.quit()

感谢您的时间并帮助我

PS:欢迎任何书籍或网站推荐!

2 个答案:

答案 0 :(得分:0)

这里你要做的是跟踪按下按键的第一帧,并在发生这种情况时调用移动功能。这可以按如下方式完成:

def keys(self):
        key = pygame.key.get_pressed()
        if key[pygame.K_DOWN] and not self.prev_pressed[pygame.K_DOWN]:
            self.row += 1
        if key[pygame.K_UP] and not self.prev_pressed[pygame.K_UP]:
            self.row -= 1
        if key[pygame.K_LEFT] and not self.prev_pressed[pygame.K_LEFT]:
            self.col -= 1
        if key[pygame.K_RIGHT] and not self.prev_pressed[pygame.K_RIGHT]:
            self.col += 1

        self.prev_pressed[pygame.K_DOWN] = key[pygame.K_DOWN]
        self.prev_pressed[pygame.K_UP] = key[pygame.K_UP]
        self.prev_pressed[pygame.K_LEFT] = key[pygame.K_LEFT]
        self.prev_pressed[pygame.K_RIGHT] = key[pygame.K_RIGHT]

其中prev_pressed初始化为构造时的所有false。而不是存储posxy而是将rowcol存储为tile数组中士兵的当前行和列。在打印时,将阵列中的图块映射到屏幕上的像素。例如,如果每个图块的宽度为50像素,高度为50像素,则某些图块的屏幕上的相应像素将为50*row, 50*col

答案 1 :(得分:0)

surface.blit(self.image,(self.POS [0] * tilesize,self.POS [1] * tilesize))

对不起,我正在工作并在糟糕的平板电脑上发帖。