tighogg

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

commit
ad4b5d27536781a500bd4d6498f3015a669b8a88
parent
ca2d4125a650c486acb1985fd2e845525e476a68
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-05-08 09:10
camera

Diffstat

M tighogg.py 26 ++++++++++++++------------

1 files changed, 14 insertions, 12 deletions


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

@@ -8,6 +8,7 @@ LEFT = 0
    8     8 RIGHT = 1
    9     9 YELLOW = 10
   10    10 GREEN = 11
   -1    11 LEVEL_WIDTH = 500
   11    12 
   12    13 
   13    14 class Player:
@@ -43,9 +44,9 @@ class Player:
   43    44                 yield r' |w'
   44    45             yield r' Λ '
   45    46 
   46    -1     def render(self):
   -1    47     def render(self, camera):
   47    48         for i, line in enumerate(self._render()):
   48    -1             x = self.x - 1 + len(line) - len(line.lstrip())
   -1    49             x = round(self.x - camera) - 1 + len(line) - len(line.lstrip())
   49    50             y = self.y - 2 + i
   50    51             boon.move(y, x)
   51    52             sys.stdout.write(
@@ -58,8 +59,8 @@ class Player:
   58    59 
   59    60 class Game:
   60    61     def __init__(self):
   61    -1         self.player1 = Player(10, 10, RIGHT, YELLOW)
   62    -1         self.player2 = Player(20, 10, LEFT, GREEN)
   -1    62         self.player1 = Player(LEVEL_WIDTH // 2 - 10, 10, RIGHT, YELLOW)
   -1    63         self.player2 = Player(LEVEL_WIDTH // 2 + 10, 10, LEFT, GREEN)
   63    64         self.players = [self.player1, self.player2]
   64    65         self.running = True
   65    66 
@@ -69,14 +70,14 @@ class Game:
   69    70 
   70    71     @property
   71    72     def position(self):
   72    -1         return self.leader.x / self.cols
   -1    73         return self.leader.x / LEVEL_WIDTH
   73    74 
   74    -1     def render_hud(self):
   75    -1         x = round(self.position * self.cols)
   -1    75     def render_hud(self, cols):
   -1    76         x = round(self.position * cols)
   76    77         if self.leader == self.player1:
   77    -1             bar = ' ' * x + '<' + '=' * (self.cols - x - 1)
   -1    78             bar = '=' * x + '>' + ' ' * (cols - x - 1)
   78    79         else:
   79    -1             bar = '=' * x + '>' + ' ' * (self.cols - x - 1)
   -1    80             bar = ' ' * x + '<' + '=' * (cols - x - 1)
   80    81         boon.move(0, 0)
   81    82         sys.stdout.write(
   82    83             boon.get_cap('setaf', self.leader.color)
@@ -85,13 +86,14 @@ class Game:
   85    86         )
   86    87 
   87    88     def render(self):
   88    -1         self.cols, self.rows = shutil.get_terminal_size()
   -1    89         cols, rows = shutil.get_terminal_size()
   89    90         sys.stdout.write(boon.get_cap('clear'))
   -1    91         camera = self.leader.x - cols / 2
   90    92 
   91    -1         self.render_hud()
   -1    93         self.render_hud(cols)
   92    94 
   93    95         for player in self.players:
   94    -1             player.render()
   -1    96             player.render(camera)
   95    97 
   96    98         sys.stdout.flush()
   97    99