Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a game board with Tkinter

I am trying to build a simple game of Connect Four with Python(2.7)

I have created a board, that consists of a simple multidimensional Python list.
My Board list looks like this:

board = [
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,_,_,_,_,_,_],
    [_,_,_,_,O,_,_,_,_,_],
    [_,_,_,_,X,_,_,_,_,_],
    [_,_,_,_,X,O,_,_,_,_],
    [_,_,_,_,X,O,_,_,_,_],
]

Were X is Player1 and O is Player2 (or Computer).

Now, I have created some basic code for the GUI, like this:

# Connect 4 Game
import Tkinter

screen = Tkinter.Tk()
screen.title("My First Game")

#Create a board
board = Tkinter.Canvas(screen,width=500,height=500)
board.pack()

screen.mainloop()

Question: How can i create a visual representation of the board, so that for every list, there is a rectangle? Also, is there a way to detect, when a rectangle is clicked and replace the corresponding list value?

like image 342
intelis Avatar asked Jan 16 '13 00:01

intelis


People also ask

Can I use tkinter to make a game?

TKinter is widely used for developing GUI applications. Along with applications, we can also use Tkinter GUI to develop games.

Is tkinter better than Pygame?

First, tkinter is definitely not the best GUI toolkit for python. It's one of the simplest and usually comes with the python interpreter. But it's not as powerful as Qt, wx or Gtk. pygame is - as it's name states - a package designed to allow to create games in python very easily, not to create GUIs.

Can I use tkinter commercially?

Tkinter and Kivy are free libraries, so you can use them for any of your Python projects. PyQT and PySide, however, require that you get a license to make your project commercial. Also, Tkinter is a built-in Python library while the other three require that you install them to be able to make use of them.

Is learning tkinter worth it?

If your goal is to learn how to create GUIs, tkinter is arguably one of the best toolkits there is to reach that goal. It's simple and easy to learn, and can provide a fantastic introduction to concepts you must master in order to create graphical desktop applications.


1 Answers

I created a board of labels and color them according to which is clicked:

import Tkinter as tk

board = [ [None]*10 for _ in range(10) ]

counter = 0

root = tk.Tk()

def on_click(i,j,event):
    global counter
    color = "red" if counter%2 else "black"
    event.widget.config(bg=color)
    board[i][j] = color
    counter += 1


for i,row in enumerate(board):
    for j,column in enumerate(row):
        L = tk.Label(root,text='    ',bg='grey')
        L.grid(row=i,column=j)
        L.bind('<Button-1>',lambda e,i=i,j=j: on_click(i,j,e))

root.mainloop()

This doesn't do any validation (to make sure that the element clicked is at the bottom for example). It would also be much better with classes instead of global data, but that's an exercise for the interested coder :).

like image 143
mgilson Avatar answered Sep 21 '22 13:09

mgilson