Exemplo n.º 1
0
 def __init__(self,win):
     self.win = win
     self.drawGUI(win)
     self.locations = [(r,c) for r in range(45) for c in range(45)]
     self.model = LifeModel(45,45)
     self.animating = False         # the model/display is not changing
     self.running = True            # the application is running
Exemplo n.º 2
0
	def __init__(self):
		"""Initializes the PyGame framework and various
		   other elements.   
		"""
		pygame.init()		

		self.screen = pygame.display.set_mode(SCREEN_SIZE)
		pygame.display.set_caption(WINDOW_CAPTION)	
		
		# Create toolbar
		self.toolbar = pygame.surface.Surface((SCREEN_SIZE[0], TOOLBAR_HEIGHT))
		
		# Initialize the model
		self.model = LifeModel(BOARD_SIZE, BOARD_SIZE)
		self.start = False
		self.done  = False
		
		# Create the "Generation:" label object
		self.game_text = pygame.font.SysFont(FONT, FONT_SIZE)
		self.generation_label = self.game_text.render(GENERATION_TEXT, 1, BLACK)
		
		# Load the button images
		self.start_button = pygame.image.load(START_BUTTON).convert()
		self.clear_button = pygame.image.load(CLEAR_BUTTON).convert()

		# Initialize frame rate stuff
		self.clock = pygame.time.Clock()
		self.speed = FRAME_RATE
Exemplo n.º 3
0
	def test_find_neighbours_1(self):
		testmodel = LifeModel(10,10)
		testmodel.set_seed(1,1)
		# Check from (1, 0)
		# (1, 1) is 1 unit below (1,0)
		self.assertEquals(testmodel.find_neighbours(1, 0), 1)
Exemplo n.º 4
0
	def test_find_neighbours_none(self):
		testmodel = LifeModel(10,10)
		# Check from (1, 0)
		self.assertEquals(testmodel.find_neighbours(0,0), 0)
Exemplo n.º 5
0
class LifeApp:

    def __init__(self,win):
        self.win = win
        self.drawGUI(win)
        self.locations = [(r,c) for r in range(45) for c in range(45)]
        self.model = LifeModel(45,45)
        self.animating = False         # the model/display is not changing
        self.running = True            # the application is running


    def drawGUI(self, win):
        # draws gui components including: grid, runbutton, pausebutton, stepbutton
        # savebutton, loadbutton, clearbutton, quitbutton
        
        self.grid = GridView(win,Point(50,60), 2.1,45,45)
        self.runbutton = Button(win,Point(12.5,98),6,3,"Run")
        self.runbutton.activate()
        self.pausebutton = Button(win,Point(25,98),8.5,3,"Pause")
        self.pausebutton.activate()
        self.stepbutton = Button(win,Point(37.5,98),7,3,"Step")
        self.stepbutton.activate()
        self.savebutton = Button(win,Point(62.5,98),7,3,"Save")
        self.savebutton.activate()
        self.loadbutton = Button(win,Point(75,98),7,3,"Load")
        self.loadbutton.activate()
        self.clearbutton = Button(win,Point(50,98),7.5,3,"Clear")
        self.clearbutton.activate()
        self.quitbutton = Button(win,Point(87.5,98),6,3,"Quit")
        self.quitbutton.activate()

    def run(self,win):
        #defines the event loop
        
        while self.running:
            self.setButtonState()
            pt = self.win.checkMouse()
            if pt != None:
                self.handleEvent(pt)
            if self.animating:
                changes = self.model.step()
                self.updateView(changes)
            time.sleep(.1)
        self.win.close()


    def setButtonState(self):
        # sets proper activation of run and pause buttons
        
        if self.animating == True:
            self.runbutton.deactivate()
            self.pausebutton.activate()
        else:
            self.runbutton.activate()
            self.pausebutton.deactivate()
        


    def updateView(self,locations):
        # Sets checkboxes in locations of grid to reflect their state
        # in the life model. Locations is a list of (row,column) pairs.
        
        for r,c in self.locations:
            value = self.model.isAlive(r,c)
            self.grid.setChecked(r,c,value)
        self.win.update()
            


    def handleEvent(self, pt):
        # The user clicks at point pt, take appropriate action
        
        if self.quitbutton.clicked(pt):
            self.running = False
        elif self.runbutton.clicked(pt):
            self.animating = True
        elif self.pausebutton.clicked(pt):
            self.animating = False

        if self.grid.clicked(pt):
            self.on_grid()            
        elif self.stepbutton.clicked(pt):
            self.on_step()
        elif self.loadbutton.clicked(pt):
            self.on_load()
        elif self.savebutton.clicked(pt):
            self.on_save()
        elif self.clearbutton.clicked(pt):
            self.on_clear()
            
            


    def on_step(self):
        # turns off animation 
        # call step on your life model to get a list of changed locations
        # call your LifeApps updateView passing the list of changed locations
       
        self.animating = False
        
        x = self.model.step()
        self.updateView(x)
       
                
    def on_load(self):
        #loads a file and displays its saved string as checkboxes on the gridview
        
        self.animating = False
        fromFile=askopenfilename()
        self.model.loadString(open(fromFile).read())
        self.updateView(self.locations)

    def on_save(self):
        #saves the current checkboxes drawn on the grid view as string to a specified file
        
        self.animating = False
        
        outFile=asksaveasfilename()
        f=open(outFile, 'w')
        f.write(str(self.model))
        

    def on_grid(self):
        #allows the user to click on the grid
        
        i,j = self.grid.getLocation()
        oldValue = self.model.isAlive(i,j)
        self.model.setAlive(i,j, not oldValue)
        self.updateView([(i,j)])
                  

    def on_clear(self):
        #clears the grid to update it to its original state
        
        for r in range(45):
            for c in range(45):
                
                self.model.setAlive(r,c,False)
        self.updateView(self.locations)
