-
Notifications
You must be signed in to change notification settings - Fork 1
/
window.py
180 lines (148 loc) · 6.31 KB
/
window.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
from pygame.locals import *
import sys
import logging
from utils import Color
from world import World
import settings
class Window(object):
SIZE = 600, 600
CELL_SIZE = 60
def __init__(self, log, colors, options, args):
logging.basicConfig(filename=log, level=logging.DEBUG, filemode='w')
Color.init(colors)
self.background = Color.by_html('c6cfbf')
if len(args) > 0:
self.world = World.load_json(args[0])
self.cols = self.world.cols
self.rows = self.world.rows
self.density = int(100.0 * len(self.world._objects) / (self.cols * self.rows))
else:
self.cols = options.cols
self.rows = options.rows
self.density = options.density
self.world = self.create_world()
self.is_force = False
self.size = (self.cols * Window.CELL_SIZE, self.rows * Window.CELL_SIZE)
self.scale = float(min(Window.SIZE)) / max(self.size)
self.position = [Window.SIZE[0]/2, Window.SIZE[0]/2]
# set up pygame
pygame.init()
pygame.display.set_caption('Life')
self.clock = pygame.time.Clock()
# set up the window
self.surface = pygame.display.set_mode(Window.SIZE, 0, 32)
self.world_surface = pygame.Surface(self.size)
# set up fonts
self.font = pygame.font.SysFont(None, 16)
self.font_color = Color.by_name('white')
self.downpos = None
def create_world(self):
from creatures import Predator, Herbivore, Plant
if self.density < 1 or self.density > 100:
sys.exit()
world = World(cols=self.cols, rows=self.rows)
cnt = int(world.cols * world.rows * 0.01 * self.density / 3)
for i in xrange(cnt):
pos = world.get_rnd_free_space()
if pos is None:
break
creature = Predator(x=pos[0], y=pos[1])
world.queue.append((i * 21, creature))
for i in xrange(cnt):
pos = world.get_rnd_free_space()
if pos is None:
break
creature = Herbivore(x=pos[0], y=pos[1])
world.queue.append((i * 22, creature))
for i in xrange(cnt):
pos = world.get_rnd_free_space()
if pos is None:
break
creature = Plant(x=pos[0], y=pos[1])
world.queue.append((i * 23, creature))
world.start(0.1)
return world
def draw_text(self, text, x, y):
textobj = self.font.render(text, 1, self.font_color)
textrect = textobj.get_rect()
textrect.topleft = (x, y)
self.surface.blit(textobj, textrect)
def restart(self):
World.clear_all()
self.is_force = False
self.world = self.create_world()
def events(self):
for event in pygame.event.get():
if event.type == QUIT:
self.world.stop()
pygame.quit()
sys.exit()
elif event.type == KEYUP:
if event.key == K_ESCAPE:
self.world.stop()
pygame.quit()
sys.exit()
elif event.key == K_r:
self.restart()
elif event.key == K_s:
self.world.save_json('./current_map.json')
print "Save complete"
elif event.key == K_f:
self.is_force = not self.is_force
if self.is_force:
self.world.begin_force()
print "Begin Force"
else:
self.world.end_force()
print "End Force"
elif event.type == MOUSEBUTTONDOWN:
self.downpos = event.pos
if event.button == 4:
self.scale *= 1.05
elif event.button == 5:
self.scale *= 0.95
#for i in range(2):
# self.position[i] += (self.size[i]/2.0 - self.position[i]) * (1 - self.scale)
elif event.type == MOUSEBUTTONUP:
self.downpos = None
elif self.downpos and event.type == pygame.MOUSEMOTION:
dx = event.pos[0] - self.downpos[0]
dy = event.pos[1] - self.downpos[1]
if dx != 0 or dy != 0:
self.position[0] += dx
self.position[1] += dy
self.downpos = event.pos
def loop(self):
try:
dt = 0
# run the game loop
while True:
# check for events
self.events()
# draw the black background onto the surface
self.world_surface.fill(self.background)
for obj in self.world._objects:
obj.update(dt)
obj.draw(self.world_surface, Window.CELL_SIZE)
cursize = [int(self.world_surface.get_width() * self.scale), int(self.world_surface.get_height() * self.scale)]
scaled_surf = pygame.transform.smoothscale(self.world_surface, cursize)
self.surface.fill(self.background)
scaled_surf_pos = scaled_surf.get_rect(centerx=self.position[0], centery=self.position[1])
self.surface.blit(scaled_surf, scaled_surf_pos)
self.draw_text("Turns: %i, Pr: %2i, He: %2i, Pl: %2i, Re: %2i, De: %2i" % (self.world.turns,
self.world.predators,
self.world.herbivores,
self.world.plants,
self.world.reproductions,
self.world.deaths),
10, 10)
pygame.display.flip()
dt = self.clock.tick(settings.FPS)
except:
import traceback
logging.debug(traceback.format_exc())
self.world.stop()
sys.exit()