-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathworld.py
More file actions
162 lines (130 loc) · 4.14 KB
/
world.py
File metadata and controls
162 lines (130 loc) · 4.14 KB
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
#!/usr/bin/python
"""
Random world generator.
Very slow :\
"""
import random
import sys
import pygame
from pygame.locals import *
WIDTH = 640
HEIGHT = 480
START_HEIGHT = 128
def init_world():
world = [[START_HEIGHT for width in range(WIDTH)] for height in range(HEIGHT)]
assert len(world) == HEIGHT
assert len(world[0]) == WIDTH
return world
# and access is fast
def reset_world(world):
for x in range(WIDTH):
for y in range(HEIGHT):
world[y][x] = 0
return world
def split_horizontal(world):
# pick two random points on left and right borders
y1 = random.randint(0, HEIGHT-1)
y2 = random.randint(0, HEIGHT-1)
gradient = float(y2 - y1) / WIDTH
sign = random.choice((2, 1, -1, -2))
for x in range(WIDTH):
cutoff = y1 + x * gradient
for y in range(HEIGHT):
if y >= cutoff:
# below
world[y][x] += sign
else:
# above
world[y][x] -= sign
world[y][x] = max(0, min(255, world[y][x]))
#return world
def split_vertical(world):
# pick two random points on top and bottom borders
x1 = random.randint(0, WIDTH-1)
x2 = random.randint(0, WIDTH-1)
while x1 == x2:
x2 = random.randint(0, WIDTH-1)
gradient = float(x2 - x1) / HEIGHT
sign = random.choice((2, 1, -1, -2))
for y in range(HEIGHT):
cutoff = x1 + y * gradient
for x in range(WIDTH):
if x >= cutoff:
# below
world[y][x] += sign
else:
# above
world[y][x] -= sign
world[y][x] = max(0, min(255, world[y][x]))
def iter_world(world, iterations=1):
for i in range(iterations):
method = random.choice((split_horizontal, split_vertical))
print (". ", end="")
sys.stdout.flush()
#world = method(world)
method(world)
print()
#return world
def draw_world(world, screen, water_level=32):
screen.fill((0, 0, 0))
for x in range(WIDTH):
for y in range(HEIGHT):
height = world[y][x]
if height > water_level:
screen.set_at((x, y), (height, height, height))
else:
screen.set_at((x, y), (86, 165, 236)) # steel blue
pygame.display.flip()
def main():
screen = pygame.display.set_mode((WIDTH, HEIGHT), DOUBLEBUF)
clock = pygame.time.Clock()
world = init_world()
quitting = False
continuous = False
water_level = START_HEIGHT - 2
while 1:
if continuous:
iterations = 1
else:
iterations = 0
# keypresses
for event in pygame.event.get():
if event.type == QUIT:
quitting = True
if event.type == KEYDOWN:
if event.key == K_SPACE:
continuous = not continuous
iterations = 1
if event.key == K_1:
continuous = False
iterations = 1
if event.key == K_2:
continuous = False
iterations = 10
if event.key == K_3:
continuous = False
iterations = 50
if event.key == K_4:
continuous = False
iterations = 100
if event.key == K_EQUALS:
water_level += 1
print ("water level:", water_level)
draw_world(world, screen, water_level)
if event.key == K_MINUS:
water_level -= 1
print ("water level:", water_level)
draw_world(world, screen, water_level)
if event.key == K_ESCAPE:
quitting = True
if iterations > 0:
if not continuous:
print (iterations, "iterations")
sys.stdout.flush()
iter_world(world, iterations)
draw_world(world, screen, water_level)
#clock.tick(30)
if quitting:
break
if __name__ == '__main__':
main()