class Game: def __init__(self): self.graphics = Graphics() self.board = Board() self.selected_piece = None def setup(self): self.graphics.setup_window() def event_loop(self): self.mouse_pos = self.graphics.board_coords(pygame.mouse.get_pos()) for event in pygame.event.get(): if event.type == QUIT: self.quit() if event.type == MOUSEBUTTONDOWN: if self.board.location(self.mouse_pos).occupant != None and \ self.board.location(self.mouse_pos).occupant.color == self.turn: self.selected_piece = self.mouse_pos elif self.selected_piece != None and self.mouse_pos in \ self.board.legal_moves(self.selected_piece): self.board.move_piece(self.selected_piece, self.mouse_pos) else: self.end_turn() def update(self): self.graphics.update(self.board) def quit(self): pygame.quit() sys.exit() def main(self): self.setup() while True: self.event_loop() self.update() def end_turn(self): if self.turn == BLUE: self.turn = RED else: self.turn = BLUE
class Game: """ The main game control. """ def __init__(self): self.graphics = Graphics() self.board = Board() self.turn = Color.BLUE self.selected_piece = None # a board location. self.hop = False self.selected_legal_moves = [] def setup(self): """Draws the window and board at the beginning of the game""" self.graphics.setup_window() def event_loop(self): """ The event loop. This is where events are triggered (like a mouse click) and then effect the game state. """ self.mouse_pos = self.graphics.board_coords( pygame.mouse.get_pos()) # what square is the mouse in? if self.selected_piece != None: self.selected_legal_moves = self.board.legal_moves( self.selected_piece, self.hop) for event in pygame.event.get(): if event.type == pygame.locals.QUIT: self.terminate_game() if event.type == pygame.locals.MOUSEBUTTONDOWN: if self.hop == False: if self.board.location( self.mouse_pos ).occupant != None and self.board.location( self.mouse_pos).occupant.color == self.turn: self.selected_piece = self.mouse_pos elif self.selected_piece != None and self.mouse_pos in self.board.legal_moves( self.selected_piece): self.board.move_piece(self.selected_piece, self.mouse_pos) if self.mouse_pos not in self.board.adjacent( self.selected_piece): self.board.remove_piece(( self.selected_piece[0] + (self.mouse_pos[0] - self.selected_piece[0]) // 2, self.selected_piece[1] + (self.mouse_pos[1] - self.selected_piece[1]) // 2)) self.hop = True self.selected_piece = self.mouse_pos else: self.end_turn() if self.hop == True: if self.selected_piece != None and self.mouse_pos in self.board.legal_moves( self.selected_piece, self.hop): self.board.move_piece(self.selected_piece, self.mouse_pos) self.board.remove_piece(( self.selected_piece[0] + (self.mouse_pos[0] - self.selected_piece[0]) // 2, self.selected_piece[1] + (self.mouse_pos[1] - self.selected_piece[1]) // 2)) if self.board.legal_moves(self.mouse_pos, self.hop) == []: self.end_turn() else: self.selected_piece = self.mouse_pos def update(self): """Calls on the graphics class to update the game display.""" self.graphics.update_display(self.board, self.selected_legal_moves, self.selected_piece) def terminate_game(self): """Quits the program and ends the game.""" pygame.quit() sys.exit def main(self): """"This executes the game and controls its flow.""" self.setup() while True: # main game loop self.event_loop() self.update() def end_turn(self): """ End the turn. Switches the current player. end_turn() also checks for and game and resets a lot of class attributes. """ if self.turn == Color.BLUE: self.turn = Color.RED else: self.turn = Color.BLUE self.selected_piece = None self.selected_legal_moves = [] self.hop = False if self.check_for_endgame(): if self.turn == Color.BLUE: self.graphics.draw_message("RED WINS!") else: self.graphics.draw_message("BLUE WINS!") def check_for_endgame(self): """ Checks to see if a player has run out of moves or pieces. If so, then return True. Else return False. """ for x in range(8): for y in range(8): if self.board.location( (x, y)).color == Color.BLACK and self.board.location( (x, y)).occupant != None and self.board.location( (x, y)).occupant.color == self.turn: if self.board.legal_moves((x, y)) != []: return False return True