使用for循环将精灵放置在坐标处

时间:2015-03-19 23:02:30

标签: python for-loop pygame coordinates sprite

我目前正在尝试根据文本文件中的字符串将壁精灵放置在某些坐标处。

我想要它做的是读取文本文件(从中获取字符串)。然后根据字符串索引处的字符,在特定坐标处放置一个墙精灵。如果它不是某个角色,那么只需跳过一个墙精灵并继续前进。

在这种情况下,我想用#来放墙。它不会让我发布图像,所以生病了。

TEXTFILE:

###...

正如你所看到的,我放置3#来告诉脚本我想要3个墙,就是这样。 它们之后的3个点不是#,所以不应该放置墙壁。

在这种情况下它起作用,因为它在我的游戏中显示如此

"# = Wall"
"P = Player"

###

  P

但是当我尝试使用这种位置在我的墙上打洞时

TEXTFILE:

###...###

它在我的游戏中如此出现,在#的

的两个部分之间没有洞
"# = Wall"
"P = Player"

#########

  P

这是我到目前为止的完整主要脚本(减去课程),如果您需要更多信息,请告诉我。再次感谢!

#I - Import and Initialize
import pygame, IceBackground, Player, Wall, IceBlock
pygame.init()

#D - Display Configuration
screen = pygame.display.set_mode((700, 600))


def main():
    pygame.display.set_caption("Sliders")

    #E - Entities(Just background for now)
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((213, 220, 255))
    screen.blit(background, (0, 0))

    icebackground = IceBackground.IceBG()
    player = Player.Box(90, 150)
    iceblock = IceBlock.Box(90, 0)

    coords = []
    with open("TestLevel.txt") as file:
        testLevel = file.read()
        print(testLevel)

        file.close()

    strLength = len(testLevel)

    xcoord = 0
    ycoord = 0

    for i in range(strLength):
        print(i)
        coordInsert = (xcoord, ycoord)
        coords.insert(i, coordInsert) 

        if testLevel[i] == "#":    
            print("Wall placed!")
            print(coordInsert)
            print("\n")
            walls = [Wall.Box(xcoord, ycoord) for xcoord, ycoord in coords]
        elif testLevel[i] != "#":
            print("Nothing placed")
            print(coordInsert)
            print("\n")
        xcoord += 30



    #iceblock = [IceBlock.Box(xcoord, ycoord) for xcoord, ycoord, in coords]


    backgroundSprites = pygame.sprite.Group(icebackground)
    wallSprites = pygame.sprite.Group(*walls)
    playerSprites = pygame.sprite.Group(player)

    #A - Assign values to key variables
    clock = pygame.time.Clock()
    keepGoing = True

    #L - Set up the Main Loop
    while keepGoing:

    #T - Timer to set frame rate
        clock.tick(60)

    #E - Event Handling
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                keepGoing = False

        hitWalls = pygame.sprite.spritecollide(player, wallSprites, False)
        if hitWalls:
            if player.dx > 0:
                player.dx = 0
                player.rect.centerx = player.rect.centerx - 10

            if player.dx < 0:
                player.dx = 0
                player.rect.centerx = player.rect.centerx + 10

            if player.dy > 0:
                player.dy = 0
                player.rect.centery = player.rect.centery - 10

            if player.dy < 0:
                player.dy = 0
                player.rect.centery = player.rect.centery + 10

    #R - Refresh Display
        backgroundSprites.update()
        wallSprites.update()
        playerSprites.update()

        backgroundSprites.draw(screen)
        wallSprites.draw(screen)
        playerSprites.draw(screen)

        pygame.display.flip()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

当xcoord == 270时,你忘了将ycoord增加30点。如果你不这样做,你的所有精灵都会对齐。