|
| 1 | +from ctypes.wintypes import PINT |
| 2 | +from pickle import TRUE |
| 3 | +from numpy import flip, source |
| 4 | +import pygame |
| 5 | +import game_config as gc |
| 6 | +from process import TicTacToe as T |
| 7 | +from pygame import display, event, image |
| 8 | +from time import sleep |
| 9 | +import Images |
| 10 | + |
| 11 | +def initial() -> pygame.Surface: |
| 12 | + pygame.init() |
| 13 | + display.set_caption('Tic-Tac-Toe') |
| 14 | + screen = display.set_mode((gc.SCREEN_SIZE, gc.SCREEN_SIZE)) |
| 15 | + print( type(screen) ) |
| 16 | + return screen |
| 17 | + |
| 18 | +def find_xy(x, y): |
| 19 | + row = y // gc.IMAGE_SIZE |
| 20 | + col = x // gc.IMAGE_SIZE |
| 21 | + return row, col |
| 22 | + |
| 23 | +def update_board_display(screen : pygame.Surface, Game : T ):#Update only |
| 24 | + screen.blit(image.load('assets/blank.png'), (0, 0)) |
| 25 | + #sleep(1) |
| 26 | + screen.fill((0, 0, 0)) |
| 27 | + for i in range(gc.NUM_TILES_SIDE): |
| 28 | + for j in range(gc.NUM_TILES_SIDE): |
| 29 | + tile = Images.Image(Game.board[i][j]) |
| 30 | + screen.blit(tile.image, (j * gc.IMAGE_SIZE + gc.MARGIN, i * gc.IMAGE_SIZE + gc.MARGIN)) |
| 31 | + display.flip() |
| 32 | + #sleep(1) |
| 33 | + |
| 34 | +def GameOver(Game : T,screen : pygame.Surface) -> bool: |
| 35 | + val = Game.CheckWin() |
| 36 | + if val == 1: |
| 37 | + display.flip() |
| 38 | + update_board_display(screen,Game=Game) |
| 39 | + sleep(1.5) |
| 40 | + screen.blit(image.load('assets/win.png'), (0, 0)) |
| 41 | + display.flip() |
| 42 | + sleep(2.3) |
| 43 | + return True |
| 44 | + if val == 0: |
| 45 | + display.flip() |
| 46 | + update_board_display(screen,Game=Game) |
| 47 | + sleep(1.5) |
| 48 | + screen.blit(image.load('assets/lose.png'), (0, 0)) |
| 49 | + display.flip() |
| 50 | + sleep(2.3) |
| 51 | + return TRUE |
| 52 | + return False |
| 53 | + |
| 54 | + |
| 55 | +def run(Game : T,screen : pygame.Surface, running : bool): |
| 56 | + update_board_display(screen,Game) |
| 57 | + display.flip() |
| 58 | + while running: |
| 59 | + current_events = event.get() |
| 60 | + for e in current_events: |
| 61 | + if e.type == pygame.QUIT:# clicked X |
| 62 | + running = False |
| 63 | + |
| 64 | + if e.type == pygame.KEYDOWN: #keyboard |
| 65 | + if e.key == pygame.K_ESCAPE:# Esc to end it |
| 66 | + running = False |
| 67 | + |
| 68 | + if e.type == pygame.MOUSEBUTTONDOWN: #clickd |
| 69 | + mouse_x, mouse_y = pygame.mouse.get_pos()# got position |
| 70 | + row, col = find_xy(mouse_x, mouse_y)#location of click |
| 71 | + if row >= gc.NUM_TILES_SIDE or col >= gc.NUM_TILES_SIDE:# if it's on screen |
| 72 | + continue |
| 73 | + if Game.MoveRecord(row,col) == True:#if the move is possible |
| 74 | + if GameOver(Game,screen):#if game ends with it |
| 75 | + running = False |
| 76 | + break |
| 77 | + #game is still on |
| 78 | + a,b = Game.NextMove()# computer makes the move |
| 79 | + print(a,b, " here ") |
| 80 | + if a == -1 and b == -1: #special condition for tie |
| 81 | + running = False #end the game |
| 82 | + screen.blit(image.load('assets/tie.png'), (0, 0)) |
| 83 | + display.flip() |
| 84 | + sleep(2.3) |
| 85 | + break; |
| 86 | + else:#not a tie |
| 87 | + if GameOver(Game,screen):#if game ends with it |
| 88 | + running = False |
| 89 | + break |
| 90 | + |
| 91 | + update_board_display(screen=screen,Game=Game) |
| 92 | + display.flip() |
| 93 | + sleep(2.1) |
| 94 | + |
| 95 | + #screen.blit(tile.image, (j * gc.IMAGE_SIZE + gc.MARGIN, i * gc.IMAGE_SIZE + gc.MARGIN)) |
| 96 | + |
| 97 | +def X_or_O(screen : pygame.Surface) -> str: |
| 98 | + X = Images.Image("x") |
| 99 | + O = Images.Image("o") |
| 100 | + #pygame.transform.scale() |
| 101 | + # Surface, (width, height) -> Surface |
| 102 | + X.image = pygame.transform.scale(X.image,(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE)) |
| 103 | + O.image = pygame.transform.scale(O.image,(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE)) |
| 104 | + screen.blit(X.image, (gc.MARGIN, gc.MARGIN)) |
| 105 | + screen.blit(O.image, (gc.MARGIN +gc.SCREEN_SIZE//2 , gc.MARGIN)) |
| 106 | + #screen.blits( blit_sequence=[ (X.image , (0,0) , pygame.Rect(0,0,gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2)), (O.image , (gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2) , pygame.Rect(gc.SCREEN_SIZE//2,gc.SCREEN_SIZE//2,gc.SCREEN_SIZE,gc.SCREEN_SIZE)) ] ) |
| 107 | + display.flip() |
| 108 | + pick = "-1" |
| 109 | + while True: |
| 110 | + current_events = event.get() |
| 111 | + for e in current_events: |
| 112 | + if e.type == pygame.QUIT:# clicked X |
| 113 | + return pick |
| 114 | + |
| 115 | + if e.type == pygame.KEYDOWN: #keyboard |
| 116 | + if e.key == pygame.K_ESCAPE:# Esc to end it |
| 117 | + return pick |
| 118 | + if e.type == pygame.MOUSEBUTTONDOWN: |
| 119 | + x,y = pygame.mouse.get_pos() |
| 120 | + if (x < gc.SCREEN_SIZE and x >= 0) or (y < gc.SCREEN_SIZE and y > 0):# if it's on screen |
| 121 | + if x > gc.SCREEN_SIZE//2: |
| 122 | + pick = "O" |
| 123 | + else: pick = "X" |
| 124 | + screen.fill((0,0,0)) |
| 125 | + display.flip() |
| 126 | + sleep(1.1) |
| 127 | + return pick |
| 128 | + return pick |
| 129 | + |
| 130 | + |
| 131 | +def play(): |
| 132 | + screen = initial() |
| 133 | + running = True |
| 134 | + pick = X_or_O(screen=screen) |
| 135 | + if pick == "-1": |
| 136 | + return |
| 137 | + Game = T(pick,gc.NUM_TILES_SIDE) |
| 138 | + run(Game,screen,running) |
| 139 | + |
| 140 | +if __name__ == "__main__": |
| 141 | + |
| 142 | + print("Hello World!") |
| 143 | + play() |
| 144 | + print('Goodbye!') |
| 145 | + |
0 commit comments