Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

tic tac toe pygame

#you need three pictures: 
#line.png (600 X 10): line (with rounded corners)
#x.png (200 X 200): an X
#o.png (200 X 200): an O

import pygame as pg
import numpy as np
import time as t
import random as r
import pdb

pg.init()
pg.font.init()

screen = pg.display.set_mode((600,600),pg.DOUBLEBUF)
pg.display.set_caption("...?")
playing = True

line_picture_vert = pg.image.load("line.png").convert_alpha()
line_picture_horiz = pg.transform.rotate(line_picture_vert, 90)

x_picture = pg.image.load("x.png").convert_alpha()
o_picture = pg.image.load("o.png").convert_alpha()

grid = np.zeros((3,3), dtype="int")

#0 = nothing
#1 = x
#2 = o

#functions and classes
def draw_grid(screen):
    screen.blit(line_picture_vert,(195, 0))
    screen.blit(line_picture_horiz,(0, 195))
    screen.blit(line_picture_vert,(395, 0))
    screen.blit(line_picture_horiz,(0, 395))
    for x in range(3):
        for y in range(3):
            if grid[y, x] == 1: #x
                screen.blit(x_picture,(x*200, y*200))
            elif grid[y, x] == 2: #o
                screen.blit(o_picture,(x*200, y*200))

def draw_win_lines(screen, grid_win_line, line_animation):
    for line in grid_win_line:
        posx = line[0][0] + (line[1][0] - line[0][0]) * line_animation
        posy = line[0][1] + (line[1][1] - line[0][1]) * line_animation
        pg.draw.line(screen, (200,200,200), line[0], (posx,posy), width=10)



def calculate_mouse_pos(mx, my):
    mrx = int(mx/200)
    mry = int(my/200)
    return mrx, mry

def calculate_if_row(grid):
    win = 0 #if win 1: x has won, if win 2: o has won
    grid_win_line = []
    line_width = 10
    half_l_w = int(line_width/2)
    
    #check horizontal
    for y in range(3): #x = 0, 1, 2
        if grid[y, 0] == grid[y, 1] and grid[y, 1] == grid[y, 2] and grid[y, 0] != 0:
            win = grid[y, 0]
            grid_win_line.append([[100-half_l_w,y*200+100-half_l_w],[500-half_l_w,y*200+100-half_l_w]])

    #check vertical
    for x in range(3): #x = 0, 1, 2
        if grid[0, x] == grid[1, x] and grid[1, x] == grid[2, x] and grid[2, x] != 0:
            win = grid[0, x]
            grid_win_line.append([[x*200+100-half_l_w, 100-half_l_w],[x*200+100-half_l_w, 500-half_l_w]])

    #check diagonals
    if grid[0, 0] == grid[1, 1] and grid[1, 1] == grid[2, 2] and grid[1, 1] != 0:
        win = grid[1, 1]
        grid_win_line.append([[100-half_l_w,100-half_l_w],[500-half_l_w,500-half_l_w]])

    if grid[2, 0] == grid[1, 1] and grid[1, 1] == grid[0, 2] and grid[1, 1] != 0:
        win = grid[1, 1]
        grid_win_line.append([[100-half_l_w,500-half_l_w],[500-half_l_w,100-half_l_w]])

    return win, grid_win_line
        


#game variables
turn = 1 #turn 1 = x, turn 2 = o
win = 0 #when one of player wins
grid_win_line = [] #line to be drawn when someone wins
grid_change = True #when grid changes (for 1 frame)
line_animation = 0 #from 0.0 to 1.0 animation of line
deltaTime = 0 #deltaTime

#menu variables

clock = pg.time.Clock()

while playing:

    #events

    for e in pg.event.get():
        if e.type == pg.QUIT:
            playing = False
            
        if e.type == pg.KEYDOWN:
            if e.key == pg.K_ESCAPE:
                playing = False

        if e.type == pg.MOUSEBUTTONDOWN:
            mousebtn = pg.mouse.get_pressed()
            mx, my = pg.mouse.get_pos()

            if mousebtn[0]: #left mouse btn
                mrx, mry = calculate_mouse_pos(mx, my)
                if grid[mry, mrx] == 0 and win == 0:
                    grid[mry,mrx] = turn
                    turn = turn % 2 + 1
                    grid_change = True

    #calculate stuff
    if grid_change:
        win, grid_win_line = calculate_if_row(grid)

    #update screen

    screen.fill((40,40,40))


    draw_grid(screen)
    draw_win_lines(screen, grid_win_line, line_animation)
    if win != 0 and line_animation < 1:
        line_animation += 0.01
    pg.event.pump()
    pg.display.flip()
    grid_change = False

    deltaTime = clock.tick(60) #max of 60 fps

pg.font.quit()
pg.quit()
Source by pythonguides.com #
 
PREVIOUS NEXT
Tagged: #tic #tac #toe #pygame
ADD COMMENT
Topic
Name
8+8 =