-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameLifeGraphics.py
123 lines (112 loc) · 4.3 KB
/
GameLifeGraphics.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
import pygame, sys
from pygame.locals import *
from GameOfLife import GameOfLife
from SquareGridUI import SquareGridUI
from colors import *
from music import LifeAudio
# Program parameters
resolution = (800, 800)
board_size = 20
gaps = 1
time_per_col = .25
LIVING_CELL_COLORS = MAT_ORANGE
MUSIC_NOTE_COLOR = MAT_AMBER
DEAD_CELL_COLORS = BLUE_GREY
GRID_COLOR = BLUE_GREY
HIGHLIGHT_COLOR = BLUE_GREY_HIGH
# Global initialization
pygame.init()
pygame.display.set_caption("The Music of Life")
DISPLAYSURF = pygame.display.set_mode(resolution, RESIZABLE)
the_grid = SquareGridUI(DISPLAYSURF, board_size, resolution[0], gaps)
the_game = GameOfLife(board_size, board_size)
the_game.random_grid_init()
audio = LifeAudio()
current_column = 0
column_dir = 1
current_row = 0
row_dir = 1
paused = False
def toggle_pause():
global paused
paused = not paused
def increase_speed():
global time_per_col
if time_per_col > .05:
time_per_col -= .025
def decrease_speed():
global time_per_col
time_per_col += .025
# Checks if a given pixel position is on the game window
def is_on_grid(position):
if position[0] > 0 and position[0] < resolution[0]:
if position[1] > 0 and position[1] < resolution[1]:
return True
return False
# Highlights the current mouse position
def highlight_current_mouse_position():
global the_grid
mouse_position = pygame.mouse.get_pos()
if is_on_grid(mouse_position):
square_location = the_grid.get_square_location(mouse_position)
the_grid.color_square(square_location[0], square_location[1], HIGHLIGHT_COLOR)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
toggle_pause()
elif event.key == K_UP:
increase_speed()
elif event.key == K_DOWN:
decrease_speed()
elif event.key == K_r:
the_game.reset_grid()
elif event.key == K_RETURN:
the_game.random_grid_init()
elif event.key == K_g:
mouse_position = pygame.mouse.get_pos()
if is_on_grid(mouse_position):
square_location = the_grid.get_square_location(mouse_position)
the_game.insert_glider(square_location)
elif event.key == K_p:
mouse_position = pygame.mouse.get_pos()
if is_on_grid(mouse_position):
square_location = the_grid.get_square_location(mouse_position)
loc = (square_location[0] - 7, square_location[1] - 7)
the_game.insert_pulsar(loc)
if event.type == MOUSEBUTTONUP:
if paused:
mouse_position = pygame.mouse.get_pos()
if is_on_grid(mouse_position):
square_location = the_grid.get_square_location(mouse_position)
if the_game.grid[square_location[0]][square_location[1]] == GameOfLife.ALIVE:
the_game.grid[square_location[0]][square_location[1]] = GameOfLife.DEAD
else:
the_game.grid[square_location[0]][square_location[1]] = GameOfLife.ALIVE
if not paused:
DISPLAYSURF.fill(BLUE_GREY)
the_grid.draw_grid((0,0), the_game.grid, GRID_COLOR, LIVING_CELL_COLORS)
alive_rows = []
for row in range(the_game.grid.getHeight()):
if the_game.grid[current_column][row] == GameOfLife.ALIVE:
alive_rows.append(row)
the_grid.color_square(current_column, row, MUSIC_NOTE_COLOR)
else:
the_grid.color_square(current_column, row, HIGHLIGHT_COLOR)
current_column += column_dir
if current_column < 0 or current_column == the_game.grid.getWidth():
column_dir *= -1
current_column += 2*column_dir
pygame.display.update()
if len(alive_rows) != 0:
audio.play_notes(alive_rows, time_per_col)
else:
pygame.time.wait(int(time_per_col*1000))
the_game.time_step()
if paused:
the_grid.draw_grid((0,0), the_game.grid, GRID_COLOR, LIVING_CELL_COLORS)
highlight_current_mouse_position()
pygame.display.update()