Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import sys
- import time
- WIDTH, HEIGHT = 600, 700
- WHITE = (255, 255, 255)
- PINK = (255,0,120)
- board = []
- END = False
- openWindow = True
- click = (0,0,0)
- clickPos = [0,0]
- player = 'X'
- win_commbinations = ((0, 1, 2), (3, 4, 5), (6, 7, 8), (0, 3, 6), (1, 4, 7), (2, 5, 8), (0, 4, 8), (2, 4, 6))
- canvas = pygame.display.set_mode((WIDTH,HEIGHT))
- pygame.display.set_caption('TicTacToe')
- pygame.init()
- font = pygame.font.SysFont('arial', 50)
- font2 = pygame.font.SysFont('arial', 100)
- def printBoard():
- pygame.display.update()
- pygame.draw.line(canvas, WHITE, [200, 100], [200,700], 2)
- pygame.draw.line(canvas, WHITE, [400, 100], [400,700], 2)
- pygame.draw.line(canvas, WHITE, [0, 100], [600,100], 2)
- pygame.draw.line(canvas, WHITE, [0, 300], [600,300], 2)
- pygame.draw.line(canvas, WHITE, [0, 500], [600,500], 2)
- def printHeader(winner):
- if winner == 'X' or winner == 'O':
- text = 'WINNER: ' + str(winner)
- elif winner == 'TIE':
- text = 'TIE: NOBODY WINS'
- else:
- text = 'Player:' + str(player)
- if takenPos == True:
- text = str(player) + ': choose again'
- canvas.fill((0,0,0), (0,0,600,100))
- textSet = font.render(text, True, (PINK))
- canvas.blit(textSet,(20,20))
- def getClickPos():
- global clickPos
- global board
- takenPos = False
- x, y = pygame.mouse.get_pos()
- x = int(x / 200)
- y = int((y-100) / 200)
- if board[x][y]==0:
- board[x][y] = player
- else:
- takenPos = True
- board[x][y]==0
- if x == 0:
- if y == 0:
- clickPos = [100, 200]
- if y == 1:
- clickPos = [100, 400]
- if y == 2:
- clickPos = [100, 600]
- if x == 1:
- if y == 0:
- clickPos = [300, 200]
- if y == 1:
- clickPos = [300, 400]
- if y == 2:
- clickPos = [300, 600]
- if x == 2:
- if y == 0:
- clickPos = [500, 200]
- if y == 1:
- clickPos = [500, 400]
- if y == 2:
- clickPos = [500, 600]
- return x, y, takenPos
- def printPlayer(player, clickPos, x, y):
- if board[x][y] == player:
- if player == 'O':
- pygame.draw.circle(canvas, WHITE, clickPos, 50, 5)
- player = 'X'
- elif player == 'X':
- pygame.draw.line(canvas, WHITE, [clickPos[0] - 50,clickPos[1] + 50], [clickPos[0] + 50,clickPos[1] - 50], 5)
- pygame.draw.line(canvas, WHITE, [clickPos[0] + 50,clickPos[1] + 50], [clickPos[0] - 50,clickPos[1] - 50], 5)
- player = 'O'
- return player
- def getWinner():
- winComb = []
- turnCounter = 0
- winner = ''
- END = False
- for i in range(len(board)):
- for j in range(len(board[i])):
- winComb.append(board[i][j])
- for i in range(len(board)):
- for j in range(len(board[i])):
- if board[i][j] == 'X' or board[i][j] == 'O':
- turnCounter += 1
- if turnCounter > 8:
- winner = 'TIE'
- END = True
- for i in win_commbinations:
- if winComb[i[0]] == winComb[i[1]] == winComb[i[2]] == player:
- winner = player
- END = True
- return winner, END
- text = 'Player: ' + str(player)
- textSet = font.render(text, True, (PINK))
- canvas.blit(textSet,(20,20))
- def theGame():
- openWindow = True
- global board
- board = [[0,0,0],[0,0,0],[0,0,0]]
- global player
- global takenPos
- global END
- while openWindow == True:
- printBoard()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- openWindow = False
- elif event.type == pygame.MOUSEBUTTONUP:
- x, y, takenPos = getClickPos()
- winner, END = getWinner()
- player = printPlayer(player, clickPos, x, y)
- printHeader(winner)
- pygame.display.flip()
- while END == True:
- canvas.fill((0,0,0,))
- text = 'The End press P to play again press Q to quit'
- textSet = font.render(text, True, (PINK))
- canvas.blit(textSet,(20,20))
- pygame.display.update()
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- END = False
- openWindow = False
- if event.type == pygame.KEYDOWN:
- if event.key == pygame.K_p:
- END = False
- theGame()
- elif event.key == pygame.K_q:
- openWindow = False
- END = False
- theGame()
- ####################################################
- """
- - bug #2 - po wygranej grze dalej można uzupełniać planszę
- - po zakonczonej grze brak możliwości ponownej gry
- """
- ####################################################
Advertisement
Add Comment
Please, Sign In to add comment