-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
235 lines (183 loc) · 6.78 KB
/
game.py
File metadata and controls
235 lines (183 loc) · 6.78 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import pygame
import random
import time
import json
from Vector2 import Vector2
from Board import PixelBoard
with open("config.json") as f:
conf = json.load(f)
# Define Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BG_COLOR = [44] * 3
GRID_COLOR = [22] * 3
CURSOR_COLOR = [255, 127, 0]
# Configuration
DEBUG = conf['debug']
GRID_SIZE = conf['grid_size']
SCREEN_SIZE = Vector2(conf['screen_x'], conf['screen_y'])
SCREEN_SIZE.x = int(SCREEN_SIZE.x / GRID_SIZE) * GRID_SIZE
SCREEN_SIZE.y = int(SCREEN_SIZE.y / GRID_SIZE) * GRID_SIZE
GRID_RESOLUTION = Vector2(int(SCREEN_SIZE.x / GRID_SIZE), int(SCREEN_SIZE.y / GRID_SIZE))
BRUSH_SIZE = conf['brush_size']
print(f"Screen size: {SCREEN_SIZE.x} | {SCREEN_SIZE.y}")
print(f"Grid size: {GRID_RESOLUTION.x} | {GRID_RESOLUTION.y}")
FPS = conf['fps']
# Members
running = True
paused = False
recording = False
pixels = PixelBoard(GRID_RESOLUTION.x, GRID_RESOLUTION.y)
brush_size = BRUSH_SIZE
def screen_to_pixel_coordinates(screen_pos):
return Vector2(int(screen_pos.x / GRID_SIZE), int(screen_pos.y / GRID_SIZE))
mousedown = False
mousepos_in_game_coo = None
clear = False
def process_input():
global running
global paused
global mousedown
global mousepos_in_game_coo
global clear
global brush_size
global recording
global DEBUG
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key in [pygame.K_ESCAPE, pygame.K_F8]:
running = False
elif event.key in [pygame.K_RETURN]:
update(True)
# render()
elif event.key in [pygame.K_r]:
recording = not recording
print(f"Recording: {recording}")
elif event.key in [pygame.K_p, pygame.K_SPACE]:
paused = not paused
elif event.key in [pygame.K_BACKSPACE]:
pixels.clear()
# render()
elif event.key in [pygame.K_TAB]:
DEBUG = not DEBUG
elif event.type == pygame.MOUSEBUTTONDOWN:
pos = Vector2(pygame.mouse.get_pos())
mousepos_in_game_coo = screen_to_pixel_coordinates(pos)
if event.button == pygame.BUTTON_LEFT:
mousedown = True
brush_draw_pixels(mousepos_in_game_coo) # Allow adding Pixel when paused
elif event.button == pygame.BUTTON_RIGHT:
clear = True
elif event.button == pygame.BUTTON_WHEELUP:
brush_size += 1
elif event.button == pygame.BUTTON_WHEELDOWN:
brush_size = max(1, brush_size - 1)
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == pygame.BUTTON_LEFT:
mousedown = False
elif event.button == pygame.BUTTON_RIGHT:
clear = False
mousepos_in_game_coo = None
elif event.type == pygame.MOUSEMOTION:
pos = Vector2(pygame.mouse.get_pos())
mousepos_in_game_coo = screen_to_pixel_coordinates(pos)
def brush_draw_pixels(pos, clearing=False):
radius = brush_size
for y in range(-radius, radius + 1):
for x in range(-radius, radius + 1):
screen_x = pos.x + x
screen_y = pos.y + y
if 0 <= screen_x < GRID_RESOLUTION.x and 0 <= screen_y < GRID_RESOLUTION.y:
if x*x + y*y < radius * radius: # Make round brush
if clearing:
f = pixels.get_pixel(screen_x, screen_y)
for p in f:
pixels.remove_pixel(p)
else:
spawn_pixel(Vector2(screen_x, screen_y))
tick_spawn_counter = 0
tick_spawn_duration = 3
def update(force = False):
global tick_spawn_counter
global tick_spawn_duration
if force or not paused:
if conf["water_running"]:
tick_spawn_counter += 1
if tick_spawn_counter >= tick_spawn_duration:
tick_spawn_counter -= tick_spawn_duration
pixels.add_pixel(Vector2(int(GRID_RESOLUTION.x * 0.5), 0))
pixels.update()
if DEBUG:
pixels.check_orphans()
if mousepos_in_game_coo != None:
if clear:
brush_draw_pixels(mousepos_in_game_coo, True)
elif mousedown:
brush_draw_pixels(mousepos_in_game_coo)
recording_counter = 0
def render():
global recording_counter
screen.fill(BG_COLOR)
def draw_pixels():
i = 0 # was pixel.id
for pixel in pixels:
#val = int((i / len(pixels)) * 127 + 100)
val = int((i / len(pixels)) * 255)
col = (0, 0, val)
pygame.draw.rect(screen, col,(pixel.x * GRID_SIZE, pixel.y * GRID_SIZE, GRID_SIZE, GRID_SIZE))
if DEBUG:
if pixel.sleeping:
screen.blit(rect_outline, (pixel.x * GRID_SIZE, pixel.y * GRID_SIZE))
i += 1
# Overlay
def draw_overlays():
# Draw cursor
pos = Vector2(pygame.mouse.get_pos())
pos.x = int(pos.x / GRID_SIZE) * GRID_SIZE + int(GRID_SIZE*0.5)
pos.y = int(pos.y / GRID_SIZE) * GRID_SIZE + int(GRID_SIZE*0.5)
pygame.draw.circle(screen, CURSOR_COLOR, [pos.x, pos.y], brush_size * GRID_SIZE, 1)
if DEBUG:
draw_grid()
draw_pixels()
draw_overlays()
pygame.display.flip()
if recording:
rec_id = str(recording_counter)
rec_id = rec_id.zfill(5)
pygame.image.save(screen, f"Recordings/recording_{rec_id}.png")
recording_counter += 1
def _draw_initial_grid():
for x in range(int(SCREEN_SIZE.x / GRID_SIZE)):
pygame.draw.line(background, GRID_COLOR, (x * GRID_SIZE, 0), (x*GRID_SIZE, SCREEN_SIZE.y))
for y in range(int(SCREEN_SIZE.y / GRID_SIZE)):
pygame.draw.line(background, GRID_COLOR, (0, y * GRID_SIZE), (SCREEN_SIZE.x, y*GRID_SIZE))
def draw_grid():
screen.blit(background, (0, 0))
def game_loop():
while running:
process_input()
update()
render()
clock.tick(FPS)
def spawn_pixel(p):
pixels.add_pixel(p.x, p.y)
pygame.init()
pygame.mixer.init() ## For sound
screen = pygame.display.set_mode((SCREEN_SIZE.x, SCREEN_SIZE.y))
pygame.display.set_caption(f"Fluid Simulation {SCREEN_SIZE.x}x{SCREEN_SIZE.y} - [{GRID_SIZE}]")
clock = pygame.time.Clock() ## For syncing the FPS
# Surface blits
background = pygame.Surface((SCREEN_SIZE.x, SCREEN_SIZE.y), pygame.SRCALPHA, 32)
_draw_initial_grid()
rect_outline = pygame.Surface((GRID_SIZE, GRID_SIZE), pygame.SRCALPHA, 32)
# col = [55] * 3
col = (127, 0, 127)
col = (0, 127, 0)
pygame.draw.rect(rect_outline, col, (0, 0, GRID_SIZE, GRID_SIZE), 1)
game_loop()
pygame.quit()