Amazing-Python-Scripts
221 строка · 7.1 Кб
1# -*- coding: utf-8 -*-
2"""
3Tic Toe Using pygame , numpy and sys with Graphical User Interface
4"""
5import pygame6import sys7from pygame.locals import *8import numpy as np9# ------
10# constants
11# -------
12width = 80013height = 80014# row and columns
15board_rows = 316board_columns = 317cross_width = 2518square_size = width//board_columns19# colors in RGB format
20line_Width = 1521red = (255, 0, 0)22bg_color = (28, 170, 156)23line_color = (23, 145, 135)24circle_color = (239, 231, 200)25cross_color = (66, 66, 66)26space = square_size//427# circle
28circle_radius = square_size//329circle_width = 1430pygame.init()31screen = pygame.display.set_mode((height, width))32pygame.display.set_caption('Tic Tac Toe!')33screen.fill(bg_color)34# color to display restart
35white = (255, 255, 255)36green = (0, 255, 0)37blue = (0, 0, 128)38
39font = pygame.font.Font('freesansbold.ttf', 25)40
41# create a text suface object,
42# on which text is drawn on it.
43text = font.render('Press R to restart', True, green, blue)44
45Won = font.render(" Won", True, blue, green)46leave = font.render("Press X to Exit", True, white, red)47# create a rectangular object for the
48# text surface object
49leaveRect = text.get_rect()50textRect = text.get_rect()51winRect = Won.get_rect()52winRect.center = (100, 30)53textRect.center = (width-400, 30)54leaveRect.center = (width-120, 30)55board = np.zeros((board_rows, board_columns))56# print(board)
57# pygame.draw.line( screen ,red ,(10,10),(300,300),10)
58
59
60def draw_figures():61for row in range(board_rows):62for col in range(board_columns):63if board[row][col] == 1:64pygame.draw.circle(screen, circle_color, (int(col*square_size + square_size//2), int(65row*square_size + square_size//2)), circle_radius, circle_width)66elif board[row][col] == 2:67pygame.draw.line(screen, cross_color, (col*square_size + space, row*square_size + square_size -68space), (col*square_size+square_size - space, row*square_size + space), cross_width)69pygame.draw.line(screen, cross_color, (col*square_size + space, row*square_size + space),70(col*square_size + square_size - space, row*square_size + square_size - space), cross_width)71
72
73def draw_lines():74pygame.draw.line(screen, line_color, (0, square_size),75(width, square_size), line_Width)76# 2nd horizontal line77pygame.draw.line(screen, line_color, (0, 2*square_size),78(width, 2*square_size), line_Width)79# 1st verticle80pygame.draw.line(screen, line_color, (square_size, 0),81(square_size, height), line_Width)82# 2nd verticle83pygame.draw.line(screen, line_color, (2*square_size, 0),84(2*square_size, height), line_Width)85
86# To mark which square player has chosen
87
88
89def mark_square(row, col, player):90board[row][col] = player91
92# TO check the availablity of a square
93
94
95def available_square(row, col):96return board[row][col] == 097
98# check board full or not
99
100
101def is_board_full():102k = False103for row in range(board_rows):104for col in range(board_columns):105if board[row][col] == 0:106k = False107else:108k = True109return k110
111
112def check_win(player):113# check verticle win114for col in range(board_columns):115if board[0][col] == player and board[1][col] == player and board[2][col] == player:116draw_vertical_winning_line(col, player)117return True118# check Horizontal win119for row in range(board_rows):120if board[row][0] == player and board[row][1] == player and board[row][2] == player:121draw_horizontal_winning_line(row, player)122return True123# check for asc win124if board[2][0] == player and board[1][1] == player and board[0][2] == player:125draw_asc_diagonal(player)126return True127if board[0][0] == player and board[1][1] == player and board[2][2] == player:128draw_des_diagonal(player)129return True130
131
132def draw_horizontal_winning_line(row, player):133posY = row*square_size + square_size//2134
135if (player == 1):136color = circle_color137else:138color = cross_color139
140pygame.draw.line(screen, color, (15, posY), (width-15, posY), 15)141
142
143def draw_vertical_winning_line(col, player):144posX = col*square_size + square_size//2145if (player == 1):146color = circle_color147else:148color = cross_color149pygame.draw.line(screen, color, (posX, 15), (posX, width-15), 15)150
151
152def draw_asc_diagonal(player):153if (player == 1):154color = circle_color155else:156color = cross_color157pygame.draw.line(screen, color, (15, height-15), (width-15, 15), 15)158
159
160def draw_des_diagonal(player):161if (player == 1):162color = circle_color163else:164color = cross_color165pygame.draw.line(screen, color, (15, 15), (width-15, height-15), 15)166
167
168def restart():169screen.fill(bg_color)170draw_lines()171player = 1172for row in range(board_rows):173for col in range(board_columns):174board[row][col] = 0175
176
177draw_lines()178# player
179player = 1180game_over = False181while True: # main game loop182for event in pygame.event.get(): # constantly looks for the event183if event.type == pygame.QUIT: # if user clicks exit pygame.QUIT and sys exits184pygame.quit()185sys.exit()186board_full = is_board_full()187if board_full and not game_over:188Won = font.render(" It's a Tie ", True, blue, green)189screen.blit(Won, winRect)190screen.blit(text, textRect)191screen.blit(leave, leaveRect)192if event.type == pygame.MOUSEBUTTONDOWN and not game_over:193mouseX = event.pos[0] # x194mouseY = event.pos[1] # y195clicked_row = int(mouseY // square_size)196clicked_column = int(mouseX // square_size)197if available_square(clicked_row, clicked_column):198mark_square(clicked_row, clicked_column, player)199if (check_win(player)):200game_over = True201Won = font.render("Player"+str(player) +202" Won ", True, blue, green)203screen.blit(Won, winRect)204screen.blit(text, textRect)205screen.blit(leave, leaveRect)206player = player % 2 + 1207if not game_over and not board_full:208Won = font.render("Player"+str(player) +209" Turn ", True, blue, green)210screen.blit(Won, winRect)211draw_figures()212# to restart the game213if event.type == pygame.KEYDOWN:214if event.key == pygame.K_r:215restart()216game_over = False217elif event.key == pygame.K_x:218pygame.quit()219sys.exit()220# print(board)221pygame.display.update()222