-
Notifications
You must be signed in to change notification settings - Fork 0
/
cell.py
51 lines (28 loc) · 1.24 KB
/
cell.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
import pygame
from pygame.locals import *
from random import choice
class Cell(pygame.sprite.Sprite):
def __init__(self, type):
self.type = ""
self.type_sprites = {
'water': (0, 0, 255),
'grass': (0,255,0),
'boat': (139,69,19)
}
self.size = (50,50)
self.width = 2
self.cells_matrix = {}
pygame.sprite.Sprite.__init__(self)
def handle_event(self, screen,event):
if event.type == MOUSEBUTTONDOWN:
if event.button == 1:
index = (event.pos[0] // self.size[0], event.pos[1] // self.size[1])
pos = self.cells_matrix[index]
pygame.draw.rect(screen, self.type_sprites['boat'], Rect(pos, self.size))
def build_grid(self, screen):
for width in range(screen.get_width()):
for height in range(screen.get_height()):
random_color = choice(['water', 'grass', 'boat'])
pos = (self.size[0]*width, self.size[1]*height)
self.cells_matrix[(width,height)] = pos
pygame.draw.rect(screen, self.type_sprites[random_color], Rect(pos, self.size))