2013-07-09 54 views
0

基本巢循环看起来像这样不工作。但是,我在我的一款游戏中为关卡设计运行了完全相同的事情,并在第一行之后停止。For循环如预期

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

也许只有一行? – Dahaka

+5

你可以显示'level'的值吗? – Louis

+0

代码中的任何'break'语句?目前我看不到任何理由,所以我想你必须告诉我们更多。 – Alfe

回答

3

这里是我的镜头:

你把所有的代码后entities.add(player)缩进INSIDE循环。删除额外的缩进将其放在循环外。

编辑: 事实上,我猜entities.add(player)也应该没有循环。

+2

如何提前中止外环? – Alfe

+0

我不认为这可能会结束循环,但显然它可以。我实际上是使用记事本++的一些代码,并没有意识到标签设置被设置为“8”。我经常在空闲和记事本++之间切换,所以我的缩进结束了。谢谢! – ReallyGoodPie

+3

它不是,但是在读完数组的第一行之后它正在执行所有的游戏逻辑。它在阅读世界数据的同时运行游戏! – Racso