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

2024-10-05 14:29:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正试图放置墙精灵在某些坐标的基础上字符串从一个文本文件。你知道吗

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

在这种情况下,我想用#放置一面墙。它不会让我发布图片,所以我画它。你知道吗

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()

Tags: rectboxforifcoordsscreenpygamebackground