0

一个基本的嵌套 for 循环,如下所示:

test = [
    "AAAAAAAAAAA",
    "BBBBBBBBBBB",
    "CCCCCCCCCCC",
]
for row in test:
    for col in row:
        print col

应该输出列表中的每个字符test。但是,我在我的一个关卡设计游戏中运行完全相同的东西,它在第一行之后停止。

for row in level:
        for col in row:
            print col
            if col == "G":
                g = Grass(x,y)
                obsticles.append(g)
                entities.add(g)
                print "grass added to entites"
            elif col == "P":
                p = Plain_Grass(x,y)
                obsticles.append(p)
                entities.add(p)
            elif col == "F":
                f = Grass_Flower(x,y)
                obsticles.append(f)
                entities.add(f)
            elif col == "Y":
                y = Grass_To_SandD(x,y)
                obsticles.append(y)
                entities.add(y)

老实说,我不明白为什么。我知道这可能是我问过的最新问题,但老实说这让我很烦。我已经尝试过完全相同的test一切。我创建了另一个程序,以查看它在不在游戏程序中时是否有效,并且确实有效。任何人都可以看到问题吗?

[编辑] 这是我的 tilemap 文件:

def testPlace():

    #This is where all the testing happens...

    tilemap = [
        # 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0
        "T T T T T T T T T T T T T T T T T T T T",
        "T G F G G G G P P F P G G G G G G G G T",
        "T G G G G G G G F G P G G G G G G G G G",
        "T G G G G G G G G F P F F G F G G G G G",
        "O O O O O O O O O O O O O O O O O O O O",
        "O O O O O O O O O O O O O O O O O O O O",
        "G G F G G G G G G O O F G G G G G G G G",
        "F G F T G G G G T O O F G G G G G G G G",
        "T G G G G G G G G O O T G G G G F G P G",
        "G G G T F G G G G O O G G G G G G G T G",
        "G G G G G G G G G G G G G F G G G T G G",
        "Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y",
        "S S S S S S S S S S S S S S S S S S S S",
        "S S S S S S S S S S S S S S S S S S S S",
        "S S S S S S S S S S S S S S S S S S S S",
        "Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q Q",
        "W W W W W W W W W W W W W W W W W W W W",
        "W W W W W W W W W W W W W W W W W W W W",
        "W W W W W W W W W W W W W W W W W W W W",
        "W W W W W W W W W W W W W W W W W W W W",
        "W W W W W W W W W W W W W W W W W W W W",
        ]

    return tilemap

和主要脚本:

#!/usr/bin/python

import pygame, sys, os, tilemap
from pygame.locals import *

#Always ensure that the height and width are devisible by 32!
windowSize = windowWidth, windowHeight = 576, 576
#Ensuring width and height can take 32x32 blocks
if windowWidth%32 == 0 and windowHeight%32 == 0:
    blocksInX = windowWidth/32
    blocksInY = windowHeight/32
else:
    raise SystemExit, "Too Much Space To Fit 32x32 Blocks in X and Y"

#Frames Per Second (MAX, not what it is going to be)
FPS = 15



#Colours
black = [ 0, 0, 0]
white = [255, 255, 255]
red = [255, 0, 0]
green = [ 0, 255, 0]
blue = [ 0, 0, 255]
orange = [ 255, 122, 0]
cyan = [ 0, 155, 255]
purple = [ 155, 0, 255]
lime = [155, 255, 0]

