patternpythonMinor
Simple Tic-Tac-Toe using tkinter
Viewed 0 times
simpletoetictkintertacusing
Problem
I've been learning programming using a book about Python for a few months. To explore object-oriented programming, I built a few things in tkinter.
When I started this code, I wanted to be able to play on rectangular boards of any sizes, but it made checking for a winner more difficult, so it's just the regular 3x3 game at the moment.
How can I improve it?
```
from tkinter import *
# PARAMETERS
# graphics
WINDOW_SIZE = 600 # pixels
GRID_LINE_WIDTH = 2 # pixels
SYMBOL_WIDTH = WINDOW_SIZE/12 # pixels - adjust ratio
# 0-1 : size of a symbol relative to it's cell
# bigger than 1 is too much
SYMBOL_SIZE = 0.5
X_COLOR = 'dodger blue'
O_COLOR = 'tomato'
# background color of the 'draw' gameover screen
DRAW_SCREEN_COLOR = 'light sea green'
GRID_COLOR = 'light grey'
BG_COLOR = 'white'
# gameplay
FIRST_PLAYER = 2 # 1 - X, 2 = O
# OTHER
CELL_SIZE = WINDOW_SIZE / 3
# game states
STATE_TITLE_SCREEN = 0
STATE_X_TURN = 1
STATE_O_TURN = 2
STATE_GAME_OVER = 3
# symbol notation in the board memory
EMPTY = 0
X = 1
O = 2
class Game(Tk):
"""
Main class
"""
def __init__(self):
Tk.__init__(self)
self.canvas = Canvas(
height=WINDOW_SIZE, width=WINDOW_SIZE,
bg=BG_COLOR)
self.canvas.pack()
self.bind('', self.exit)
self.canvas.bind('', self.click)
self.gamestate = STATE_TITLE_SCREEN
self.title_screen()
self.board = [
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def title_screen(self):
# placeholder title screen
self.canvas.delete('all') #just in case
self.canvas.create_rectangle(
0, 0,
WINDOW_SIZE, WINDOW_SIZE,
fill=O_COLOR,
outline='')
self.canvas.create_rectangle(
int(WINDOW_SIZE/15), int(WINDOW_SIZE/15),
int(WINDOW_SIZE14/15), int(WINDOW_SIZE14/15),
width=int(WINDOW_SIZE/20),
outlin
When I started this code, I wanted to be able to play on rectangular boards of any sizes, but it made checking for a winner more difficult, so it's just the regular 3x3 game at the moment.
How can I improve it?
```
from tkinter import *
# PARAMETERS
# graphics
WINDOW_SIZE = 600 # pixels
GRID_LINE_WIDTH = 2 # pixels
SYMBOL_WIDTH = WINDOW_SIZE/12 # pixels - adjust ratio
# 0-1 : size of a symbol relative to it's cell
# bigger than 1 is too much
SYMBOL_SIZE = 0.5
X_COLOR = 'dodger blue'
O_COLOR = 'tomato'
# background color of the 'draw' gameover screen
DRAW_SCREEN_COLOR = 'light sea green'
GRID_COLOR = 'light grey'
BG_COLOR = 'white'
# gameplay
FIRST_PLAYER = 2 # 1 - X, 2 = O
# OTHER
CELL_SIZE = WINDOW_SIZE / 3
# game states
STATE_TITLE_SCREEN = 0
STATE_X_TURN = 1
STATE_O_TURN = 2
STATE_GAME_OVER = 3
# symbol notation in the board memory
EMPTY = 0
X = 1
O = 2
class Game(Tk):
"""
Main class
"""
def __init__(self):
Tk.__init__(self)
self.canvas = Canvas(
height=WINDOW_SIZE, width=WINDOW_SIZE,
bg=BG_COLOR)
self.canvas.pack()
self.bind('', self.exit)
self.canvas.bind('', self.click)
self.gamestate = STATE_TITLE_SCREEN
self.title_screen()
self.board = [
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY],
[EMPTY, EMPTY, EMPTY]]
def title_screen(self):
# placeholder title screen
self.canvas.delete('all') #just in case
self.canvas.create_rectangle(
0, 0,
WINDOW_SIZE, WINDOW_SIZE,
fill=O_COLOR,
outline='')
self.canvas.create_rectangle(
int(WINDOW_SIZE/15), int(WINDOW_SIZE/15),
int(WINDOW_SIZE14/15), int(WINDOW_SIZE14/15),
width=int(WINDOW_SIZE/20),
outlin
Solution
As a small suggestion, I noticed that your
Note the now missing artifact:
Os have an odd line at the right hand side due to drawing them with the outline of an oval (as you noticed and commented in the draw_O function). You can fix that by instead using two filled ovals, a big one which is filled using O_COLOR and a smaller one filled using BG_COLOR (Although this does require adjusting the delta to compensate for the missing width):def draw_O(self, grid_x, grid_y):
"""
draw an O symbol at x, y in the grid
note : a big outline value appears to cause a visual glitch in tkinter
"""
x = self.gtpix(grid_x)
y = self.gtpix(grid_y)
delta = 1.5*CELL_SIZE/2*SYMBOL_SIZE
self.canvas.create_oval(
x-delta, y-delta,
x+delta, y+delta,
fill = O_COLOR, outline="")
self.canvas.create_oval(
x-delta/3, y-delta/3,
x+delta/3, y+delta/3,
fill = BG_COLOR, outline="")Note the now missing artifact:
Code Snippets
def draw_O(self, grid_x, grid_y):
"""
draw an O symbol at x, y in the grid
note : a big outline value appears to cause a visual glitch in tkinter
"""
x = self.gtpix(grid_x)
y = self.gtpix(grid_y)
delta = 1.5*CELL_SIZE/2*SYMBOL_SIZE
self.canvas.create_oval(
x-delta, y-delta,
x+delta, y+delta,
fill = O_COLOR, outline="")
self.canvas.create_oval(
x-delta/3, y-delta/3,
x+delta/3, y+delta/3,
fill = BG_COLOR, outline="")Context
StackExchange Code Review Q#155692, answer score: 4
Revisions (0)
No revisions yet.