I created a simple program that displays 28 numbers on the screen. Currently, nothing happens when you click on the numbers. Eventually, I want the numbers to disappear on click. Also, when you click on a '5', you get 5 points. I would like to make this multiplayer too where the user with the most points after 10 seconds wins. But that is all for later.
from Tkinter import *
import random
class Game(Frame):
def __init__(self, parent):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.parent.title("Centered window")
self.pack(fill=BOTH, expand=1)
self.centerWindow()
def centerWindow(self):
w = 850
h = 530
sw = self.parent.winfo_screenwidth()
sh = self.parent.winfo_screenheight()
x = (sw - w)/2
y = (sh - h)/2
self.parent.geometry('%dx%d+%d+%d' % (w, h, x, y))
def main():
global tile_frame_column # the labels that go on the screen of individual numbers
tile_frame_column = {}
global tiles_make_number # dictionary of all the individual numbers
tiles_make_number = {}
global tile_frame_column_counter
tile_frame_column_counter=0
global tiles_make_number_counter
tiles_make_number_counter=0
global xpos
xpos=35
global ypos
ypos=50
tile_numbers = ['1', '2', '3', '4', '5', '6', '1', '2', '1', '2', '1', 'l', '1', '3', '4', '2', '1', '2', '1', '1', '2', '1', '1', '6', '4', '3', '2', '2']
root = Tk()
ex = Game(root)
def add_number_to_screen():
global tile_frame_column_counter
global tiles_make_number_counter
global tile_frame_column
global xpos
global ypos
if not tile_numbers:
return
rand = random.choice(tile_numbers)
tile_frame_column[tile_frame_column_counter] = Button(root, text=rand, font="Helvetica 16 bold")
tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
tile_numbers.remove(rand) # remove that tile from list of tiles
xpos += 80
if (len(tiles_make_number) % 7 == 0) & (len(tiles_make_number) > 0):
xpos = 35
ypos += 80
tile_frame_column[tile_frame_column_counter].place(x=xpos, y=ypos)
xpos += 80
tiles_make_number[tiles_make_number_counter] = rand
tile_frame_column_counter += 1
tiles_make_number_counter += 1
root.after(10, add_number_to_screen)
root.after(10, add_number_to_screen)
root.mainloop()
if __name__ == '__main__':
main()
I want the main()
function to be initialized, though. Whenever a game object is created I want it to automatically add the main function -- meaning add numbers to the screen. I think the way my code is now, when a game object is created it only creates the screen.
There must be a better way to maintain this code, though.
main()
function to be initialized"? – skrrgwasme Oct 15 '14 at 19:05