# NAME PENDING # # By: Fabian Finkbeiner # Start: 30/4/2021 # End: # Import neccesary libraries from tkinter import * from tkinter import messagebox as msg import random class Tile(Canvas): ''' Represents a Tile of the Raft ''' def __init__(self, master, coord,color = "dodgerblue"): '''Tile(master,coord,color) -> Tile Creates the Tile''' # Initiate the canvas Canvas.__init__(self,master,width=50,height=50,\ bg=color, highlightbackground='black',\ highlightthickness = 2, bd = 0) # Bind Buttons self.bind('',self.clicked) self.bind('',self.clicked) self.bind('',self.clicked) # Grids the square self.grid(row=coord[0],column=coord[1]) # Store and define variables self.color = color self.coord = coord self.master = master self.occupied = False self.tiled = False self.selected = False self.border = False # Add the text for a player self.player = self.create_text(26,31,font=('Helvetica', '25'),text='') self.player2 = self.create_text(27,25,font=('Helvetica', '25'),text='') def clicked(self,fakecoords): '''SplashSquare.clicked(fakecoords) Alerts the frame that the tile has been clicked.''' self.master.squareclicked(self,self.coord) def getColor(self): '''Tile.getColor() Returns the color of the tile''' return self.color def getCoord(self): '''Tile.getCoord() Returns the coord of the tile''' return self.coord def ifOccupied(self): '''Tile.ifOccupied() Returns if the tile is occupied''' return self.occupied def ifTiled(self): '''Tile.ifOccupied() Returns if the tile is 'tiled' Which means occupied by a player at one point''' return self.tiled def ifBorder(self): '''Tile.ifBorder() Returns if the tile is a border''' return self.border def addPlayer(self, color): '''Tile.addPlayer() Adds a player to the tile''' self.occupied = True # Add drawing self.itemconfigure(self.player, text='*', fill=color) self.itemconfigure(self.player2, text='O', fill=color) def rmvPlayer(self): '''Tile.rmvPlayer() Removes the player''' # Remove drawing self.itemconfigure(self.player, text='') self.itemconfigure(self.player2, text='') self.tiled = True def addRock(self): '''Tile.addRock() Adds a rock to the tile''' self.occupied = True self['bg'] = 'silver' def rmvRock(self): '''Tile.rmvRock() Removes the rock''' self.occupied = False self['bg'] = 'dodgerblue' def changeColor(self,color): '''Tile.getColor(color) Changes the color of the tile''' self.color = color self['bg'] = self.color self.occupied = True def toggleBorder(self): '''Tile.toggleBorder() Makes the tile a border tile''' self.border = True def highlight(self): '''SplashSquare.highlight() Highlights the square.''' self.selected = True self['highlightbackground'] = 'white' def unHighlight(self): '''SplashSquare.unHighlight() Unhighlights the square.''' self.selected = False self['highlightbackground'] = 'black' class GameFrame(Frame): '''Represents the whole game''' def __init__(self,master): '''SplashFrame(master) -> SplashFrame initiates the Slpashes of Color Frame''' # Initiate the frame Frame.__init__(self,master,bg='black') self.grid() # Store and define variables self.Frame = Frame self.master = master self.tiles = [] self.numRocks = 4 self.rockLocations = [] self.numPlayers = 2 self.playerLocations = [] self.players = [] self.playerColors = ['red', 'green'] self.turn = 0 self.round = 1 self.roundAmount = 3 self.size = [13,12] # Make the squares # For every row for row in range(self.size[0]): rowList = [] # For every column for column in range(self.size[1]): # Store the coords coord = [row,column] # Make the Tile square = Tile(self,coord) rowList.append(square) self.tiles.append(rowList) # Add a Border self.addBorder() # Add players for numPlayer in range(self.numPlayers): self.addPlayer(numPlayer) # Add Rocks for numRock in range(self.numRocks): self.addRock(numRock) # Bind buttons for moving self.bind_all('',self.keyPress) self.bind_all('',self.keyPress) self.bind_all('',self.keyPress) self.bind_all('',self.keyPress) self.bind_all('',self.select) # Highlight the top left square and save it in a variable self.tiles[1][1].highlight() self.currentHighlight = self.tiles[1][1] def addBorder(self): '''GameFrame.addBorder() Adds a border around the board''' for rowlist in self.tiles: for tile in rowlist: if tile.getCoord()[1] == 0 or tile.getCoord()[1] == (self.size[1]-1): tile.changeColor('black') tile.toggleBorder() elif tile.getCoord()[0] == 0 or tile.getCoord()[0] == (self.size[0]-1): tile.changeColor('black') tile.toggleBorder() def addPlayer(self,numPlayer): '''GameFrame.addPlayer() Adds one player in a random spot on the board''' # Get random coords and get tile on those coords playerCoord = [random.randrange(self.size[0]), random.randrange(self.size[1])] player = self.tiles[playerCoord[0]][playerCoord[1]] # If occupied, run again to get another random tile if player.ifOccupied(): self.addPlayer(numPlayer) # Else add this as a player location, change color, and run add player command else: self.playerLocations.append(playerCoord) self.players.append(player) player.changeColor(self.playerColors[numPlayer]) color = 'dark' + self.playerColors[numPlayer] player.addPlayer(color) def addRock(self,numRocks): '''GameFrame.addRock() Adds a Rock in a random spot on the board''' # Get random coords and get tile on those coords rockCoord = [random.randrange(self.size[0]), random.randrange(self.size[1])] rock = self.tiles[rockCoord[0]][rockCoord[1]] # If occupied, run again to get another random tile if rock.ifOccupied(): self.addRock(numRocks) # Else add this as a rock location, change color, and run add rock command else: self.rockLocations.append(rockCoord) rock.addRock() def win(self): '''GameFrame.win() Runs the win command and finds out who wins.''' print('WE HAVE A WINNER!!!') def keyPress(self,info): '''GameFrame.selectMove(info) Moves the highlight of the square.''' key = info.keysym coord = self.currentHighlight.getCoord() tilePossibility = [self.tiles[coord[0]+1][coord[1]],self.tiles[coord[0]-1][coord[1]], self.tiles[coord[0]][coord[1]+1], self.tiles[coord[0]][coord[1]-1]] if key == 'Down' and not tilePossibility[0].ifBorder(): self.currentHighlight.unHighlight() self.currentHighlight = tilePossibility[0] self.currentHighlight.highlight() elif key == 'Up' and not tilePossibility[1].ifBorder(): self.currentHighlight.unHighlight() self.currentHighlight = tilePossibility[1] self.currentHighlight.highlight() elif key == 'Right' and not tilePossibility[2].ifBorder(): self.currentHighlight.unHighlight() self.currentHighlight = tilePossibility[2] self.currentHighlight.highlight() elif key == 'Left' and not tilePossibility[3].ifBorder(): self.currentHighlight.unHighlight() self.currentHighlight = tilePossibility[3] self.currentHighlight.highlight() def select(self, info): '''GameFrame.select(info) Selects the highlighted square.''' self.currentHighlight.clicked('fakecoords') def jump(self,tile): '''GameFrame.jump(tile) Jumps the player from one spot to another.''' # Change color, and put the player where it is supposed to be color = 'dark' + self.playerColors[self.turn] tile.addPlayer(color) self.players[self.turn].rmvPlayer() self.players[self.turn] = tile self.playerLocations[self.turn] = tile.getCoord() def squareclicked(self,tile,coord): '''GameFrame.squareclicked(tile,coord) Alerts the frame that a square has been clicked.''' if self.legal(tile): # Change color, and put the player where it is supposed to be tile.changeColor(self.playerColors[self.turn]) color = 'dark' + self.playerColors[self.turn] tile.addPlayer(color) self.players[self.turn].rmvPlayer() self.players[self.turn] = tile self.playerLocations[self.turn] = tile.getCoord() # Update the turn number, making sure to loop if it's the last number if self.round < self.roundAmount: self.round += 1 else: self.round = 1 if self.turn != (self.numPlayers-1): self.turn += 1 elif self.turn == (self.numPlayers-1): self.turn = 0 # Check if it's a win win = True points = [] for rowlist in self.tiles: for tile in rowlist: if not tile.ifTiled(): win = False if win: self.win() def legal(self, tile): '''GameFrame.legal(tile) -> boolean Checks if adding the square is legal.''' playerCoord = self.playerLocations[self.turn] if not tile.ifOccupied(): if self.tiles[playerCoord[0]+1][playerCoord[1]] == tile: return True elif self.tiles[playerCoord[0]-1][playerCoord[1]] == tile: return True elif self.tiles[playerCoord[0]][playerCoord[1]+1] == tile: return True elif self.tiles[playerCoord[0]][playerCoord[1]-1] == tile: return True else: return False elif tile.ifTiled() == True: self.jump(tile) return False else: return False def Game(): '''Game() Starts the game''' # Makes the root root = Tk() # Gives a title root.title('game') # Starts the game game = GameFrame(root) game.mainloop() Game()