Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import wx
- import wx.xrc
- import wx.grid
- import random
- ###########################################################################
- ## Class MyFrame1
- ###########################################################################
- class MyFrame1 ( wx.Frame ):
- def __init__( self, parent ):
- wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 819,267 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
- self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
- bSizer1 = wx.BoxSizer( wx.VERTICAL )
- self.m_scrolledWindow1 = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
- self.m_scrolledWindow1.SetScrollRate( 5, 5 )
- wSizer1 = wx.WrapSizer( wx.HORIZONTAL )
- self.Grid = wx.grid.Grid( self.m_scrolledWindow1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
- # Grid
- self.Grid.CreateGrid( 10, 10 )
- self.Grid.EnableEditing( True )
- self.Grid.EnableGridLines( True )
- self.Grid.EnableDragGridSize( False )
- self.Grid.SetMargins( 0, 0 )
- # Columns
- self.Grid.EnableDragColMove( False )
- self.Grid.EnableDragColSize( True )
- self.Grid.SetColLabelSize( 0 )
- self.Grid.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
- # Rows
- self.Grid.EnableDragRowSize( True )
- self.Grid.SetRowLabelSize( 0 )
- self.Grid.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
- # Label Appearance
- # Cell Defaults
- self.Grid.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
- wSizer1.Add( self.Grid, 0, wx.ALL, 5 )
- self.Button = wx.Button( self.m_scrolledWindow1, wx.ID_ANY, u"Trace it!", wx.DefaultPosition, wx.DefaultSize, 0 )
- wSizer1.Add( self.Button, 0, wx.ALL, 5 )
- self.GenerateBtn = wx.Button( self.m_scrolledWindow1, wx.ID_ANY, u"Generate road!", wx.DefaultPosition, wx.DefaultSize, 0 )
- wSizer1.Add( self.GenerateBtn, 0, wx.ALL, 5 )
- self.m_scrolledWindow1.SetSizer( wSizer1 )
- self.m_scrolledWindow1.Layout()
- wSizer1.Fit( self.m_scrolledWindow1 )
- bSizer1.Add( self.m_scrolledWindow1, 1, wx.EXPAND |wx.ALL, 0 )
- self.SetSizer( bSizer1 )
- self.Layout()
- self.Centre( wx.BOTH )
- # Connect Events
- self.Button.Bind( wx.EVT_BUTTON, self.Trace )
- self.GenerateBtn.Bind( wx.EVT_BUTTON, self.Generate )
- def __del__( self ):
- pass
- #Reset original colors of grid
- def reset( self ):
- for i in range(0, self.Grid.GetNumberRows()):
- for j in range(0, self.Grid.GetNumberCols()):
- self.Grid.SetCellBackgroundColour(i, j, wx.WHITE)
- self.Grid.SetCellTextColour(i, j, wx.BLACK)
- #Recursive function to find path through road
- def FindPath( self, i, j ):
- #Timer for visualisating work
- #Colorize current cell
- color = wx.Colour(100, 100, 255)
- self.Grid.SetCellBackgroundColour(i, j, color)
- self.Grid.SetCellTextColour(i, j, wx.WHITE)
- self.Grid.ForceRefresh()
- #Down
- if i + 1 < self.Grid.GetNumberRows() and self.Grid.GetCellValue(i + 1, j) == "0" and self.Grid.GetCellBackgroundColour(i + 1, j) != color:
- return self.FindPath(i + 1, j)
- #Up
- elif i - 1 >= 0 and self.Grid.GetCellValue(i - 1, j) == "0" and self.Grid.GetCellBackgroundColour(i - 1, j) != color:
- return self.FindPath(i - 1, j)
- #Left
- elif j - 1 >= 0 and self.Grid.GetCellValue(i, j - 1) == "0" and self.Grid.GetCellBackgroundColour(i, j - 1) != color:
- return self.FindPath(i, j - 1)
- #Right
- elif j + 1 < self.Grid.GetNumberCols() and self.Grid.GetCellValue(i, j + 1) == "0" and self.Grid.GetCellBackgroundColour(i, j + 1) != color:
- return self.FindPath(i, j + 1)
- else: return None
- #Function for check for free cells in defined range
- def free( self, i1, i2, j1, j2 ):
- #Go through all cells
- for i in range(i1, i2 + 1):
- for j in range(j1, j2 + 1):
- #If cell is above grid or a part of road
- #then way is not free
- try:
- if self.Grid.GetCellValue(i, j) == "0":
- return False
- except:
- return False
- return True #Else way is clear
- #Recursive function to make a road
- def MakeRoad( self, i, j ):
- self.Grid.SetCellValue(i, j, "0")
- #Make random direction
- #Set list with directions
- dirs = [n for n in range(1, 5)] #1 for down, 2 for up, 3 for left, 4 for right
- #Check 2x3 rectangle in cells of 1 below these directions
- #Down
- if self.free(i + 1, i + 2, j - 1, j + 1) == False:
- dirs.remove(1)
- #Up
- if self.free(i - 2, i - 1, j - 1, j + 1) == False:
- dirs.remove(2)
- stop = False
- #Left
- if self.free(i - 1, i + 1, j - 2, j - 1) == False:
- dirs.remove(3)
- #Right
- if self.free(i - 1, i + 1, j + 1, j + 2) == False:
- dirs.remove(4)
- if dirs == []:
- return None #If no free directions left generation stops
- else:
- direction = random.choice(dirs) #Else set a random direction from last ones
- #Make a road
- #Down
- if direction == 1:
- return self.MakeRoad(i + 1, j)
- #Up
- elif direction == 2:
- return self.MakeRoad(i - 1, j)
- #Left
- elif direction == 3:
- return self.MakeRoad(i, j - 1)
- #Right
- elif direction == 4:
- return self.MakeRoad(i, j + 1)
- # Virtual event handlers, overide them in your derived class
- def Trace( self, event ):
- self.reset()
- #Find beginning of the road
- for i in range(0, self.Grid.GetNumberRows()):
- for j in range(0, self.Grid.GetNumberCols()):
- if self.Grid.GetCellValue(i, j) == '0':
- break
- if self.Grid.GetCellValue(i, j) == '0':
- break
- #Call the function of making path
- #Statment used for check that there is really road in case that last cell isn't equal to 0
- if self.Grid.GetCellValue(i, j) == '0':
- return self.FindPath(i, j)
- else:
- return wx.LogMessage("No road there!") #Log message if there is no road
- #Fill all cells with random numbers and call a function to make a road with a parameters as random cell on the top
- #By the way, it's a click's on generate button event
- def Generate( self, event ):
- self.reset()
- for i in range(0, self.Grid.GetNumberRows()):
- for j in range(0, self.Grid.GetNumberCols()):
- self.Grid.SetCellValue(i, j, str(random.randrange(1, 2)))
- return self.MakeRoad(0, random.randrange(1, self.Grid.GetNumberCols() - 1))
- app = wx.App(False) #Create example of app's class
- frame = MyFrame1(parent=None) #Example of window's frame's class
- frame.Show() #Show frame
- app.MainLoop() #Show app's window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement