1

私は小さなゲームを作っていますが、推測できるようにお願いします.私には問題があります:p

私の最初のクラス (ゲームの最初の部分から元の画面を取得します)

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

ご覧のとおり、makemapをインポートして画面を渡します

   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

しかし、その後、問題が発生します。背景が黒 (0) のままであるため、mapmaker-function は画面を最初のコード ファイルに戻しません。

画面を編集して (マップメーカーで行うように)、表示するにはどうすればよいですか?

誰かが私を助けてくれることを願っています ルーカス

ps: ご不明な点がございましたら、お問い合わせください。そして私の英語でごめんなさい、私はオランダ人です...

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」にはならないため、条件がトリガーされることはありません。

あなたが望むのは、インデックスではなく、対応するcharacterのようです。最も簡単な修正は、次のようにインデックス演算子を挿入することです。

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 に答える