Skip to content

Instantly share code, notes, and snippets.

@azzgo
Created September 10, 2016 15:36
Show Gist options
  • Save azzgo/ee646dccf2f4540b83e61d1159d10c73 to your computer and use it in GitHub Desktop.
Save azzgo/ee646dccf2f4540b83e61d1159d10c73 to your computer and use it in GitHub Desktop.
#! --coding: utf-8--
from Tkinter import *
import random
master = Tk()
master.title("康威生命游戏")
WIDTH=700
HEIGHT=700
UNIT=10
GRID_X = WIDTH / UNIT
GRID_Y = HEIGHT / UNIT
LIVE = True
DIE = False
class Ground(object):
def __init__(self):
startButton = Button(text="start")
startButton.grid(row=0, column=0, sticky=W)
stopButton = Button(text="stop")
stopButton.grid(row=0, column=1, sticky=W)
randomButton = Button(text="random")
randomButton.grid(row=0, column=2, sticky=W)
clearButton = Button(text="clear")
clearButton.grid(row=0, column=3, sticky=W)
gunButton = Button(text="gun")
gunButton.grid(row=0, column=4, sticky=W)
game = Game(height=HEIGHT, width=WIDTH)
game.grid(row=1, columnspan=20)
def click(event):
x, y = event.x / UNIT, event.y/ UNIT
print x,", ",y
game.toggle(x, y)
game.bind("<Button-1>", click)
startButton.bind("<Button-1>", game.start)
stopButton.bind("<Button-1>", game.stop)
randomButton.bind("<Button-1>", game.random)
clearButton.bind("<Button-1>", game.clear)
def gun(event):
points = [(1, 5), (2, 5), (1, 6), (2, 6), (11, 5), (11, 6), (11, 7), (12, 4), (12, 8), (13, 3), (13, 9), (14, 3), (14, 9), (15, 6), (16, 4), (16, 8), (17, 5), (17, 6), (17, 7), (18, 6), (21, 3), (21, 4), (21, 5), (22, 3), (22, 5), (22, 4), (23, 2), (23, 6), (25, 1), (25, 2), (25, 6), (25, 7), (35, 3), (35, 4), (36, 3), (36, 4)]
for point in points:
game.toggle(*point)
gunButton.bind("<Button-1>", gun)
class Game(Canvas):
cells = []
def __init__(self, master=None, **options):
Canvas.__init__(self, master, **options)
for y in range(GRID_Y):
for x in range(GRID_X):
self.cells.append(Cell(x, y))
for cell in self.cells:
cell.getArround(self.cells)
cell.draw(self)
def random(self, *args):
for cell in self.cells:
cell.update(random.choice([ LIVE, DIE ]))
self.frameUpdate()
def clear(self, *args):
for cell in self.cells:
cell.update(DIE)
self.frameUpdate()
def frameUpdate(self):
for cell in self.cells:
cell.refresh(self)
def toggle(self, x, y):
cell = self.cells[ GRID_X * y + x ]
cell.toggle()
cell.refresh(self)
def iteration(self):
for cell in self.cells:
cell.kanvy()
self.frameUpdate()
self.loopId = Canvas.after(self, 300, self.iteration)
def start(self, *args):
self.loopId = Canvas.after(self, 300, self.iteration)
def stop(self, *args):
if hasattr(self, "loopId"):
Canvas.after_cancel(self, self.loopId)
class Cell(object):
def __init__(self, x, y):
self.x = x
self.y = y
self._isLive = self.isLive = DIE
self.arround = []
def draw(self, ctx):
x, y = self.x, self.y
color = 'black' if self.isLive else 'white'
ctx.create_rectangle(x * UNIT, y * UNIT, (x+1) * UNIT, (y+1) * UNIT, fill = color)
def update(self, isLive):
self._isLive = isLive
def refresh(self, ctx):
if (self.isLive != self._isLive):
self.isLive = self._isLive
self.draw(ctx)
def toggle(self):
self._isLive = not self.isLive
def judge(self, count):
if self.isLive:
if count < 2:
self.update(DIE)
if count == 2 or count == 3:
self.update(LIVE)
if count > 3:
self.update(DIE)
else:
if count == 3:
self.update(LIVE)
else:
self.update(DIE)
def getArround(self, cells):
for x in range(self.x - 1, self.x + 1 + 1):
for y in range(self.y - 1, self.y + 1 + 1):
if x == self.x and y == self.y:
continue
if x < 0 or x >= GRID_X:
continue
if y < 0 or y >= GRID_Y:
continue
self.arround.append(cells[y * GRID_X + x])
def kanvy(self):
x, y = self.x, self.y
count = 0
for nebor in self.arround:
if nebor.isLive:
count += 1
self.judge(count)
if __name__ == "__main__":
ground = Ground()
mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment