Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import pygame
- SCREEN_WIDTH, SCEEN_HEIGHT = 500, 600
- screen = pygame.display.set_mode((SCREEN_WIDTH, SCEEN_HEIGHT))
- pygame.display.set_caption("Tic Tac Toe")
- clock = pygame.time.Clock()
- pygame.init()
- pygame.font.init()
- my_font = pygame.font.SysFont('impact', 35)
- win = my_font.render("YOU WIN ", True, (150, 150, 150))
- lose = my_font.render("YOU LOSE", True, (150, 150, 150))
- introimage = pygame.image.load('veryintroimage.png')
- tietext = my_font.render("Tie Game! Press space to return", True, (150, 150, 150)) #Tie message
- wintext = my_font.render("You win! Press space to return", True, (150, 150, 150)) #win message
- losetext = my_font.render("You Lose!! Press space to return", True, (150, 150, 150)) #Lose text
- XWin = my_font.render("X Has won the game!", True, (150, 150, 150)) #Lose text
- OWin = my_font.render("O Has won the game!", True, (150, 150, 150)) #Lose text
- choose = pygame.image.load('choose.png') #IMAGE
- friend = pygame.image.load('friend.png')
- hard = pygame.image.load('hard.png')
- normal = pygame.image.load('normal.png')
- X = pygame.image.load('x.png')
- O = pygame.image.load('o.png')
- grid = pygame.image.load('grid.png')
- play = pygame.image.load('play.png')#IMAGE
- class Click: #Changes screens/ detects clicks
- def __init__(self, Type):
- self.type = Type
- def image(self, surface, name, x, y):
- surface.blit(name, (x, y))
- def detectclick(self, surface, name, x, y): # homescreen with norma
- global gamestate
- self.mousepos = pygame.mouse.get_pos()
- self.click = pygame.mouse.get_pressed()
- self.b = surface.blit(name, (x, y))
- for i in range(0, 6):
- if self.type == i:
- if self.b.collidepoint(self.mousepos) and self.click[0] == 1:
- gamestate = i + 1
- Turns = False
- class GridScreenNormal: #CREATES GRID
- def __init__(self, x, y):
- self.x = x
- self.y = y
- self.contains = ""
- def draw(self, surface):
- global Turns
- global X
- global O
- pygame.draw.rect(surface, (0, 0, 0), (self.x, self.y, 133, 133), 1)
- if self.contains == "O":
- surface.blit(O, (self.x, self.y))
- elif self.contains == "X":
- surface.blit(X, (self.x, self.y))
- def check_clicked(self, mousepos):
- rect = pygame.Rect(self.x, self.y, 133, 133)
- if rect.collidepoint(mousepos[0], mousepos[1]):
- return True
- return False
- def set_contains(self, new_value):
- self.contains = new_value
- def ai(self):
- global Turns
- grid_array = [] #CREATES EACH GRID SQUARE USING THE CLASS AS A TEMPLATE
- for i in [0, 1, 2]:
- grid_line = []
- for j in [0, 1, 2]:
- x = [50, 183, 316][i]
- y = [166, 299, 432][j]
- grid_square = GridScreenNormal(x, y)
- grid_line.append(grid_square)
- grid_array.append(grid_line)
- tie = [] #TIE LIST
- a = grid_array
- Button1 = Click(1) #BUTTON FOR CHANGING GAMESTATES
- Button2 = Click(2)
- Button3 = Click(3)
- Button4 = Click(4)
- Button5 = Click(5)
- gameend = True #CHECKS IF GAME ENDED
- gamestate = 1
- notie = True #If game was tie
- playerturn = 1
- ready = 0
- ready1 = 0
- running = True
- while running:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- if event.type == pygame.MOUSEBUTTONDOWN:
- if gamestate == 3: #detects clicks
- mousepos = pygame.mouse.get_pos()
- if gameend == True:
- for i in range(0, 3, 1):
- for j in range(0, 3, 1):
- if grid_array[i][j].check_clicked(mousepos) == True:
- if grid_array[i][j].contains == (""):
- grid_array[i][j].set_contains("X")
- tie.append(1)
- Turns = True
- if gamestate == 4 :
- mousepos = pygame.mouse.get_pos()
- if gameend == True :
- for i in range(0, 3, 1) :
- for j in range(0, 3, 1) :
- if grid_array[i][j].check_clicked(mousepos) == True :
- if grid_array[i][j].contains == ("") :
- grid_array[i][j].set_contains("X")
- tie.append(1)
- Turns = True
- if gamestate == 5 :
- mousepos = pygame.mouse.get_pos()
- if gameend == True :
- if playerturn == 1:
- for i in range(0, 3, 1) :
- for j in range(0, 3, 1) :
- if grid_array[i][j].check_clicked(mousepos) == True :
- if grid_array[i][j].contains == ("") :
- grid_array[i][j].set_contains("X")
- playerturn = 2
- tie.append(1)
- if playerturn == 2:
- for i in range(0, 3, 1) :
- for j in range(0, 3, 1) :
- if grid_array[i][j].check_clicked(mousepos) == True :
- if grid_array[i][j].contains == ("") :
- playerturn = 1
- grid_array[i][j].set_contains("O")
- tie.append(1)
- k = pygame.key.get_pressed()
- if gamestate == 1: #Intro Screen/ play
- Button1.image(screen, introimage, 0, 0)
- Button1.image(screen, play, 170, 300)
- Button1.detectclick(screen, play, 170, 300)
- pygame.display.update()
- clock.tick(60)
- if gamestate == 2: #Menu screen with normal hard and friend
- Button1.image(screen, choose, 0, 0)
- Button2.image(screen, normal, 170, 250)
- Button2.detectclick(screen, normal, 170, 250)
- Button3.image(screen, hard, 170, 350)
- Button3.detectclick(screen, hard, 170, 350)
- Button4.image(screen, friend, 170, 450)
- Button4.detectclick(screen, friend, 170, 450)
- pygame.display.update()
- clock.tick(60)
- if gamestate == 3: # NORMAL TIC TAC TOE GAME (NORMAL AI)
- screen.fill((10, 10, 10))
- pygame.draw.rect(screen, (0, 204, 204), (50, 166, 400, 400))
- if Turns == True: # AI plays randomly
- i = random.randint(0, 2)
- j = random.randint(0, 2)
- if grid_array[i][j].contains == (""):
- grid_array[i][j].set_contains("O")
- tie.append(1)
- Turns = False
- x_wins = False
- o_wins = False
- for i in [0, 1, 2]: #CHECK IF O OR X WINS
- row_is_x = True
- row_is_o = True
- for j in [0, 1, 2]:
- if grid_array[i][j].contains != "X":
- row_is_x = False
- if grid_array[i][j].contains != "O":
- row_is_o = False
- if row_is_x:
- x_wins = True
- if row_is_o:
- o_wins = True
- for j in [2, 1, 0]:
- row_is_y = True
- row_is_oy = True
- for i in [0, 1, 2]:
- if grid_array[i][j].contains != "X":
- row_is_y = False
- if grid_array[i][j].contains != "O":
- row_is_oy = False
- if row_is_y:
- x_wins = True
- if row_is_oy:
- o_wins = True
- row_is_d = True
- row_is_do = True
- for i in range(0, 3):
- if grid_array[i][i].contains != "X":
- row_is_d = False
- if grid_array[i][i].contains != "O":
- row_is_do = False
- if row_is_d:
- x_wins = True
- if row_is_do:
- o_wins = True
- row_is_dd = True
- row_is_ddo = True
- for i in range(0, 3):
- if grid_array[i][2 - i].contains != "X":
- row_is_dd = False
- print("bruh")
- if grid_array[i][2 - i].contains != "O":
- row_is_ddo = False
- print("bruh")
- if row_is_dd:
- x_wins = True
- if row_is_ddo:
- o_wins = True
- if x_wins: #IF X WON CHANGE GAMESTATE AND RESET GAME
- o_wins = False
- Turns = False
- gameend = False
- screen.blit(wintext, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE]:
- gamestate = 2
- gameend = True
- notie = True
- for i in range(0, 3):
- for j in range(0, 3):
- grid_array[i][j].contains = ""
- tie = []
- x_wins = False
- if o_wins:#IF O WON CHANGE GAMESTATE AND RESET GAME
- x_wins = False
- Turns = False
- screen.blit(losetext, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE]:
- gamestate = 2
- gameend = True
- tie = []
- notie = True
- o_wins = False
- for i in range(0, 3):
- for j in range(0, 3):
- grid_array[i][j].contains = ""
- if notie == True: #IF GAME TIED THAN RESET GAME AND TELL PLAYERS MESSAGE THAT NO PLAYER WON
- if len(tie) >= 9:
- screen.blit(tietext, (10, 0))
- if k[pygame.K_SPACE]:
- gameend = True
- notie = True
- gamestate = 2
- tie = []
- o_wins = False
- for i in range(0, 3):
- for j in range(0, 3):
- grid_array[i][j].contains = ""
- screen.blit(grid, (50, 166))
- for grid_line in grid_array: #Draws
- for grid_square in grid_line:
- grid_square.draw(screen)
- pygame.display.update()
- clock.tick(60)
- if gamestate == 4: #HARD AI SCREEN
- screen.fill((10, 10, 10))
- pygame.draw.rect(screen, (204, 0, 0), (50, 166, 400, 400))
- '''
- Sadly I couldnt figure it out and had to do something different
- if Turns == True:
- test = random.randint(0, 2)
- play_i = 0
- play_j = 0
- for i in [0, 1, 2]:
- x_count = 0
- for j in [0, 1, 2]:
- if grid_array[i][j].contains != "X":
- x_count = x_count + 1
- if x_count == 2:
- play_i = i
- for j in [0, 1, 2]:
- if grid_array[i][j].contains == "":
- play_j = j
- '''
- if Turns == True : # AI plays randomly twice
- lp = random.randint(0, 2)
- lo = random.randint(0, 2)
- lpo = random.randint(0, 2)
- loo = random.randint(0, 2)
- print(tie)
- if grid_array[i][j].contains == ("") and ready == 0:
- grid_array[lp][lo].set_contains("O")
- ready = ready + 1
- tie.append(1)
- if grid_array[i][j].contains == ("") and ready1 == 0:
- grid_array[lpo][loo].set_contains("O")
- tie.append(1)
- ready1 = ready1 + 1
- if ready + ready1 == 2:
- ready = 0
- ready1 = 0
- Turns = False
- x_wins = False
- o_wins = False
- for i in [0, 1, 2] : #CHECK IF X OR O WON
- row_is_x = True
- row_is_o = True
- for j in [0, 1, 2] :
- if grid_array[i][j].contains != "X" :
- row_is_x = False
- if grid_array[i][j].contains != "O" :
- row_is_o = False
- if row_is_x :
- x_wins = True
- if row_is_o :
- o_wins = True
- for j in [2, 1, 0] :
- row_is_y = True
- row_is_oy = True
- for i in [0, 1, 2] :
- if grid_array[i][j].contains != "X" :
- row_is_y = False
- if grid_array[i][j].contains != "O" :
- row_is_oy = False
- if row_is_y :
- x_wins = True
- if row_is_oy :
- o_wins = True
- row_is_d = True
- row_is_do = True
- for i in range(0, 3) :
- if grid_array[i][i].contains != "X" :
- row_is_d = False
- if grid_array[i][i].contains != "O" :
- row_is_do = False
- if row_is_d :
- x_wins = True
- if row_is_do :
- o_wins = True
- row_is_dd = True
- row_is_ddo = True
- for i in range(0, 3) :
- if grid_array[i][2 - i].contains != "X" :
- row_is_dd = False
- if grid_array[i][2 - i].contains != "O" :
- row_is_ddo = False
- if row_is_dd :
- x_wins = True
- if row_is_ddo :
- o_wins = True
- if x_wins : #If X won then reset game and tell player X won
- o_wins = False
- Turns = False
- gameend = False
- screen.blit(wintext, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE] :
- gamestate = 2
- gameend = True
- notie = True
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- tie = []
- x_wins = False
- if o_wins :#If O won then reset game and tell player X won
- x_wins = False
- Turns = False
- screen.blit(losetext, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE] :
- gamestate = 2
- gameend = True
- tie = []
- notie = True
- o_wins = False
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- if notie == True : #IF GAME TIE THAN RESET GAME AND TELL PLAYERS NO WINNERS
- if len(tie) >= 9 :
- screen.blit(tietext, (10, 0))
- if k[pygame.K_SPACE] :
- gameend = True
- notie = True
- gamestate = 2
- tie = []
- o_wins = False
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- screen.blit(grid, (50, 166))
- for grid_line in grid_array :
- for grid_square in grid_line :
- grid_square.draw(screen)
- pygame.display.update()
- clock.tick(60)
- if gamestate == 5: #NORMAL GRID SCREEN/ PLAYER VS PLAYER
- screen.fill((10, 10, 10))
- pygame.draw.rect(screen, (0, 204, 0), (50, 166, 400, 400))
- x_wins = False
- o_wins = False
- for i in [0, 1, 2] : #CHECKS WHO WON
- row_is_x = True
- row_is_o = True
- for j in [0, 1, 2] :
- if grid_array[i][j].contains != "X" :
- row_is_x = False
- if grid_array[i][j].contains != "O" :
- row_is_o = False
- if row_is_x :
- x_wins = True
- if row_is_o :
- o_wins = True
- for j in [2, 1, 0] :
- row_is_y = True
- row_is_oy = True
- for i in [0, 1, 2] :
- if grid_array[i][j].contains != "X" :
- row_is_y = False
- if grid_array[i][j].contains != "O" :
- row_is_oy = False
- if row_is_y :
- x_wins = True
- if row_is_oy :
- o_wins = True
- row_is_d = True
- row_is_do = True
- for i in range(0, 3) :
- if grid_array[i][i].contains != "X" :
- row_is_d = False
- if grid_array[i][i].contains != "O" :
- row_is_do = False
- if row_is_d :
- x_wins = True
- if row_is_do :
- o_wins = True
- row_is_dd = True
- row_is_ddo = True
- for i in range(0, 3) :
- if grid_array[i][2 - i].contains != "X" :
- row_is_dd = False
- if grid_array[i][2 - i].contains != "O" :
- row_is_ddo = False
- if row_is_dd :
- x_wins = True
- if row_is_ddo :
- o_wins = True
- if x_wins : #IF X WON THEN RESET
- o_wins = False
- Turns = False
- gameend = False
- screen.blit(XWin, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE] :
- gamestate = 2
- gameend = True
- notie = True
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- tie = []
- x_wins = False
- if o_wins : #if O won then reset
- x_wins = False
- Turns = False
- screen.blit(OWin, (10, 0))
- notie = False
- gameend = False
- if k[pygame.K_SPACE] :
- gamestate = 2
- gameend = True
- tie = []
- notie = True
- o_wins = False
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- if notie == True : #if tie than reset
- if len(tie) >= 9 :
- screen.blit(tietext, (10, 0))
- if k[pygame.K_SPACE] :
- gameend = True
- notie = True
- gamestate = 2
- tie = []
- o_wins = False
- for i in range(0, 3) :
- for j in range(0, 3) :
- grid_array[i][j].contains = ""
- screen.blit(grid, (50, 166))
- for grid_line in grid_array :
- for grid_square in grid_line :
- grid_square.draw(screen)
- pygame.display.update()
- clock.tick(60)
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement