-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
78 lines (61 loc) · 2.18 KB
/
main.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
from painter import Painter
from grid import Grid
import pygame as pg
import math
(width, height) = (800, 600)
window = pg.display.set_mode((width, height))
clock = pg.time.Clock()
painter = Painter(window, width, height)
grid = Grid()
cameraPosition = (0,0)
mousePressed = False
running = True
simulate = False
framesPerStep = 5
framesToNextStep = framesPerStep
tools = ("Move Tool", "Edit Tool")
toolIndex = 1
while running:
painter.draw(grid.getCells(), cameraPosition)
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
elif event.type == pg.MOUSEBUTTONDOWN:
if(event.button == 1):
if(toolIndex == 1):
mousePressed = True
elif(toolIndex == 2):
pos = pg.mouse.get_pos()
actualCellSize = painter.getActualCellSize()
gridThickness = painter.getGridThickness()
x = int((pos[0]+cameraPosition[0])/(math.ceil(actualCellSize)+gridThickness))
y = int((pos[1]+cameraPosition[1])/(math.ceil(actualCellSize)+gridThickness))
if(grid.isCellLive((x, y))):
grid.removeCell((x, y))
else:
grid.addCell((x, y))
if(event.button == 4):
painter.ZoomIn()
if(event.button == 5):
painter.ZoomOut()
elif event.type == pg.MOUSEBUTTONUP:
if(event.button == 1):
mousePressed = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_SPACE:
simulate = not simulate
elif event.key == pg.K_1:
toolIndex = 1
elif event.key == pg.K_2:
toolIndex = 2
elif event.key == pg.K_g:
painter.toggleGrid()
clock.tick(60)
if(simulate):
if(framesToNextStep == 0):
grid.nextState()
framesToNextStep = framesPerStep
framesToNextStep-=1
mouseMove = pg.mouse.get_rel()
if(mousePressed):
cameraPosition = (cameraPosition[0]-mouseMove[0], cameraPosition[1]-mouseMove[1])