1

Python と wx Python の学習に役立つ単純なタイル ベースのゲームを作成しています。手始めに、独自の「世界」を作成し、作成した単純なマップ ジェネレーターをテストするために、リターン キーをバインドして新しいマップを生成し、それを表示しました。その時、私はこの問題に遭遇しました。戻るをクリックするたびに速度が大幅に低下し、各タイルを 1 行ずつレンダリングし (これは明らかに遅くて非効率的です)、最終的にはフリーズします。

私はかなり初心者のプログラマーであり、GUI を扱ったことがないため、これはすべて私にとって非常に新しいことです。ご容赦ください。私が設定した方法はマシンにとって非常に負担が大きく、おそらく多くの再帰を引き起こしていると推測できます。私は単に気づいていません。また、私は OOP にあまり詳しくないので、例に従ってクラスを作成するだけです (したがって、すべてを処理する大規模なクラスが 1 つしかないのはなぜですか? すべての '__ something__' 関数についてはよくわかりません。)

これまでに書いたすべてのコードは次のとおりです。コメントアウトされたセクションは無視してください。これらは将来の機能などのためのものです。

import wx
import random


#main screen class, handles all events within the main screen
class MainScreen(wx.Frame):

    hMap = []
    tTotalX = 50
    tTotalY = 50

    def __init__(self, *args, **kwargs):
        #This line is equivilant to wx.Frame.__init__('stuff')
        super(MainScreen, self).__init__(None, -1, 'You shouldnt see this', style = wx.SYSTEM_MENU | wx.CAPTION | wx.CLOSE_BOX)

        self.renderScreen()

    def genMap(self,tTotalX,tTotalY):
        count1 = 0
        count2 = 0
        self.hMap = []
        while count1 < tTotalY:
            count2 = 0
            newrow = []
            while count2 < tTotalX: 
                newrow.append(random.randint(1,120))
                count2 += 1
            self.hMap.append(newrow)
            count1 += 1
        self.smooth(tTotalX, tTotalY)
        self.smooth(tTotalX, tTotalY)


    def smooth(self, tTotalX, tTotalY):
        countx = 0
        county = 0
        while county < tTotalY:
            countx = 0

            while countx < tTotalX: 
                above = county - 1
                below = int(county + 1)
                east = int(countx + 1)
                west = int(countx - 1)
                if east >= tTotalX:
                    east = 0
                if west < 0:
                    west = tTotalX -1

                teast = self.hMap[county][east]
                twest = self.hMap[county][west]

                if above < 0 or below >= tTotalY: 
                    smooth = (self.hMap[county][countx] + teast + twest)/3
                else:
                    tabove = self.hMap[above][countx]
                    tbelow = self.hMap[below][countx]
                    smooth = (self.hMap[county][countx] + tabove + tbelow + teast + twest)/5

                self.hMap[countx][county] = int(smooth)               
                countx += 1

            county += 1        

    def getTileType(self, coordX, coordY, totalX, totalY):
        #this is the part of map creation, getting tile type based on tile attributes
        tType = ''
        height = self.hMap[coordX][coordY]
        #the below values are all up to tweaking in order to produce the best maps
        if height <= 55:
            tType = 'ocean.png'

        if height > 55:
            tType = 'coast.png'

        if height > 60:
            tType = 'grassland.png'

        if height > 75:
            tType = 'hills.png'

        if height > 80:
            tType = 'mountain.png'

        if tType == '':
            tType = 'grassland.png'

        return tType

    #render the main screen so that it dislays all data
    def renderScreen(self):
        frameSize = 810 #Size of the game window
        tTotalX = self.tTotalX #the dimensions of the tile display, setting for in-game coordinates
        tTotalY = self.tTotalY
        #tsTiny = 1 #ts = Tile Size
        #tsSmall = 4
        tsMed = 16
        #tsLrg = 32
        #tsXlrg = 64
        tsCurrent = tsMed #the currently selected zoom level, for now fixed at tsMed
        pposX = 0 #ppos = Pixel Position
        pposY = 0
        tposX = 0 #tpos = tile position, essentially the tile co-ordinates independent of pixel position
        tposY = 0
    #The below is just an example of how to map out the grid, it should be in its own function in due time

        self.genMap(tTotalX, tTotalY)

        while tposY < tTotalY: #loops through all y coordinates
            tposX = 0   
            while tposX < tTotalX: #loops through all x coordinates
                pposX = tposX*tsCurrent
                pposY = tposY*tsCurrent
                tiletype = self.getTileType(tposX,tposY,tTotalX,tTotalY)

                img = wx.Image(('F:\First Pass\\' + str(tiletype)), wx.BITMAP_TYPE_ANY).ConvertToBitmap()
                wx.StaticBitmap(self, -1, img, (pposX, pposY))#paints the image object (i think)

                tposX += 1
            tposY += 1

        self.Bind(wx.EVT_KEY_DOWN, self.onclick)
        self.SetSize((frameSize-4, frameSize+16))
        self.SetBackgroundColour('CYAN')
        self.Centre()
        self.SetTitle('Nations First Pass')
        #string = wx.StaticText(self, label = 'Welcome to Nations, First Pass', pos = (tTotalX*tsCurrent/2,tsCurrent*tTotalY/2))
        #string.SetFont(wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT))

        self.Show()

    def onclick(self, e):
        key = e.GetKeyCode()

        if key == wx.WXK_RETURN:

            self.renderScreen()


 #game loop
     def main():
         app = wx.App()
         MainScreen(None)
         app.MainLoop()

 if __name__ == '__main__':
     main()

独自の 'ocean.png' 'coast.png' 'grassland.png' 'hills.png' および 'mountain.png' (16x16 ピクセルである必要があります) を作成するか、Imgur リンクから私のものを使用できます。 :

http://imgur.com/a/uFxfn

また、必要に応じて img コード内のファイル パスを変更してください。それ自体を行うためにそれを設定する方法を理解する必要がありますが、それは別の日の別の課題です.

これについて何か洞察があれば、私は非常に感謝しています。

4

1 に答える 1