1

I'm making a little game, but ask you could guess, i have a problem :p

My first class (it gets originalscreen from the first part of the game)

    import pygame
    from pygame.locals import *
    import pygbutton

class part():
 def __init__(self,originalscreen):

    self.screen = originalscreen
    self.part()

 def part(self):

    """ Finally: the game!"""
    # But first, some variables and imports
    parton = True
    import makemap

    # Looping
    while parton:
        # The screen
        self.screen.fill(0)
        makemap.mapmaker(self.screen)

        # Events
        for event in pygame.event.get():
            if event.type == QUIT:
                exit()

        # Updating the screen
        pygame.display.flip()

As you can read, I import makemap and pass the screen to it

   import pygame
   from pygame.locals import *

   """ Add tiles here"""
   grass = pygame.image.load("tiles/grass.png")

   def mapmaker(screen):
     height = screen.get_height()
     width = screen.get_width()

     # Set the size of each tile (25 pixels)
     xscale = width / 40
     yscale = height / 24

     # Loading file
     mapfile = open("part1map.txt", "r")

     # Making the map
     xco = 0
     yco = 0

     lines = mapfile.readlines()
     for line in lines:
        for typeoftile in range(0, len(line)):
            if str(typeoftile) == "g":
                screen.blit(grass, (xco*xscale, yco*yscale))
                xco = xco + 1
        yco = yco + 1

But then my problem occurs: my background stays black (0), so the mapmaker-function doesn't pass the screen back to my first file of code.

How can i edit the screen (like i do in mapmaker) and then display it?

I hope someone can help me Lukas

ps: if you have any questions, please ask. And sorry for my English, I'm dutch...

4

2 回答 2

1

您的问题没有完全说明——例如,我们不知道地图文件是什么样的。但这部分看起来肯定是错误的:

for line in lines:
    for typeoftile in range(0, len(line)):
        if str(typeoftile) == "g":
            screen.blit(grass, (xco*xscale, yco*yscale))
            xco = xco + 1

“typeoftile”在这里将绑定到一个介于 0 和 len(line) - 1 之间的数字。这显然永远不会是字符“g”,因此条件永远不会触发。

似乎您想要的是相应的字符,而不是索引。最简单的解决方法是像这样插入索引运算符:

for line in lines:
    for index in range(0, len(line)):
        typeoftile = line[index]
        if str(typeoftile) == "g":
            screen.blit(grass, (xco*xscale, yco*yscale))
            xco = xco + 1

但是,像这样遍历字符串中的字符会更加优雅和“Pythonic”:

for line in lines:
    for typeoftile in line:
        if str(typeoftile) == "g":
            screen.blit(grass, (xco*xscale, yco*yscale))
            xco = xco + 1

假设“line”是一个字符串,那么“typeoftile”将依次绑定到字符串中的每个字符。(当涉及到像这样迭代字符时,“abc”的工作原理与 ["a","b","c"] 相同。)

于 2013-05-22T20:18:45.713 回答
0

但是我仍然有一个问题:只显示了“grasstiles”的一条水平线,所以看起来“yco = yco + 1”不起作用,但它确实有效(我通过让我的程序打印 yco在for循环中)。

那么,没有显示第二条(和第三条……)水平草线的原因是什么?

代码现在看起来像这样:

    xco = 0
    yco = 0

    lines = mapfile.readlines()
    for line in lines:
        for typeoftile in line:
            if str(typeoftile) == "g":
                screen.blit(grass, (xco*xscale, yco*yscale))
                xco = xco + 1
        yco = yco + 1

    return screen

顺便说一句,输入文件如下所示:

gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg gggggggggggggggggggggggggggggggggggggggg格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格格

于 2013-05-23T17:06:58.410 回答