Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import random
- import time
- #pygame.mixer.Sound(file).play
- #pygame.mixer.Sound.stop
- #pygame.mixer.Sound.fadeout
- pygame.init()
- HEIGHT = 600
- WIDTH = 600 # 800 # 600 -> square
- visual = pygame.display.set_mode((HEIGHT, WIDTH))
- #title screen animation start
- dt=pygame.time.Clock()
- enterPressed=False
- Current_fps = None
- which_TitleScreen=1
- time_since_TitleScreen_changed=0
- TitleScreen=pygame.image.load("TitleScreen.png")
- TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH, HEIGHT))
- visual.blit(TitleScreen,(0,1))
- #pygame.display.flip()
- while enterPressed==False:
- time_since_TitleScreen_changed+=dt
- dt = clock.tick()
- if time_since_TitleScreen_changed>=350:
- if which_TitleScreen==1:
- which_TitleScreen=2
- TitleScreen= pygame.image.load("TitleScreen2.png")
- TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH,HEIGHT))
- visual.blit(TitleScreen,(0,0))
- pygame.display.flip()
- time_since_TitleScreen_changed=0
- else:
- which_TitleScreen=1
- TitleScreen=pygame.image.load("TitleScreen.png")
- TitleScreen = pygame.transform.scale(TitleScreen, (WIDTH, HEIGHT))
- pygame.display.flip()
- time_since_TitleScreen_changed=0
- for event in pygame.event.get():
- if event.type==pygame.KEYDOWN:
- if event.key==pygame.K_SPACE:
- enterPressed=True
- #title screen animation end
- xImg = pygame.transform.scale(pygame.image.load('X.png'), (WIDTH // 3, HEIGHT // 3))
- oImg = pygame.transform.scale(pygame.image.load('o.png'), (WIDTH // 3, HEIGHT // 3))
- image=pygame.image.load("choice.png")
- image = pygame.transform.scale(image, (WIDTH, HEIGHT))
- visual.blit(image,(0,0))
- pygame.display.flip()
- #Kadables event mode choosing code
- mode=""
- pygame.event.clear()
- option_chosen=False
- while option_chosen==False:
- for event in pygame.event.get():
- if event.type==pygame.KEYDOWN:
- if event.key==pygame.K_0:
- mode=0
- option_chosen=True
- elif event.key==pygame.K_1:
- mode=1
- option_chosen=True
- board = [
- 0,0,0,
- 0,0,0,
- 0,0,0
- ]
- locations = [
- (0,0),(200,0),(400,0),
- (0,205),(200,205),(400,205),
- (0,410),(200,410),(400,410)
- ]
- # Is it the player's player_peice
- player_move = True
- running = True
- draw = False
- player_peice = 'x'
- ai_peice = 'o'
- active_peice = 'x'
- position = -1
- while running:
- # Draw grid
- pygame.draw.line(visual, (255,255,255), (200,0), (200,600), 5)
- pygame.draw.line(visual, (255,255,255), (400,0), (400,600), 5)
- pygame.draw.line(visual, (255,255,255), (0,200), (600,200), 5)
- pygame.draw.line(visual, (255,255,255), (0,400), (600,400), 5)
- if player_move:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- running = False
- # Where to place the X or O value
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_1:
- position = event.unicode
- if event.key == pygame.K_2:
- position = event.unicode
- if event.key == pygame.K_3:
- position = event.unicode
- if event.key == pygame.K_4:
- position = event.unicode
- if event.key == pygame.K_5:
- position = event.unicode
- if event.key == pygame.K_6:
- position = event.unicode
- if event.key == pygame.K_7:
- position = event.unicode
- if event.key == pygame.K_8:
- position = event.unicode
- if event.key == pygame.K_9:
- position = event.unicode
- position = int(position) - 1
- if mode == 0 and active_peice == ai_peice:
- check = 0
- while check == 0: # for check if positionition is free
- position = random.randint(0,8) # 9 is important
- if board[position] == 0:
- check = 1
- board[position] = 'o'
- #if position >= 0 and board[position] == 0: # only when button has been pressed
- if position > -1: # and mode == 1:
- if board[position] == 0:
- board[position] = active_peice
- position = -1
- if active_peice == 'x': # change player after edit board
- active_peice = 'o'
- else:
- active_peice = 'x'
- for spot in range(len(board)): # 0 to 8
- if not board[spot] == 0:
- col = spot
- row = 0
- while col >= 3:
- col = col - 3
- row += 1
- if board[spot] == 'x':
- visual.blit(xImg, (col * 200, row * 200))
- if board[spot] == 'o':
- visual.blit(oImg, (col * 200, row * 200))
- #print(board[spot], col, row)
- # check if one player wins
- for i in range(3):
- for p in ["x", "o"]:
- if board[i*3] == p and board[i*3+1] == p and board[i*3+2] == p: # checking rows
- print("horizontal")
- win = p
- running = False
- if board[i] == p and board[i+3] == p and board[i+6] == p:
- print("vertical")
- win = p
- running = False
- if board[0] == p and board[4] == p and board[8] == p:
- print("diagonal right")
- win = p
- running = False
- if board[2] == p and board[4] == p and board[6] == p:
- print("diagonal left")
- win = p
- running = False
- # Checks the board from 0-9
- used = 0
- for spot in board:
- if not spot == 0:
- used += 1
- if used == 9:
- running = False
- draw = True
- pygame.display.update()
- visual.fill((0,0,0))
- if draw == False:
- print("\nPlayer " + win + " wins")
- if draw: # == True:
- print("No winner")
- # playing draw sound
- pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement