def initialize_board(screen, player1, player2, against): """ To initialize the board. """ screen.fill(screenfill) srect = pygame.Rect(0, 5, hsize, 55) brect = pygame.Rect(margin_left, margin_top, box_width*3, box_height*3) pygame.draw.rect(screen, fill_color, srect) pygame.draw.rect(screen, fill_color, brect) #buttons title = TextInput("Tic-Tac-Toe", hsize//2 - 130, 10, white, 60) if against == "is_human": pl1, pl2 = "Player 1", "Player 2" elif against == "is_random": pl1, pl2 = "Player", "Random agent" elif against == "is_ai": pl1, pl2 = "Player", "AI agent" players_def1 = Button(pl1 +" - " + player1, hsize//2-300, 100, white, 36, fill_color) players_def2 = Button(pl2+" - " + player2, hsize//2+120, 100, white, 36, fill_color) #vertical and horizontal lines pygame.draw.line(screen, white, (margin_left, margin_top+box_height), (hsize-margin_right, margin_top+box_height), 5) pygame.draw.line(screen, white, (margin_left, margin_top+box_height*2), (hsize-margin_right, margin_top+box_height*2), 5) pygame.draw.line(screen, white, (margin_left+box_width, margin_top), (margin_left+box_width, vsize-margin_bottom), 5) pygame.draw.line(screen, white, (hsize-margin_right-box_width, margin_top), (hsize-margin_right-box_width, vsize-margin_bottom), 5) title.add_text(screen) players_def1.add_button(screen) players_def2.add_button(screen) return screen
def show_board(screen, message, p1, p2): """ To show the board with current status. """ st1 = str(p1.wins) st2 = str(p2.wins) msg = Button(message, hsize//2 - 150, vsize-70, white, 48, fill_color) stat1 = Button(st1, hsize//2 - 300, margin_top-50, white, 48, fill_color) stat2 = Button(st2, hsize//2 +250, margin_top-50, white, 48, fill_color) msg.add_button(screen) stat1.add_button(screen) stat2.add_button(screen) return screen
Xbutton = Button("X", hsize//2 - 160, 550, white, 100, fill_color) Obutton = Button("O", hsize//2+100, 550, white, 100, fill_color) choose = TextInput("Choose Player: ", hsize//2-200, 400, fill_color, 60) again = Button("Play Again?", hsize//2 - 300, vsize//2, white, 60, fill_color) newgame = Button("New Game", hsize//2+50, vsize//2, white, 60, fill_color() versus_ai = Button("Versus AI", hsize//2-300, 500, white, 80, fill_color) versus_hu = Button("Versus Human", hsize//2-300, 600, white, 80, fill_color) versus_ra = Button("Versus Random agent", hsize//2-300, 700, white, 80, fill_color) def first_screen(screen): screen.fill(screenfill) srect = pygame.Rect(0, 80, hsize, 100) trect = pygame.Rect(0, 450, hsize, 350) pygame.draw.rect(screen, fill_color, srect) title.add_button(screen) versus_ai.add_button(screen) versus_hu.add_button(screen) versus_ra.add_button(screen) return screen def end_screen(screen): srect = pygame.Rect(0, vsize//2-30, hsize, 100) pygame.draw.rect(screen, gray3, srect) again.add_button(screen) newgame.add_button(screen) return screen def current_player(player, pos = None, e = None):