|
| 1 | +import numpy as np |
| 2 | +from tkinter import * |
| 3 | +import pygame |
| 4 | +import sys |
| 5 | +import math |
| 6 | + |
| 7 | +BLACK = (33, 33, 33) |
| 8 | +GREY = (235, 235, 235) |
| 9 | +RED = (255,0,0) |
| 10 | +YELLOW = (255,255,0) |
| 11 | + |
| 12 | +ROW_COUNT = 6 |
| 13 | +COLUMN_COUNT = 7 |
| 14 | + |
| 15 | +player_names = [] |
| 16 | + |
| 17 | +#adding players |
| 18 | +def submit(): |
| 19 | + player_names.append(player_one.get()) |
| 20 | + player_names.append(player_two.get()) |
| 21 | + root.destroy() |
| 22 | + |
| 23 | +#creating connect 4 board 6 X 7 common board size |
| 24 | +def create_board(): |
| 25 | + board = np.zeros((ROW_COUNT,COLUMN_COUNT)) |
| 26 | + return board |
| 27 | + |
| 28 | +def drop_piece(board, row, col, piece): |
| 29 | + board[row][col] = piece |
| 30 | + |
| 31 | +def is_valid_location(board, col): |
| 32 | + return board[ROW_COUNT-1][col] == 0 |
| 33 | + |
| 34 | +def get_next_open_row(board, col): |
| 35 | + for r in range(ROW_COUNT): |
| 36 | + if board[r][col] == 0: |
| 37 | + return r |
| 38 | + |
| 39 | + |
| 40 | +def winning_move(board, piece): |
| 41 | + # Check horizontal locations for win |
| 42 | + for c in range(COLUMN_COUNT-3): |
| 43 | + for r in range(ROW_COUNT): |
| 44 | + if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece: |
| 45 | + return True |
| 46 | + |
| 47 | + # Check vertical locations for win |
| 48 | + for c in range(COLUMN_COUNT): |
| 49 | + for r in range(ROW_COUNT-3): |
| 50 | + if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece: |
| 51 | + return True |
| 52 | + |
| 53 | + # Check positively sloped diaganols |
| 54 | + for c in range(COLUMN_COUNT-3): |
| 55 | + for r in range(ROW_COUNT-3): |
| 56 | + if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece: |
| 57 | + return True |
| 58 | + |
| 59 | + # Check negatively sloped diaganols |
| 60 | + for c in range(COLUMN_COUNT-3): |
| 61 | + for r in range(3, ROW_COUNT): |
| 62 | + if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece: |
| 63 | + return True |
| 64 | + |
| 65 | +def draw_board(board): |
| 66 | + for c in range(COLUMN_COUNT): |
| 67 | + for r in range(ROW_COUNT): |
| 68 | + pygame.draw.rect(screen, BLACK, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE)) |
| 69 | + pygame.draw.circle(screen, GREY, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS) |
| 70 | + |
| 71 | + for c in range(COLUMN_COUNT): |
| 72 | + for r in range(ROW_COUNT): |
| 73 | + if board[r][c] == 1: |
| 74 | + pygame.draw.circle(screen, RED, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS) |
| 75 | + elif board[r][c] == 2: |
| 76 | + pygame.draw.circle(screen, YELLOW, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS) |
| 77 | + pygame.display.update() |
| 78 | + |
| 79 | +try: |
| 80 | + |
| 81 | + root = Tk() |
| 82 | + root.resizable(0,0) |
| 83 | + player_one = StringVar(master=root) |
| 84 | + player_two = StringVar(master=root) |
| 85 | + root.geometry("500x300") |
| 86 | + root.title("Connect 4 Game") |
| 87 | + Label(master=root, text="Connect 4 Game", font=('Poppins', 30, 'normal')).place(x=80, y=20) |
| 88 | + Label(master=root, text="Enter player 1 name: ").place(x=120, y=100) |
| 89 | + Entry(master=root, textvariable = player_one,font=('calibre',10,'normal')).place(x=250, y=100) |
| 90 | + Label(master=root, text="Enter player 2 name: ").place(x=120, y=140) |
| 91 | + Entry(master=root, textvariable=player_two, font=('calibre', 10, 'normal')).place(x=250, y=140) |
| 92 | + Button(master=root, text="ADD", width=10, command=submit).place(x=210,y=190) |
| 93 | + root.mainloop() |
| 94 | + |
| 95 | + if player_names[0] != '' and player_names[1] != '': |
| 96 | + |
| 97 | + board = create_board() |
| 98 | + |
| 99 | + game_over = False |
| 100 | + turn = 0 |
| 101 | + |
| 102 | + pygame.init() |
| 103 | + |
| 104 | + SQUARESIZE = 100 |
| 105 | + |
| 106 | + width = COLUMN_COUNT * SQUARESIZE |
| 107 | + height = (ROW_COUNT+1) * SQUARESIZE |
| 108 | + |
| 109 | + size = (width, height) |
| 110 | + |
| 111 | + RADIUS = int(SQUARESIZE/2 - 5) |
| 112 | + |
| 113 | + screen = pygame.display.set_mode(size) |
| 114 | + draw_board(board) |
| 115 | + pygame.display.update() |
| 116 | + |
| 117 | + myfont = pygame.font.SysFont("monospace", 30) |
| 118 | + |
| 119 | + while not game_over: |
| 120 | + |
| 121 | + for event in pygame.event.get(): |
| 122 | + if event.type == pygame.QUIT: |
| 123 | + sys.exit() |
| 124 | + |
| 125 | + if event.type == pygame.MOUSEMOTION: |
| 126 | + pygame.draw.rect(screen, GREY, (0,0, width, SQUARESIZE)) |
| 127 | + posx = event.pos[0] |
| 128 | + if turn == 0: |
| 129 | + pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), RADIUS) |
| 130 | + else: |
| 131 | + pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE/2)), RADIUS) |
| 132 | + pygame.display.update() |
| 133 | + |
| 134 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 135 | + pygame.draw.rect(screen, GREY, (0,0, width, SQUARESIZE)) |
| 136 | + |
| 137 | + # Ask for Player 1 Input |
| 138 | + if turn == 0: |
| 139 | + posx = event.pos[0] |
| 140 | + col = int(math.floor(posx/SQUARESIZE)) |
| 141 | + |
| 142 | + if is_valid_location(board, col): |
| 143 | + row = get_next_open_row(board, col) |
| 144 | + drop_piece(board, row, col, 1) |
| 145 | + turn += 1 |
| 146 | + turn = turn % 2 |
| 147 | + |
| 148 | + if winning_move(board, 1): |
| 149 | + label = myfont.render(player_names[0] + " wins the game", 1, RED) |
| 150 | + screen.blit(label, (40,10)) |
| 151 | + game_over = True |
| 152 | + |
| 153 | + |
| 154 | + # # Ask for Player 2 Input |
| 155 | + else: |
| 156 | + posx = event.pos[0] |
| 157 | + col = int(math.floor(posx/SQUARESIZE)) |
| 158 | + |
| 159 | + if is_valid_location(board, col): |
| 160 | + row = get_next_open_row(board, col) |
| 161 | + drop_piece(board, row, col, 2) |
| 162 | + turn += 1 |
| 163 | + turn = turn % 2 |
| 164 | + |
| 165 | + if winning_move(board, 2): |
| 166 | + label = myfont.render(player_names[1] + " wins the game", 1, YELLOW) |
| 167 | + screen.blit(label, (40,10)) |
| 168 | + game_over = True |
| 169 | + |
| 170 | + |
| 171 | + draw_board(board) |
| 172 | + |
| 173 | + |
| 174 | + |
| 175 | + if game_over: |
| 176 | + pygame.time.wait(3000) |
| 177 | + |
| 178 | +except IndexError as e: |
| 179 | + print('Game closed') |
0 commit comments