|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +""" |
| 3 | +Tic Toe Using pygame , numpy and sys with Graphical User Interface |
| 4 | +""" |
| 5 | +import pygame, sys |
| 6 | +from pygame.locals import * |
| 7 | +import numpy as np |
| 8 | +#------ |
| 9 | +#constants |
| 10 | +#------- |
| 11 | +width=800 |
| 12 | +height=800 |
| 13 | +#row and columns |
| 14 | +board_rows=3 |
| 15 | +board_columns=3 |
| 16 | +cross_width=25 |
| 17 | +square_size=width//board_columns |
| 18 | +#colors in RGB format |
| 19 | +line_Width=15 |
| 20 | +red=(255, 0, 0) |
| 21 | +bg_color=(28, 170, 156) |
| 22 | +line_color=(23, 145, 135) |
| 23 | +circle_color=(239,231,200) |
| 24 | +cross_color=(66,66,66) |
| 25 | +space=square_size//4 |
| 26 | +#circle |
| 27 | +circle_radius=square_size//3 |
| 28 | +circle_width=14 |
| 29 | +pygame.init() |
| 30 | +screen = pygame.display.set_mode((height,width)) |
| 31 | +pygame.display.set_caption('Tic Tac Toe!') |
| 32 | +screen.fill( bg_color ) |
| 33 | +#color to display restart |
| 34 | +white = (255, 255, 255) |
| 35 | +green = (0, 255, 0) |
| 36 | +blue = (0, 0, 128) |
| 37 | + |
| 38 | +font = pygame.font.Font('freesansbold.ttf', 25) |
| 39 | + |
| 40 | +# create a text suface object, |
| 41 | +# on which text is drawn on it. |
| 42 | +text = font.render('Press R to restart', True, green, blue) |
| 43 | + |
| 44 | +Won= font.render(" Won", True ,blue,green) |
| 45 | +leave=font.render("Press X to Exit" ,True, white, red) |
| 46 | +# create a rectangular object for the |
| 47 | +# text surface object |
| 48 | +leaveRect=text.get_rect() |
| 49 | +textRect = text.get_rect() |
| 50 | +winRect=Won.get_rect() |
| 51 | +winRect.center=(100,30) |
| 52 | +textRect.center = (width-400, 30) |
| 53 | +leaveRect.center=(width-120,30) |
| 54 | +board=np.zeros( (board_rows,board_columns)) |
| 55 | +#print(board) |
| 56 | +#pygame.draw.line( screen ,red ,(10,10),(300,300),10) |
| 57 | + |
| 58 | +def draw_figures(): |
| 59 | + for row in range(board_rows): |
| 60 | + for col in range(board_columns): |
| 61 | + if board[row][col]==1: |
| 62 | + pygame.draw.circle(screen,circle_color,( int(col*square_size + square_size//2 ),int(row*square_size +square_size//2)),circle_radius,circle_width) |
| 63 | + elif board[row][col]==2: |
| 64 | + pygame.draw.line( screen ,cross_color ,(col*square_size + space,row*square_size +square_size -space),(col*square_size+square_size -space,row*square_size +space),cross_width) |
| 65 | + pygame.draw.line( screen ,cross_color ,(col*square_size +space,row*square_size +space),(col*square_size +square_size -space,row*square_size +square_size -space),cross_width) |
| 66 | + |
| 67 | +def draw_lines(): |
| 68 | + pygame.draw.line( screen ,line_color ,(0,square_size),(width,square_size),line_Width) |
| 69 | + #2nd horizontal line |
| 70 | + pygame.draw.line( screen ,line_color ,(0,2*square_size),(width,2*square_size),line_Width) |
| 71 | + #1st verticle |
| 72 | + pygame.draw.line( screen ,line_color ,(square_size,0),(square_size,height),line_Width) |
| 73 | + #2nd verticle |
| 74 | + pygame.draw.line( screen ,line_color ,(2*square_size,0),(2*square_size,height),line_Width) |
| 75 | + |
| 76 | +#To mark which square player has chosen |
| 77 | +def mark_square(row,col,player): |
| 78 | + board[row][col]=player |
| 79 | + |
| 80 | +# TO check the availablity of a square |
| 81 | +def available_square(row,col): |
| 82 | + return board[row][col]==0 |
| 83 | + |
| 84 | +#check board full or not |
| 85 | +def is_board_full(): |
| 86 | + k=False |
| 87 | + for row in range(board_rows): |
| 88 | + for col in range(board_columns): |
| 89 | + if board[row][col]==0: |
| 90 | + k=False |
| 91 | + else: |
| 92 | + k=True |
| 93 | + return k |
| 94 | + |
| 95 | +def check_win(player): |
| 96 | + #check verticle win |
| 97 | + for col in range(board_columns): |
| 98 | + if board[0][col]==player and board[1][col]==player and board[2][col]==player: |
| 99 | + draw_vertical_winning_line(col, player) |
| 100 | + return True |
| 101 | + #check Horizontal win |
| 102 | + for row in range(board_rows): |
| 103 | + if board[row][0]==player and board[row][1]==player and board[row][2]==player: |
| 104 | + draw_horizontal_winning_line(row, player) |
| 105 | + return True |
| 106 | + #check for asc win |
| 107 | + if board[2][0]==player and board[1][1]==player and board[0][2]==player: |
| 108 | + draw_asc_diagonal(player) |
| 109 | + return True |
| 110 | + if board[0][0]==player and board[1][1]==player and board[2][2]==player: |
| 111 | + draw_des_diagonal(player) |
| 112 | + return True |
| 113 | + |
| 114 | +def draw_horizontal_winning_line(row,player): |
| 115 | + posY=row*square_size +square_size//2 |
| 116 | + |
| 117 | + if(player==1): |
| 118 | + color=circle_color |
| 119 | + else: |
| 120 | + color=cross_color |
| 121 | + |
| 122 | + pygame.draw.line(screen, color, (15,posY), (width-15,posY),15) |
| 123 | + |
| 124 | +def draw_vertical_winning_line(col,player): |
| 125 | + posX=col*square_size +square_size//2 |
| 126 | + if(player==1): |
| 127 | + color=circle_color |
| 128 | + else: |
| 129 | + color=cross_color |
| 130 | + pygame.draw.line(screen, color, (posX,15), (posX,width-15),15) |
| 131 | +def draw_asc_diagonal(player): |
| 132 | + if(player==1): |
| 133 | + color=circle_color |
| 134 | + else: |
| 135 | + color=cross_color |
| 136 | + pygame.draw.line(screen,color,(15,height-15),(width-15,15),15) |
| 137 | +def draw_des_diagonal(player): |
| 138 | + if(player==1): |
| 139 | + color=circle_color |
| 140 | + else: |
| 141 | + color=cross_color |
| 142 | + pygame.draw.line(screen,color,(15,15),(width-15,height-15),15) |
| 143 | + |
| 144 | +def restart(): |
| 145 | + screen.fill(bg_color) |
| 146 | + draw_lines() |
| 147 | + player = 1 |
| 148 | + for row in range(board_rows): |
| 149 | + for col in range(board_columns): |
| 150 | + board[row][col]=0 |
| 151 | +draw_lines() |
| 152 | +#player |
| 153 | +player=1 |
| 154 | +game_over=False |
| 155 | +while True: # main game loop |
| 156 | + for event in pygame.event.get(): #constantly looks for the event |
| 157 | + if event.type == pygame.QUIT: #if user clicks exit pygame.QUIT and sys exits |
| 158 | + pygame.quit() |
| 159 | + sys.exit() |
| 160 | + board_full=is_board_full() |
| 161 | + if board_full and not game_over: |
| 162 | + Won= font.render(" It's a Tie ", True ,blue,green) |
| 163 | + screen.blit(Won, winRect) |
| 164 | + screen.blit(text, textRect) |
| 165 | + screen.blit(leave,leaveRect) |
| 166 | + if event.type==pygame.MOUSEBUTTONDOWN and not game_over: |
| 167 | + mouseX= event.pos[0] #x |
| 168 | + mouseY= event.pos[1] #y |
| 169 | + clicked_row=int(mouseY // square_size) |
| 170 | + clicked_column=int(mouseX // square_size) |
| 171 | + if available_square(clicked_row, clicked_column): |
| 172 | + mark_square(clicked_row,clicked_column, player) |
| 173 | + if(check_win(player)): |
| 174 | + game_over=True |
| 175 | + Won= font.render("Player"+str(player)+" Won ", True ,blue,green) |
| 176 | + screen.blit(Won, winRect) |
| 177 | + screen.blit(text, textRect) |
| 178 | + screen.blit(leave,leaveRect) |
| 179 | + player=player%2 +1 |
| 180 | + if not game_over and not board_full: |
| 181 | + Won= font.render("Player"+str(player)+" Turn ", True ,blue,green) |
| 182 | + screen.blit(Won, winRect) |
| 183 | + draw_figures() |
| 184 | + #to restart the game |
| 185 | + if event.type==pygame.KEYDOWN: |
| 186 | + if event.key==pygame.K_r: |
| 187 | + restart() |
| 188 | + game_over=False |
| 189 | + elif event.key==pygame.K_x: |
| 190 | + pygame.quit() |
| 191 | + sys.exit() |
| 192 | + #print(board) |
| 193 | + pygame.display.update() |
| 194 | + |
0 commit comments