Exemplo n.º 6
0
class GameOfLife:
	"""The core class of the game. Responsible
	   for drawing the world and responding to
       user events. This is the view of the
       application.
	"""
	def __init__(self):
		"""Initializes the PyGame framework and various
		   other elements.   
		"""
		pygame.init()		

		self.screen = pygame.display.set_mode(SCREEN_SIZE)
		pygame.display.set_caption(WINDOW_CAPTION)	
		
		# Create toolbar
		self.toolbar = pygame.surface.Surface((SCREEN_SIZE[0], TOOLBAR_HEIGHT))
		
		# Initialize the model
		self.model = LifeModel(BOARD_SIZE, BOARD_SIZE)
		self.start = False
		self.done  = False
		
		# Create the "Generation:" label object
		self.game_text = pygame.font.SysFont(FONT, FONT_SIZE)
		self.generation_label = self.game_text.render(GENERATION_TEXT, 1, BLACK)
		
		# Load the button images
		self.start_button = pygame.image.load(START_BUTTON).convert()
		self.clear_button = pygame.image.load(CLEAR_BUTTON).convert()

		# Initialize frame rate stuff
		self.clock = pygame.time.Clock()
		self.speed = FRAME_RATE
	
	def game_loop(self):
		"""Contains the core game loop that
		   listens for events and manipulates
		   the world accordingly. Also responsible
		   for updating the screen.
		"""
		while self.done == False:
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					self.done = True
				if event.type == pygame.MOUSEBUTTONDOWN:
					pos = pygame.mouse.get_pos()
				
					self.detect_button_events(pos)
					self.set_cell_state(pos)
			
			if self.start:
				self.model.next_generation()
			
			self.screen.fill(BLACK)

			self.screen.set_clip(0,0,SCREEN_SIZE[0], TOOLBAR_HEIGHT)
			self.draw_toolbar()			

			self.screen.set_clip(0,TOOLBAR_HEIGHT,SCREEN_SIZE[0], SCREEN_SIZE[1])			
			self.draw_world()

			self.clock.tick(FRAME_RATE)
			pygame.display.flip()
	
	def detect_button_events(self, pos):
		"""Detects when either of the two buttons
		   are clicked and manipulates the world
		   accordingly.
		"""
		if self.start_button.get_rect().move(START_BUTTON_LOC).collidepoint(pos):
			self.start = False if self.start else True

		if self.clear_button.get_rect().move(CLEAR_BUTTON_LOC).collidepoint(pos):
			self.start = False
			self.model.clear_world()
			
	def set_cell_state(self, pos):
		"""Toggles the state of a cell. Green
		   represents a live cell and white
		   represents a dead cell.
		"""
		y = pos[0] // (CELL_SIZE+MARGIN)
		x = pos[1] // (CELL_SIZE+MARGIN)

		self.model.set_seed(x, y)
	
	def draw_toolbar(self):
		"""Draws the toolbar to the screen."""
		self.toolbar.fill(GREY)
		self.screen.blit(self.toolbar, (0,0))	
		
		self.screen.blit(self.generation_label, GENERATION_LABEL_LOC)
		
		gen = self.game_text.render(str(self.model.generation), 1, BLACK)
		self.screen.blit(gen, GENERATION_LOC)
		self.screen.blit(self.start_button, START_BUTTON_LOC)
		self.screen.blit(self.clear_button, CLEAR_BUTTON_LOC)

	def draw_world(self):
		"""Draws the state of the world based
		   on the state of each cell in the LifeModel.
		"""
		for x in range(BOARD_SIZE):
			for y in range(BOARD_SIZE):
				# If the cell is dead, color it white.
				# Else the cell is alive, color it green.
				if self.model.get_currentgrid_xy(x, y) == False:
					color = WHITE
				else:
					color = GREEN

				pygame.draw.rect(self.screen, color, [ \
					MARGIN+(CELL_SIZE+MARGIN)* y, \
					MARGIN+(CELL_SIZE+MARGIN)* x, \
					CELL_SIZE, \
					CELL_SIZE \
				])
	
	def quit(self):
		"""Called when the user presses
		   the close button. Cleans up the
		   PyGame framework.
		"""
		pygame.quit()