-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
161 lines (131 loc) · 5.26 KB
/
game.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import pygame
from random import randint
class Game:
field = []
field_width = 5
field_height = 5
checked_fields = []
screen = None
graphics_multiplyer = 10
infinite = True
population = []
def __init__(self, field_width, field_height, checked_fields, screen, graphics_multiplyer, infinite):
self.field_width = field_width
self.field_height = field_height
self.checked_fields = checked_fields
self.screen = screen
self.graphics_multiplyer = graphics_multiplyer
self.infinite = infinite
self.construct_field()
def construct_field(self):
for n in range(self.field_width*self.field_height):
if n in self.checked_fields:
self.field.append(1)
else:
self.field.append(0)
def step(self):
old_board = list(self.field)
for x in range(len(self.field)):
neighbours = self.check_neighbours(x, old_board)
if old_board[x] == 1:
if neighbours < 2 or neighbours > 3:
self.field[x] = 0
else:
if neighbours == 3:
self.field[x] = 1
self.calculate_population()
def check_neighbours(self, x, board):
neighbours = 0
directions = [
x-self.field_width-1,
x-self.field_width,
x-self.field_width+1,
x-1, x+1,
x+self.field_width-1,
x+self.field_width,
x+self.field_width+1
]
for n in directions:
# sides
if x % self.field_width == self.field_width-1 and n % self.field_width == 0:
if not self.infinite: continue
n -= self.field_width
elif x % self.field_width == 0 and n % self.field_width == self.field_width-1:
if not self.infinite: continue
n += self.field_width
# bottom and top
if int(x / self.field_width) == 0 and n < 0:
if not self.infinite: continue
n += self.field_width * self.field_height
elif int(x / self.field_width) == self.field_height-1 and n > (self.field_width * self.field_height)-1:
if not self.infinite: continue
n -= self.field_width * self.field_height
neighbours += board[n%(self.field_width*self.field_height)]
return neighbours
def showNeighbours(self, pos):
# get index
x = pos[0]
x = int(x/self.graphics_multiplyer)
y = pos[1]
y = int(y/self.graphics_multiplyer)
x = self.field_width * y + x
directions = [
x-self.field_width-1,
x-self.field_width,
x-self.field_width+1,
x-1, x+1,
x+self.field_width-1,
x+self.field_width,
x+self.field_width+1
]
for n in directions:
# sides
if x % self.field_width == self.field_width-1 and n % self.field_width == 0:
if not self.infinite: continue
n -= self.field_width
elif x % self.field_width == 0 and n % self.field_width == self.field_width-1:
if not self.infinite: continue
n += self.field_width
# top and bottom
if int(x / self.field_width) == 0 and n < 0:
if not self.infinite: continue
n += self.field_width * self.field_height
elif int(x / self.field_width) == self.field_height-1 and n > (self.field_width * self.field_height)-1:
if not self.infinite: continue
n -= self.field_width * self.field_height
left = n % self.field_width
top = int(n / self.field_width)
rect = pygame.Rect(left*self.graphics_multiplyer, top*self.graphics_multiplyer, self.graphics_multiplyer, self.graphics_multiplyer)
pygame.draw.rect(self.screen, (255,0,0), rect, 1)
def print_board(self):
for n in range(len(self.field)):
rect = pygame.Rect((n%self.field_width)*self.graphics_multiplyer, int(n/self.field_width)*self.graphics_multiplyer, self.graphics_multiplyer, self.graphics_multiplyer)
if self.field[n] == 1:
pygame.draw.rect(self.screen, (255,255,255), rect, 0)
# else:
# pygame.draw.rect(self.screen, (255,255,255), rect, 1)
def change(self, pos):
# get index
x = pos[0]
x = int(x/self.graphics_multiplyer)
y = pos[1]
y = int(y/self.graphics_multiplyer)
num = self.field_width * y + x
if self.field[num] == 1:
self.field[num] = 0
else:
self.field[num] = 1
def reset(self):
for n in range(self.field_width*self.field_height):
if n in self.checked_fields:
self.field[n] = 1
else:
self.field[n] = 0
def random(self):
for n in range(self.field_width*self.field_height):
self.field[n] = randint(0, 1) % 2
def calculate_population(self):
population = 0
for n in self.field:
population += n
self.population.append(population)