-
Notifications
You must be signed in to change notification settings - Fork 6
/
LifeGame.py
138 lines (101 loc) · 2.93 KB
/
LifeGame.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
# -*- coding:utf-8 -*-
import math
import random
import os
import time
import pygame
global timepause
timepause = 10
class cell(object):
"""docstring for cell"""
def __init__(self, nowstate=0, nextstate=0,x=0,y=0):
super(cell, self).__init__()
self.nowstate = nowstate
self.nextstate= nowstate
self.x = x
self.y = y
def updatecell(self):
self.nowstate = self.nextstate
self.nextstate = 0
class world(object):
"""docstring for world"""
def __init__(self, scale = 100):
super(world, self).__init__()
self.scale = scale
self.worldmap = [[]]
self.creatworld
pygame.init()
self.py = 5
self.px = 5
self.xlen = 400/self.scale
self.ylen = 400/self.scale
self.screen = pygame.display.set_mode([2*self.px+self.scale*self.xlen,2*self.py+self.scale*self.ylen])
def creatworld(self):
worldmap = []
for x in range(self.scale):
raw = []
for y in range(self.scale):
onecell = cell(random.randint(0,1),0,x,y)
raw.append(onecell)
worldmap.append(raw)
self.worldmap = worldmap
def outlook(self,x,y):
neighbourstate = 0
for i in range(3):
for j in range (3):
ii = i
jj = j
if (x == self.scale-1 and i == 2):
ii = -self.scale+2
if (y == self.scale-1 and j ==2):
jj = -self.scale+2
neighbourstate += self.worldmap[x+ii-1][y+jj-1].nowstate
neighbourstate -= self.worldmap[x][y].nowstate
return neighbourstate
def evolution(self):
for i in range(self.scale):
for j in range(self.scale):
neighbourstate = self.outlook(i,j)
if (neighbourstate == 3 ):
self.worldmap[i][j].nextstate = 1
elif(neighbourstate == 2 ):
self.worldmap[i][j].nextstate = self.worldmap[i][j].nowstate
else:
self.worldmap[i][j].nextstate = 0
def updateworld(self):
for i in range(self.scale):
for j in range(self.scale):
self.worldmap[i][j].updatecell()
def display(self):
for i in range(20):
self.drawupdateworld()
self.updateworld()
self.evolution()
time.sleep(1)
def displaycell(self,x,y):
for i in range(10):
print self.worldmap[x][y].nowstate, self.worldmap[x][y].nextstate
self.updateworld()
self.evolution()
time.sleep(0.3)
pygame.exit()
def drawupdateworld(self):
white = (255,255,255)
black = (0,0,0)
for i in range(self.scale):
for j in range(self.scale):
if self.worldmap[i][j].nowstate == 1:
pygame.draw.rect(self.screen,white,[i*self.xlen+self.px,j*self.ylen+self.py,self.xlen,self.ylen])
if self.worldmap[i][j].nowstate == 0:
pygame.draw.rect(self.screen,black,[i*self.xlen+self.px,j*self.ylen+self.py,self.xlen,self.ylen])
pygame.display.update()
# pygame.init()
# screen = pygame.display.set_mode([sizey,sizex])
# pygame.draw.rect(screen,white,[px,py,xlen,ylen])
# pygame.draw.rect(screen,white,[px+40,py+40,xlen,ylen])
# for i in range(30):
# pygame.display.update([px,py,xlen+100,ylen+100])
# time.sleep(1)
oneworld = world(30)
oneworld.creatworld()
oneworld.display()