-
Notifications
You must be signed in to change notification settings - Fork 0
/
arena.py
133 lines (103 loc) · 4.24 KB
/
arena.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import random
from datetime import datetime
from drawille.graphics_utils import get_terminal_size_in_pixels, COLOR_MAGENTA, CH_HEIGHT, CH_WIDTH, frange, get_pos, COLOR_WHITE
from image2term import image2term
__author__ = 'ericmuxagata'
NUM_SEEDS = 4
def set_pos(frame,xd,yd):
return [(x+xd,y+yd,c) for (x,y,c) in frame]
# Massive class containing most of the game logic.
class Arena(object):
def __init__(self,height,path):
self.w, self.h, self.frame = image2term(path,height=height)
self.x = self.y = 0
tw, th = get_terminal_size_in_pixels()
self.set_pos(tw/2 - self.w/2,th/2 - self.h/2)
bapho_head = image2term('img/baphomet_head.gif', height=0.8*self.h, invert=True)
self.frame.extend(set_pos(bapho_head[2],self.x + self.w/2 - bapho_head[0]/2,self.y + self.h/2 - bapho_head[1]/2))
bapho_title = image2term('img/baphomet_title.jpg', height=85,invert=True)
self.frame.extend(set_pos(bapho_title[2],self.x + self.w/2 - bapho_title[0]/2,self.y - 20 - bapho_title[1]/2))
# Generate seeds.
random.seed(datetime.now())
self.seeds = self.generate_seeds(NUM_SEEDS)
self.reset_seeds_frame()
# Snakes.
self.snakes = None
def reset_seeds_frame(self):
self.seeds_frame = []
for (sx,sy,c) in self.seeds:
frame = []
for x in frange(sx*CH_WIDTH, (sx+1)*CH_WIDTH, 1.0):
for y in frange(sy*CH_HEIGHT, (sy+1)*CH_HEIGHT, 1.0):
frame.append((x, y, c))
self.seeds_frame.extend(frame)
def generate_seeds(self,n):
xl, xh, yl, yh = self.x + 15, self.x + self.w - 15, self.y + 15, self.y + self.h - 15
seeds = []
for i in xrange(n):
x,y = get_pos(random.randint(xl,xh), random.randint(yl,yh))
seeds.append((x,y,COLOR_MAGENTA))
return seeds
def generate_random_pos(self):
xl, xh, yl, yh = self.x + 15, self.x + self.w - 15, self.y + 15, self.y + self.h - 15
return get_pos(random.randint(xl,xh), random.randint(yl,yh))
def set_pos(self,xd,yd):
self.x, self.y = xd, yd
self.frame = [(x + self.x, y + self.y, c) for (x,y,c) in self.frame]
def set_color(self,color):
self.frame = [(x, y, color) for (x,y,c) in self.frame]
def add_snakes(self, snakes):
self.snakes = snakes
def snakes_frame(self):
frame = []
for snake in self.snakes:
frame.extend(snake.frame())
return frame
def show_scores_frame(self):
frame = []
uix, uiy = self.x + 60, self.y + self.h + 20
for snake in self.snakes:
frame.append((uix, uiy, snake.color, "Score: %d" % snake.max_points))
uix += 30
return frame
def show_timer_frame(self, t):
frame = []
t = int(t)
uix, uiy = self.x + 90, self.y + self.h + 30
frame.append((uix, uiy, COLOR_WHITE, "TIME REMAINING: %02d:%02d" % (t/60, t % 60)))
return frame
def show_victory_frame(self):
frame = []
winner = max(self.snakes, key=lambda s: s.max_points)
color = winner.color if winner else COLOR_WHITE
self.set_color(color)
uix, uiy = self.x + 70, self.y + self.h + 30
frame.append((uix, uiy, color, "LASCIATE OGNI SPERANZA, VOI CH'ENTRATE. VICTORY."))
return frame
# Game logic.
def is_out_of_bounds(self, x, y):
x *= CH_WIDTH
y *= CH_HEIGHT
return (x < self.x) or (x > self.x + self.w) or (y < self.y) or (y > self.y + self.h)
def find_and_eat_seed(self, x, y):
seed = None
for s in self.seeds:
sx,sy = s[0], s[1]
if sx == x and sy == y:
seed = s
break
# Fail and quit.
if not seed:
return False
# Add new seed and return success.
self.seeds.remove(seed)
self.seeds.extend(self.generate_seeds(1))
self.reset_seeds_frame()
return True
def has_hit_snake(self,x,y,target):
for snake in self.snakes:
for node in snake.nodes:
if node.x == x and node.y == y and node != target:
return True
return False
arena = Arena(130,'img/arena.png')