tighogg

fighting game for the terminal
git clone https://git.ce9e.org/tighogg.git

commit
92f2bcced1643a827c06729b88801ff00b35ce26
parent
ad4b5d27536781a500bd4d6498f3015a669b8a88
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-05-08 09:26
die and respawn on out-of-view

Diffstat

M tighogg.py 47 +++++++++++++++++++++++++++++++++++++++--------

1 files changed, 39 insertions, 8 deletions


diff --git a/tighogg.py b/tighogg.py

@@ -18,6 +18,7 @@ class Player:
   18    18         self.direction = direction
   19    19         self.color = color
   20    20         self.running = False
   -1    21         self.cooldown = -1
   21    22         self.weapon = '-'
   22    23 
   23    24     def step(self):
@@ -48,6 +49,8 @@ class Player:
   48    49         for i, line in enumerate(self._render()):
   49    50             x = round(self.x - camera) - 1 + len(line) - len(line.lstrip())
   50    51             y = self.y - 2 + i
   -1    52             if x < 0:
   -1    53                 continue
   51    54             boon.move(y, x)
   52    55             sys.stdout.write(
   53    56                 boon.get_cap('setaf', self.color)
@@ -69,15 +72,26 @@ class Game:
   69    72         return self.players[-1]
   70    73 
   71    74     @property
   -1    75     def straggler(self):
   -1    76         return self.players[0]
   -1    77 
   -1    78     @property
   72    79     def position(self):
   73    80         return self.leader.x / LEVEL_WIDTH
   74    81 
   75    -1     def render_hud(self, cols):
   76    -1         x = round(self.position * cols)
   -1    82     @property
   -1    83     def direction(self):
   77    84         if self.leader == self.player1:
   78    -1             bar = '=' * x + '>' + ' ' * (cols - x - 1)
   -1    85             return RIGHT
   79    86         else:
   80    -1             bar = ' ' * x + '<' + '=' * (cols - x - 1)
   -1    87             return LEFT
   -1    88 
   -1    89     def render_hud(self):
   -1    90         x = round(self.position * self.cols)
   -1    91         if self.direction == RIGHT:
   -1    92             bar = '=' * x + '>' + ' ' * (self.cols - x - 1)
   -1    93         else:
   -1    94             bar = ' ' * x + '<' + '=' * (self.cols - x - 1)
   81    95         boon.move(0, 0)
   82    96         sys.stdout.write(
   83    97             boon.get_cap('setaf', self.leader.color)
@@ -86,14 +100,19 @@ class Game:
   86   100         )
   87   101 
   88   102     def render(self):
   89    -1         cols, rows = shutil.get_terminal_size()
   -1   103         self.cols, self.rows = shutil.get_terminal_size()
   90   104         sys.stdout.write(boon.get_cap('clear'))
   91    -1         camera = self.leader.x - cols / 2
   92   105 
   93    -1         self.render_hud(cols)
   -1   106         if self.straggler.cooldown > 1:
   -1   107             camera = self.leader.x - self.cols / 2
   -1   108         else:
   -1   109             camera = (self.leader.x + self.straggler.x) / 2 - self.cols / 2
   -1   110 
   -1   111         self.render_hud()
   94   112 
   95   113         for player in self.players:
   96    -1             player.render(camera)
   -1   114             if player.cooldown < 0:
   -1   115                 player.render(camera)
   97   116 
   98   117         sys.stdout.flush()
   99   118 
@@ -124,11 +143,23 @@ class Game:
  124   143     def run(self):
  125   144         self.running = True
  126   145         with boon.fullscreen():
   -1   146             self.render()
  127   147             while self.running:
  128   148                 last = time.time()
  129   149                 self.on_key(boon.getch())
  130   150                 for player in self.players:
  131   151                     player.step()
   -1   152                     player.cooldown -= 1
   -1   153                 if (
   -1   154                     self.straggler.cooldown < 0
   -1   155                     and abs(self.straggler.x - self.leader.x) > self.cols
   -1   156                 ):
   -1   157                     self.straggler.cooldown = 10
   -1   158                 if self.straggler.cooldown == 0:
   -1   159                     if self.direction == RIGHT:
   -1   160                         self.straggler.x = self.leader.x + self.cols / 2
   -1   161                     else:
   -1   162                         self.straggler.x = self.leader.x - self.cols / 2
  132   163                 self.render()
  133   164                 time.sleep(1 / 30 - (time.time() - last))
  134   165