-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.py
98 lines (78 loc) · 3.13 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 23 13:50:07 2022
@author: richa
"""
import random
from typing import List
from typing import Tuple
import pygame
from hexagon import FlatTopHexagonTile
from hexagon import HexagonTile
# pylint: disable=no-member
def create_hexagon(position, radius=50, flat_top=False) -> HexagonTile:
"""Creates a hexagon tile at the specified position"""
class_ = FlatTopHexagonTile if flat_top else HexagonTile
return class_(radius, position, colour=get_random_colour())
def get_random_colour(min_=150, max_=255) -> Tuple[int, ...]:
"""Returns a random RGB colour with each component between min_ and max_"""
return tuple(random.choices(list(range(min_, max_)), k=3))
def init_hexagons(num_x=20, num_y=20, flat_top=False) -> List[HexagonTile]:
"""Creates a hexaogonal tile map of size num_x * num_y"""
# pylint: disable=invalid-name
leftmost_hexagon = create_hexagon(position=(-50, -50), flat_top=flat_top)
hexagons = [leftmost_hexagon]
for x in range(num_y):
if x:
# alternate between bottom left and bottom right vertices of hexagon above
index = 2 if x % 2 == 1 or flat_top else 4
position = leftmost_hexagon.vertices[index]
leftmost_hexagon = create_hexagon(position, flat_top=flat_top)
hexagons.append(leftmost_hexagon)
# place hexagons to the left of leftmost hexagon, with equal y-values.
hexagon = leftmost_hexagon
for i in range(num_x):
x, y = hexagon.position # type: ignore
if flat_top:
if i % 2 == 1:
position = (x + hexagon.radius * 3 / 2, y - hexagon.minimal_radius)
else:
position = (x + hexagon.radius * 3 / 2, y + hexagon.minimal_radius)
else:
position = (x + hexagon.minimal_radius * 2, y)
hexagon = create_hexagon(position, flat_top=flat_top)
hexagons.append(hexagon)
return hexagons
def render(screen, hexagons):
"""Renders hexagons on the screen"""
screen.fill((0, 0, 0))
for hexagon in hexagons:
hexagon.render(screen)
# draw borders around colliding hexagons and neighbours
mouse_pos = pygame.mouse.get_pos()
colliding_hexagons = [
hexagon for hexagon in hexagons if hexagon.collide_with_point(mouse_pos)
]
for hexagon in colliding_hexagons:
for neighbour in hexagon.compute_neighbours(hexagons):
neighbour.render_highlight(screen, border_colour=(100, 100, 100))
hexagon.render_highlight(screen, border_colour=(0, 0, 0))
pygame.display.flip()
def main():
"""Main function"""
pygame.init()
screen = pygame.display.set_mode((600, 400))
clock = pygame.time.Clock()
hexagons = init_hexagons(flat_top=True)
terminated = False
while not terminated:
for event in pygame.event.get():
if event.type == pygame.QUIT:
terminated = True
for hexagon in hexagons:
hexagon.update()
render(screen, hexagons)
clock.tick(50)
pygame.display.quit()
if __name__ == "__main__":
main()