1

我一直在创建一个简单的基于瓷砖的游戏来帮助我学习 python 和 wx python。对于初学者,我想创建自己的“世界”并测试我制作的简单地图生成器,我绑定了返回键以生成新地图并显示它。那是我遇到这个问题的时候。每次单击返回时它都会减慢很多,逐行渲染每个图块(这显然很慢且效率低下)并最终冻结。

我是一个新手程序员,从未处理过任何形式的 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

0

每次调用 renderScreen 时,您都在创建一组新的 wx.StaticBitmaps,并且在创建新组之前不会删除它们。一段时间后,您将拥有一堆堆在一起的小部件,旧的不再可见,但仍然在那里消耗资源。至少你应该改变一些东西,以便你的程序只制作一组 wx.StaticBitmaps,跟踪它们,然后在你想改变它们时调用它们的 SetBitmap 方法。

为了获得更好的性能,您应该忘记 StaticBitmaps 并自己在 EVT_PAINT 处理程序中绘制图像。StaticBitmaps 旨在“静态”,IOW 变化不大。相反,您可以为窗口实现 EVT_PAINT 处理程序,每当需要重绘窗口时都会调用它,并且您只需调用窗口的 Refresh 方法即可触发新的重绘。

于 2013-06-12T01:28:44.350 回答