tighogg

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

commit
b0b7991fd6c0e752b4f8776552f44f9dc4c3457e
parent
92f2bcced1643a827c06729b88801ff00b35ce26
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-05-08 10:46
larger stickfigures and walk animation

Diffstat

M tighogg.py 62 +++++++++++++++++++++++++++++++++++++++++++------------------

1 files changed, 44 insertions, 18 deletions


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

@@ -15,9 +15,12 @@ class Player:
   15    15     def __init__(self, x, y, direction, color):
   16    16         self.x = x
   17    17         self.y = y
   -1    18         self.base_direction = direction
   18    19         self.direction = direction
   19    20         self.color = color
   20    21         self.running = False
   -1    22         self.cycle_duration = 3
   -1    23         self.cycle_frame = 0
   21    24         self.cooldown = -1
   22    25         self.weapon = '-'
   23    26 
@@ -27,26 +30,46 @@ class Player:
   27    30                 self.x += 1
   28    31             else:
   29    32                 self.x -= 1
   -1    33         self.cooldown -= 1
   -1    34         self.cycle_frame = (self.cycle_frame + 1) % (self.cycle_duration * 4)
   -1    35 
   -1    36     def die(self):
   -1    37         self.cooldown = 30
   -1    38         self.direction = self.base_direction
   -1    39         self.running = False
   30    40 
   31    41     def _render(self):
   32    42         if self.running:
   33    -1             if self.direction == LEFT:
   34    -1                 yield r'o  '
   35    -1                 yield r'w\ '
   36    -1             else:
   37    -1                 yield r'  o'
   38    -1                 yield r' /w'
   39    -1             yield r' @ '
   -1    43             if self.cycle_frame // self.cycle_duration == 0:
   -1    44                 yield r'    O /'
   -1    45                 yield r'   /\/ '
   -1    46                 yield r' _/\   '
   -1    47                 yield r'    \  '
   -1    48             elif self.cycle_frame // self.cycle_duration == 1:
   -1    49                 yield r'    O /'
   -1    50                 yield r'   /\/ '
   -1    51                 yield r'  _\   '
   -1    52                 yield r'   |   '
   -1    53             elif self.cycle_frame // self.cycle_duration == 2:
   -1    54                 yield r'    O /'
   -1    55                 yield r'   /\/ '
   -1    56                 yield r'   \   '
   -1    57                 yield r"  /'   "
   -1    58             elif self.cycle_frame // self.cycle_duration == 3:
   -1    59                 yield r'    O /'
   -1    60                 yield r'   /\/ '
   -1    61                 yield r'  /\   '
   -1    62                 yield r" /  '  "
   40    63         else:
   41    -1             yield r' o '
   42    -1             if self.direction == LEFT:
   43    -1                 yield r'w| '
   44    -1             else:
   45    -1                 yield r' |w'
   46    -1             yield r' Λ '
   -1    64             yield r' \_O  /'
   -1    65             yield r'   |\/ '
   -1    66             yield r'   |\  '
   -1    67             yield r'  / |  '
   47    68 
   48    69     def render(self, camera):
   49    70         for i, line in enumerate(self._render()):
   -1    71             if self.direction == LEFT:
   -1    72                 line = line[::-1].replace('/', '1').replace('\\', '/').replace('1', '\\')
   50    73             x = round(self.x - camera) - 1 + len(line) - len(line.lstrip())
   51    74             y = self.y - 2 + i
   52    75             if x < 0:
@@ -81,10 +104,7 @@ class Game:
   81   104 
   82   105     @property
   83   106     def direction(self):
   84    -1         if self.leader == self.player1:
   85    -1             return RIGHT
   86    -1         else:
   87    -1             return LEFT
   -1   107         return self.leader.base_direction
   88   108 
   89   109     def render_hud(self):
   90   110         x = round(self.position * self.cols)
@@ -147,19 +167,25 @@ class Game:
  147   167             while self.running:
  148   168                 last = time.time()
  149   169                 self.on_key(boon.getch())
   -1   170 
  150   171                 for player in self.players:
  151   172                     player.step()
  152   173                     player.cooldown -= 1
   -1   174 
   -1   175                 # die on out-of-screen
  153   176                 if (
  154   177                     self.straggler.cooldown < 0
  155   178                     and abs(self.straggler.x - self.leader.x) > self.cols
  156   179                 ):
  157    -1                     self.straggler.cooldown = 10
   -1   180                     self.straggler.die()
   -1   181 
   -1   182                 # respawn on edge of screen
  158   183                 if self.straggler.cooldown == 0:
  159   184                     if self.direction == RIGHT:
  160   185                         self.straggler.x = self.leader.x + self.cols / 2
  161   186                     else:
  162   187                         self.straggler.x = self.leader.x - self.cols / 2
   -1   188 
  163   189                 self.render()
  164   190                 time.sleep(1 / 30 - (time.time() - last))
  165   191