Created
June 4, 2017 02:55
-
-
Save s4kr4/a1606cd467230512666efae0ad57a43f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#LIFEGAME | |
from random import * | |
from Tkinter import * | |
cell = 10 | |
height = 60 + 2 | |
width = 60 + 2 | |
frame = 5 | |
field = [] | |
dead = 0 | |
alive = 1 | |
is_run = False | |
def set_field(field): | |
for x in range(height): | |
row = [] | |
for y in range(width): | |
row.append(dead) | |
field.append(row) | |
draw() | |
def reset(): | |
for x in range(height): | |
for y in range(width): | |
field[x][y] = dead | |
age = 0 | |
draw() | |
def set_random(): | |
for x in range(height): | |
for y in range(width): | |
field[x][y] = choice((dead, alive)) | |
draw() | |
def dead_alive(event): | |
if event.x < (frame + cell) or event.x > (frame + (width-1)*cell): | |
return | |
if event.y < (frame + cell) or event.y > (frame + (height-1)*cell): | |
return | |
x = int((event.x - (frame + cell)) / cell) | |
y = int((event.y - (frame + cell)) / cell) | |
if field[x][y] == dead: | |
field[x][y] = alive | |
else: | |
field[x][y] = dead | |
x0 = frame + cell*(x+1) | |
y0 = frame + cell*(y+1) | |
x1 = x0 + cell | |
y1 = y0 + cell | |
if field[x][y] == alive: | |
color = '#00FF00' | |
else: | |
color = 'black' | |
canvas.create_rectangle(x0, y0, x1, y1, fill = color, outline = 'black', | |
tags = 'cell') | |
def next_age(): | |
calc_field = [] | |
set_field(calc_field) | |
for x in range(1, height-1): | |
for y in range(1, width-1): | |
if field[x][y] == alive: | |
calc_field[x-1][y-1] += 1 | |
calc_field[x-1][y] += 1 | |
calc_field[x-1][y+1] += 1 | |
calc_field[x][y-1] += 1 | |
calc_field[x][y+1] += 1 | |
calc_field[x+1][y-1] += 1 | |
calc_field[x+1][y] += 1 | |
calc_field[x+1][y+1] += 1 | |
for x in range(width): | |
calc_field[0][x] = dead | |
calc_field[height-1][x] = dead | |
for y in range(height): | |
calc_field[y][0]= dead | |
calc_field[y][width-1] = dead | |
for x in range(1, height-1): | |
for y in range(1, width-1): | |
if calc_field[x][y] <= 1 or calc_field[x][y] >= 4: | |
field[x][y] = dead | |
elif calc_field[x][y] == 2: | |
continue | |
elif calc_field[x][y] == 3: | |
field[x][y] = alive | |
draw() | |
def draw(): | |
canvas.delete('cell') | |
for x in range(height-2): | |
for y in range(width-2): | |
x0 = frame + cell*(x+1) | |
y0 = frame + cell*(y+1) | |
x1 = x0 + cell | |
y1 = y0 + cell | |
if field[x][y] == alive: | |
color = '#00FF00' | |
else: | |
color = 'black' | |
canvas.create_rectangle(x0, y0, x1, y1, fill = color, | |
outline = 'black', tags = 'cell') | |
def run(): | |
if is_run: | |
next_age() | |
root.after(200, run) | |
def switch(): | |
global is_run | |
is_run = not(is_run) | |
run() | |
############ display part ############# | |
root = Tk() | |
root.title('LIFE GAME') | |
canvas_h = frame*2 + cell*height | |
canvas_w = frame*2 + cell*width | |
canvas = Canvas(root, height = canvas_h, width = canvas_w) | |
canvas.bind('<Button-1>', dead_alive) | |
canvas.pack() | |
reset_button = Button(root, text = 'reset', command = reset) | |
reset_button.pack(side = LEFT) | |
random_button = Button(root, text = 'random', command = set_random) | |
random_button.pack(side = LEFT) | |
next_button = Button(root, text = 'next', command = next_age) | |
next_button.pack(side = LEFT) | |
switch_button = Button(root, text = 'start/stop', command = switch) | |
switch_button.pack(side = LEFT) | |
exit_button = Button(root, text = 'exit', command = root.destroy) | |
exit_button.pack(side = RIGHT) | |
set_field(field) | |
for x in range(width): | |
x0 = frame + cell*x | |
y0 = frame | |
x1 = x0 + cell | |
y1 = y0 + cell | |
canvas.create_rectangle(x0, y0, x1, y1, fill = 'gray', outline = 'gray') | |
for y in range(height): | |
x0 = frame | |
y0 = frame + cell*y | |
x1 = x0 + cell | |
y1 = y0 + cell | |
canvas.create_rectangle(x0, y0, x1, y1, fill = 'gray', outline = 'gray') | |
for x in range(width): | |
x0 = frame + cell*x | |
y0 = frame + cell*(height-1) | |
x1 = x0 + cell | |
y1 = y0 + cell | |
canvas.create_rectangle(x0, y0, x1, y1, fill = 'gray', outline = 'gray') | |
for y in range(height): | |
x0 = frame + cell*(width-1) | |
y0 = frame + cell*y | |
x1 = x0 + cell | |
y1 = y0 + cell | |
canvas.create_rectangle(x0, y0, x1, y1, fill = 'gray', outline = 'gray') | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment