-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.bzl
95 lines (80 loc) · 2.08 KB
/
main.bzl
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
load("loop.bzl", "loop")
load("lib.bzl", "newlib")
WIDTH = 20
HEIGHT = 20
UPDATE_EVERY_N_LOOPS = 4
def reset_game(lib):
lib.set("position", [(0, 0)])
lib.set("direction", "s")
lib.set("old_direction", "s")
lib.set("iteration", 0)
lib.set("ate_food", True)
lib.set("food", (10, 10))
def update(lib):
dir = lib.get("direction")
pos = lib.get("position")
x, y = pos[0]
if dir == "a":
x = x-1
if x < 0:
x = WIDTH - 1
if dir == "d":
x = x + 1
if x >= WIDTH:
x = 0
if dir == "w":
y = y - 1
if y < 0:
y = HEIGHT - 1
if dir == "s":
y = y + 1
if y >= HEIGHT:
y = 0
food_x, food_y = lib.get("food")
if x == food_x and y == food_y:
lib.set("food", (lib.random(0, WIDTH), lib.random(0, HEIGHT)))
else:
pos.pop()
alive = True
for body_x, body_y in pos:
if body_x == x and body_y == y:
alive = False
if alive:
pos.insert(0, (x, y))
lib.set("position", pos)
lib.set("old_direction", dir)
else:
reset_game(lib)
def core_loop(lib):
c = lib.read()
old_dir = lib.get("old_direction")
if c == "w" and old_dir != "s":
lib.set("direction", c)
if c == "s" and old_dir != "w":
lib.set("direction", c)
if c == "a" and old_dir != "d":
lib.set("direction", c)
if c == "d" and old_dir != "a":
lib.set("direction", c)
iteration = lib.get("iteration")
if iteration % UPDATE_EVERY_N_LOOPS == 0:
update(lib)
lib.set("iteration", iteration + 1)
for x in range(WIDTH):
for y in range(HEIGHT):
lib.set_px(x, y, "⬛")
food_x, food_y = lib.get("food")
lib.set_px(food_x, food_y, "🍎")
for player_x, player_y in lib.get("position"):
lib.set_px(player_x, player_y, "🟩")
lib.flush()
def _main(repo_ctx):
lib = newlib(repo_ctx, struct(
w = WIDTH,
h = HEIGHT,
))
reset_game(lib)
loop(core_loop, lib)
main = repository_rule(
_main
)