-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathant.py
More file actions
90 lines (72 loc) · 2.87 KB
/
ant.py
File metadata and controls
90 lines (72 loc) · 2.87 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
import pygame
import random
pygame.init()
SCREEN_HEIGHT = 1000
SCREEN_WIDTH = 1000
TILE_SIZE = 10
FPS = 240
THEMES = {
"greyscale": ["black", "white", "grey52", "grey32"],
"sakura": ["lightpink4", "lightpink", "lightpink2", "lightpink3"],
"ice": ["lightblue4", "lightblue1", "lightblue2", "lightblue3"],
"purple": ["mediumpurple4", "mediumpurple1", "mediumpurple2", "mediumpurple3"],
}
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT), pygame.NOFRAME)
clock = pygame.time.Clock()
class Ant:
def __init__(self):
self.pos_x = random.randrange(0, SCREEN_WIDTH // TILE_SIZE)
self.pos_y = random.randrange(0, SCREEN_HEIGHT // TILE_SIZE)
self.movement = random.choice([(0, -1), (0, 1), (-1, 0), (1, 0)])
def turn_left(self):
x, y = self.movement
if abs(x) == abs(y):
self.movement = (0, -y) if x == y else (-x, 0)
self.movement = (y, -x)
def turn_right(self):
x, y = self.movement
if abs(x) == abs(y):
self.movement = (-x, 0) if x == y else (0, -y)
self.movement = (-y, x)
def move_diagonally(self):
self.movement = random.choice([(-1, -1), (-1, 1), (1, -1), (1, 1)])
def generate_step(self, colored_tiles, colors):
current_pos = (self.pos_x, self.pos_y)
if current_pos in colored_tiles:
if colored_tiles[current_pos] == colors[1]:
self.turn_right()
colored_tiles[current_pos] = colors[2]
elif colored_tiles[current_pos] == colors[2]:
# self.move_diagonally() # -> added diagonal movement
self.turn_left() # -> traditional langton ant
colored_tiles[current_pos] = colors[3]
elif colored_tiles[current_pos] == colors[3]:
self.turn_right()
colored_tiles.pop(current_pos)
else:
self.turn_left()
colored_tiles[current_pos] = colors[1]
self.pos_x += self.movement[0]
self.pos_y += self.movement[1]
self.pos_x %= (SCREEN_WIDTH // TILE_SIZE)
self.pos_y %= (SCREEN_HEIGHT // TILE_SIZE)
def draw_step(self, colored_tiles):
for pos, color in colored_tiles.items():
pygame.draw.rect(screen, pygame.Color(color), (pos[0] * TILE_SIZE, pos[1] * TILE_SIZE, TILE_SIZE, TILE_SIZE))
def main():
running = True
colors = THEMES["purple"]
ant = Ant()
colored_tiles = {(ant.pos_x, ant.pos_y): colors[1]}
while running:
# clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(pygame.Color(colors[0]))
ant.generate_step(colored_tiles, colors)
ant.draw_step(colored_tiles)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main()