-
Notifications
You must be signed in to change notification settings - Fork 0
/
life.py
executable file
·144 lines (124 loc) · 4.62 KB
/
life.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#!/usr/bin/env python
import pygame, sys
pygame.init()
SIZE = (50,50)
class Game:
def __init__(self, size, squaresize = 10, delay = 500):
self.squaresize = squaresize
self.size = size
self.screen = pygame.display.set_mode((size[0]*squaresize,
size[1]*squaresize))
self.cells = []
for i in xrange(size[0]):
self.cells.append([])
for j in xrange(size[1]):
self.cells[i].append(False)
self.running = False
self.alive = pygame.Surface((squaresize,squaresize))
self.alive.fill((255,255,255))
self.dead = pygame.Surface((squaresize,squaresize))
self.dead.fill((0,0,0))
self.delay = delay
self.nextstep = 0
self.clock = pygame.time.Clock()
self.paused = True
def SetCell(self, posx, posy, value):
self.cells[posx][posy] = value
def GetAt(self, posx, posy):
while posx < 0:
posx += self.size[0]
while posx >= self.size[0]:
posx -= self.size[0]
while posy < 0:
posy += self.size[1]
while posy >= self.size[1]:
posy -= self.size[1]
if self.cells[posx][posy]:
return 1
else:
return 0
def Pause(self):
self.paused = not self.paused
def Run(self):
self.running = True
self.MainLoop()
def UpdateCells(self):
# create new list
cells2 = []
for i in xrange(self.size[0]):
cells2.append([])
for j in xrange(self.size[1]):
cells2[i].append(False)
for x in xrange(self.size[0]):
for y in xrange(self.size[1]):
num = 0
num += self.GetAt(x-1, y-1)
num += self.GetAt(x, y-1)
num += self.GetAt(x+1, y-1)
num += self.GetAt(x-1, y)
num += self.GetAt(x+1, y)
num += self.GetAt(x-1, y+1)
num += self.GetAt(x, y+1)
num += self.GetAt(x+1, y+1)
if (num > 3 or num <2):
cells2[x][y] = False
elif num == 3:
cells2[x][y] = True
else:
cells2[x][y] = self.cells[x][y]
self.cells = cells2
def MainLoop(self):
self.running = True
while (self.running):
self.Update()
self.HandleEvents()
self.Draw()
def Update(self):
if (self.nextstep <= 0):
self.nextstep = self.delay
self.UpdateCells()
if not self.paused:
self.nextstep -= self.clock.tick(100)
pass
def HandleEvents(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
self.running = False
if event.key == pygame.K_SPACE:
self.Pause()
if event.key == pygame.K_PLUS and self.delay > 100:
self.delay -= 100
if event.key == pygame.K_MINUS and self.delay < 1000:
self.delay += 100
if event.key == pygame.K_c:
for i in xrange(self.size[0]):
for j in xrange(self.size[1]):
self.SetCell(i,j,False)
p = pygame.mouse.get_pressed()
if p[0]:
self.SetCell(pygame.mouse.get_pos()[0] / self.squaresize, pygame.mouse.get_pos()[1] / self.squaresize, True)
elif p[2]:
self.SetCell(pygame.mouse.get_pos()[0] / self.squaresize,
pygame.mouse.get_pos()[1] / self.squaresize, False)
def Draw(self):
self.screen.fill((128,128,128))
for i in xrange(self.size[0]):
for j in xrange(self.size[1]):
if self.cells[i][j]:
s = self.alive
else:
s = self.dead
self.screen.blit(s, (i*self.squaresize, j*self.squaresize))
for i in xrange(self.size[0]):
pygame.draw.line(self.screen, (50,50,50),(i*self.squaresize,0),(self.squaresize*i,self.size[0]*self.squaresize))
for j in xrange(self.size[1]):
pygame.draw.line(self.screen, (50,50,50), (0,
j*self.squaresize),(self.size[0]*self.squaresize, j*self.squaresize))
pygame.display.flip()
if __name__ == "__main__":
SIZE = (int(sys.argv[1]),int(sys.argv[2]))
game = Game(SIZE, int(sys.argv[3]), 200)
game.MainLoop()