-
Notifications
You must be signed in to change notification settings - Fork 0
/
fish.py
executable file
·171 lines (133 loc) · 4.3 KB
/
fish.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#!/bin/python3
'''
Somewhat inspired by https://pypi.org/project/fish/
'''
import time
import curses
from random import randint, choice
stdscr = curses.initscr()
height = curses.LINES
width = curses.COLS
# This is easiest, since we don't just want to reverse
arts_r = ["><}}}*>", ">))'>", "><))))°>"]
arts_l = ["<*{{{><", "<'((<", "<°((((><"]
class Fish():
__slot__ = ["x", "y", "color", "is_left", "speed", "art_l", "art_r"]
def __init__(self):
# Bottom 3 are reserved for "rocks"
self.x = randint(1, width-1)
self.y = randint(1, height-4)
# Color number is a color pair number
self.color = randint(0, 7)
self.is_left = randint(0,1)==1
self.speed = randint(1, 3)
# Pick type of fish
art_index = randint(0,len(arts_r)-1)
self.art_l = arts_l[art_index]
self.art_r = arts_r[art_index]
self.l = len(self.art_l)
def erase(self):
# If user resizes window, addstr can throw an exception
try:
stdscr.addstr(self.y, self.x, " "*(self.l+1))
except curses.error:
pass
def display(self):
c = curses.color_pair(self.color) | curses.A_BOLD
# If user resizes window, addstr can throw an exception
try:
if self.is_left:
stdscr.addstr(self.y, self.x, self.art_l, c)
else:
stdscr.addstr(self.y, self.x, self.art_r, c)
except curses.error:
pass
def act(self):
# Undraw fish
self.erase()
# Check left/right wall collision
if self.x >= width-self.speed-self.l or self.x <= self.speed:
if not self.is_left:
self.x -= self.speed
self.is_left = not self.is_left
# Move
if self.is_left:
self.x -= self.speed
else:
self.x += self.speed
# Randomly move up/down
if randint(0,20) == 3:
if randint(0,1) == 1:
self.y -= 1
else:
self.y += 1
# Bounds checks for y
if self.y < 3:
self.y = 3
elif self.y > height-4:
self.y = height-4
# Print fish
self.display()
def init_rocks():
# Randomly generate rocks
rock_t = ["0", "o", "O", "&", "@"]
rocks = ""
for i in range(width-1):
rocks += choice(rock_t)
return rocks
def draw_rocks(rock, line):
# Draw rock line
stdscr.addstr(line, 0, rock, curses.color_pair(9))
def init_weeds():
weeds = [randint(10, width-10) for i in range(int(width/30))]
return weeds
def draw_weeds(weeds, i):
for weed in weeds:
draw_weed(weed, i)
def draw_weed(x, i):
try:
b = int(i/20)%2==1
for i in range(3, 8):
if b:
stdscr.addstr(height-i, x, "(( ", curses.color_pair(2))
else:
stdscr.addstr(height-i, x, " ))", curses.color_pair(2))
b = not b
except E:
pass
def main():
curses.noecho()
curses.curs_set(False)
curses.start_color()
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLUE)
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLUE)
curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_BLUE)
curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(6, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(7, curses.COLOR_WHITE, curses.COLOR_BLUE)
curses.init_pair(8, curses.COLOR_YELLOW, curses.COLOR_BLUE)
curses.init_pair(9, curses.COLOR_MAGENTA, curses.COLOR_BLUE)
stdscr.bkgd(' ', curses.color_pair(1))
fishes = [Fish() for i in range(int(width*height / 150))]
weeds = init_weeds()
rocks = [init_rocks() for i in range(3)]
i = 0
while True:
draw_weeds(weeds, i)
i+=1
# Draw rocks (pink at bottom)
draw_rocks(rocks[0], height-1)
draw_rocks(rocks[1], height-2)
draw_rocks(rocks[2], height-3)
# Draw all fish
for fish in fishes:
fish.act()
# Show changes
stdscr.refresh()
time.sleep(.12)
if __name__ == "__main__":
try:
main()
except (KeyboardInterrupt, Exception) as e:
curses.endwin()