tighogg

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

commit
f60fd46ced1a48b512aad4ad45a70c7a79e3b0b2
parent
b0b7991fd6c0e752b4f8776552f44f9dc4c3457e
Author
Tobias Bengfort <tobias.bengfort@posteo.de>
Date
2021-05-09 07:01
basic jump

Diffstat

M tighogg.py 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------

1 files changed, 58 insertions, 6 deletions


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

@@ -1,6 +1,7 @@
    1     1 import sys
    2     2 import shutil
    3     3 import time
   -1     4 import math
    4     5 
    5     6 import boon
    6     7 
@@ -8,13 +9,39 @@ LEFT = 0
    8     9 RIGHT = 1
    9    10 YELLOW = 10
   10    11 GREEN = 11
   11    -1 LEVEL_WIDTH = 500
   -1    12 
   -1    13 
   -1    14 class Map:
   -1    15     def __init__(self, size=1000, s='------__ _ _-- ____---- --'):
   -1    16         self.size = size
   -1    17         self.s = s
   -1    18 
   -1    19     def get_floor(self, x):
   -1    20         k = int(abs((x / self.size * 2 - 1) * len(self.s)))
   -1    21         try:
   -1    22             if self.s[k] == '-':
   -1    23                 return 12
   -1    24             elif self.s[k] == '_':
   -1    25                 return 17
   -1    26         except IndexError:
   -1    27             pass
   -1    28         return math.inf
   -1    29 
   -1    30     def render(self, camera, cols, rows):
   -1    31         for x in range(cols):
   -1    32             y = self.get_floor(camera + x)
   -1    33             if y is math.inf:
   -1    34                 continue
   -1    35             boon.move(y + 1, x)
   -1    36             sys.stdout.write('#')
   12    37 
   13    38 
   14    39 class Player:
   15    -1     def __init__(self, x, y, direction, color):
   -1    40     def __init__(self, game, x, y, direction, color):
   -1    41         self.game = game
   16    42         self.x = x
   17    43         self.y = y
   -1    44         self.dy = 0
   18    45         self.base_direction = direction
   19    46         self.direction = direction
   20    47         self.color = color
@@ -24,12 +51,27 @@ class Player:
   24    51         self.cooldown = -1
   25    52         self.weapon = '-'
   26    53 
   -1    54     @property
   -1    55     def floor(self):
   -1    56         return min(
   -1    57             self.game.map.get_floor(self.x - 2),
   -1    58             self.game.map.get_floor(self.x + 2),
   -1    59         )
   -1    60 
   27    61     def step(self):
   28    62         if self.running:
   29    63             if self.direction == RIGHT:
   30    64                 self.x += 1
   31    65             else:
   32    66                 self.x -= 1
   -1    67 
   -1    68         if self.floor > self.y:
   -1    69             self.dy += 0.1
   -1    70         self.y += self.dy
   -1    71         if self.floor < self.y:
   -1    72             self.dy = 0
   -1    73             self.y = self.floor
   -1    74 
   33    75         self.cooldown -= 1
   34    76         self.cycle_frame = (self.cycle_frame + 1) % (self.cycle_duration * 4)
   35    77 
@@ -38,6 +80,10 @@ class Player:
   38    80         self.direction = self.base_direction
   39    81         self.running = False
   40    82 
   -1    83     def jump(self):
   -1    84         if self.floor == self.y:
   -1    85             self.dy = -1
   -1    86 
   41    87     def _render(self):
   42    88         if self.running:
   43    89             if self.cycle_frame // self.cycle_duration == 0:
@@ -71,7 +117,7 @@ class Player:
   71   117             if self.direction == LEFT:
   72   118                 line = line[::-1].replace('/', '1').replace('\\', '/').replace('1', '\\')
   73   119             x = round(self.x - camera) - 1 + len(line) - len(line.lstrip())
   74    -1             y = self.y - 2 + i
   -1   120             y = round(self.y - 3 + i)
   75   121             if x < 0:
   76   122                 continue
   77   123             boon.move(y, x)
@@ -85,8 +131,9 @@ class Player:
   85   131 
   86   132 class Game:
   87   133     def __init__(self):
   88    -1         self.player1 = Player(LEVEL_WIDTH // 2 - 10, 10, RIGHT, YELLOW)
   89    -1         self.player2 = Player(LEVEL_WIDTH // 2 + 10, 10, LEFT, GREEN)
   -1   134         self.map = Map()
   -1   135         self.player1 = Player(self, self.map.size // 2 - 10, 10, RIGHT, YELLOW)
   -1   136         self.player2 = Player(self, self.map.size // 2 + 10, 10, LEFT, GREEN)
   90   137         self.players = [self.player1, self.player2]
   91   138         self.running = True
   92   139 
@@ -100,7 +147,7 @@ class Game:
  100   147 
  101   148     @property
  102   149     def position(self):
  103    -1         return self.leader.x / LEVEL_WIDTH
   -1   150         return self.leader.x / self.map.size
  104   151 
  105   152     @property
  106   153     def direction(self):
@@ -128,6 +175,7 @@ class Game:
  128   175         else:
  129   176             camera = (self.leader.x + self.straggler.x) / 2 - self.cols / 2
  130   177 
   -1   178         self.map.render(camera, self.cols, self.rows)
  131   179         self.render_hud()
  132   180 
  133   181         for player in self.players:
@@ -149,6 +197,8 @@ class Game:
  149   197             self.player1.direction = RIGHT
  150   198         elif key == boon.KEY_DOWN:
  151   199             self.player1.running = False
   -1   200         elif key == boon.KEY_UP:
   -1   201             self.player1.jump()
  152   202 
  153   203         # player2
  154   204         elif key == 'a':
@@ -159,6 +209,8 @@ class Game:
  159   209             self.player2.direction = RIGHT
  160   210         elif key == 's':
  161   211             self.player2.running = False
   -1   212         elif key == 'w':
   -1   213             self.player2.jump()
  162   214 
  163   215     def run(self):
  164   216         self.running = True