def main():
    #Setting initial variables
    pygame.init()
    screen = pygame.display.set_mode(windowSize)
    pygame.display.set_caption("Unnamed Game")
    clock = pygame.time.Clock()

    #Defaulting the all controlls to "off" upon first starting (no button is being pushed)
    up = down = left = right = False

    #Assuming everything including player is an entity (including the grass)
    entities = pygame.sprite.Group()
    player = Player(32, 32)
    obsticles = []

    x = y = 0
    #See tilemaps file for information on the map layout
    level = tilemap.testPlace()
    print level

    #Placing all the blocks needed to be displayed
    for row in level:
        for col in row:
            print col
            if col == "G":
                g = Grass(x,y)
                obsticles.append(g)
                entities.add(g)
                print "grass added to entites"
            elif col == "P":
                p = Plain_Grass(x,y)
                obsticles.append(p)
                entities.add(p)
            elif col == "F":
                f = Grass_Flower(x,y)
                obsticles.append(f)
                entities.add(f)
            elif col == "Y":
                y = Grass_To_SandD(x,y)
                obsticles.append(y)
                entities.add(y)
            x += 32
        y+= 32
        x = 0

        entities.add(player)

        while True:
            clock.tick(FPS)

            for event in pygame.event.get():
                if event.type == QUIT:
                    pygame.quit()
                    sys.exit()

                if event.type ==  KEYDOWN:
                    if event.key == K_ESCAPE:
                        pygame.quit()
                        sys.exit()
                    #Passing commands for keydown events
                    #UP = "-", down = "+", Left = "-", right = "+"
                    elif event.key == K_UP:
                        up = True
                        print ("UP")
                    elif event.key == K_DOWN:
                        down = True
                        print ("Down")
                    elif event.key == K_LEFT:
                        left = True
                        print ("LEFT")
                    elif event.key == K_RIGHT:
                        right = True
                        print ("RIGHT")

                if event.type == KEYUP:
                    if event.key == K_UP:
                        up = False
                    elif event.key == K_DOWN:
                        down = False
                    elif event.key == K_LEFT:
                        left = False
                    elif event.key == K_RIGHT:
                        right = False


            #Redrawing the player onto the screen
            player.update(up, down, left, right, obsticles)
            #Redrawing all other objects
            entities.draw(screen)

            pygame.display.update()

class Entity(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

class Player(Entity):

    def __init__(self, x, y):
        Entity.__init__(self)
        self.xPos = x
        self.yPos = y
        self.inShop = False
        self.image = pygame.Surface((32, 32))
        self.image.fill(red)
        self.image.convert()
        self.rect = Rect(self.xPos, self.yPos, 32, 32)

    def update(self, up, down, left, right, obsticles):
        if up:
            self.yPos -= 32
        if down:
            self.yPos += 32
        if left:
            self.xPos -= 32
        if right:
            self.xPos += 32

        self.rect = Rect(self.xPos, self.yPos, 32, 32)
        #Collision Detection (for x axis)
        self.collide(self.xPos, 0, obsticles)
        #Collision Detection (for y axis)
        self.collide(0, self.yPos, obsticles)

    def collide(self, xPos, yPos, obsticles):
        pass

class Grass(Entity):

    def __init__(self, x, y):
        Entity.__init__(self)
        self.image = pygame.image.load("data/images/Grass.png")
        self.image.convert()
        self.rect = Rect(x, y, 32, 32)

class Plain_Grass(Entity):

    def __init__(self, x, y):
        Entity.__init__(self)
        self.image = pygame.image.load("data/images/Grass_Plain.png")
        self.image.convert()
        self.rect = self.image.get_rect()

class Grass_Flower(Entity):

        def __init__(self, x, y):
                Entity.__init__(self)
                self.image = pygame.image.load("data/images/Grass_Flower.png")
                self.image.convert()
                self.rect = self.image.get_rect()

class Grass_To_SandD(Entity):

        def __init__(self, x, y):
                Entity.__init__(self)
                self.image = pygame.image.load("data/images/Grass_To_SandD.png")
                self.image.convert()
                self.rect = self.image.get_rect()

if __name__ == "__main__":
        main()
4

1 回答 1

3

这是我的镜头:

entities.add(player)在循环内缩进后,您拥有所有代码。删除多余的缩进,将其放在循环之外。

编辑:事实上,我想entities.add(player)也应该不在循环中。

于 2013-07-09T15:28:49.480 回答