Advertisement
Slazaa

engine.py

Oct 17th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. HP_HUD_STRING_WIDTH = 6
  2.  
  3. class Renderer:
  4.  
  5.     def clear(self):
  6.         self.area.clear()
  7.  
  8.         for i in range(0, self.height):
  9.             self.area.append([])
  10.             for j in range(0, self.width):
  11.                 self.area[i].append(" ")
  12.  
  13.     def __init__(self, width, height):
  14.         self.width = width
  15.         self.height = height
  16.         self.area = []
  17.  
  18.         self.clear()
  19.  
  20.     def present(self):
  21.         for i in range(0, self.height):
  22.             for j in range(0, self.width):
  23.                 print(self.area[i][j], end="")
  24.             print()
  25.  
  26.     def draw_rect(self, rect):
  27.         for i in range(0, rect.height):
  28.             for j in range(0, rect.width):
  29.                 if (rect.rel_y + i >= 0 and rect.rel_y + i < self.height) and (rect.rel_x + j >= 0 and rect.rel_x + j < self.width):
  30.                     self.area[rect.rel_y + i][rect.rel_x + j] = rect.char
  31.  
  32.     def draw_sprite(self, sprite):
  33.         for i in range(0, sprite.height):
  34.             for j in range(0, sprite.width):
  35.                 if (sprite.rel_y + i >= 0 and sprite.rel_y + i < self.height) and (sprite.rel_x + j >= 0 and sprite.rel_x + j < self.width):
  36.                     self.area[sprite.rel_y + i][sprite.rel_x + j] = sprite.area[i][j]
  37.  
  38.  
  39. class Rect:
  40.  
  41.     def __init__(self, camera, x, y, width, height, char):
  42.         self.x = x
  43.         self.y = y
  44.         self.rel_x = x + camera.x
  45.         self.rel_y = y + camera.y
  46.         self.width = width
  47.         self.height = height
  48.         self.char = char
  49.  
  50.     def update(self, camera):
  51.         self.rel_x = self.x + camera.x
  52.         self.rel_y = self.y + camera.y
  53.  
  54.     def move(self, x, y):
  55.         self.x += x
  56.         self.y += y
  57.  
  58.  
  59. class Sprite(Rect):
  60.  
  61.     def __init__(self, camera, x, y, width, height, char):
  62.         Rect.__init__(self, camera, x, y, width, height, char)
  63.         self.area = []
  64.  
  65.         for i in range(0, self.height):
  66.             self.area.append([])
  67.             for j in range(0, self.width):
  68.                 self.area[i].append(self.char)
  69.  
  70.     def set_from_file(self, path):
  71.         with open(path, "r") as sprite_file:
  72.             content = sprite_file.read()
  73.  
  74.             self.height = 0
  75.             self.width = 0
  76.  
  77.             for i in range(0, len(content)):
  78.                 if content[i] == "\n":
  79.                     self.height += 1
  80.  
  81.             for i in range(0, len(content)):
  82.                 if content[i] == "\n":
  83.                     break
  84.                 self.width += 1
  85.  
  86.             self.area.clear()
  87.  
  88.             for i in range(0, self.height):
  89.                 self.area.append([])
  90.                 for j in range(0, self.width):
  91.                     self.area[i].append(" ")
  92.                     self.area[i][j] = content[(self.width + 1) * i + j]
  93.  
  94. class Entity(Sprite):
  95.  
  96.     def __init__(self, camera, x, y, health, char):
  97.         Sprite.__init__(self, camera, x, y, 1, 1, char)
  98.         self.health = health
  99.  
  100.  
  101. class Player(Entity):
  102.  
  103.     def __init__(self, camera, x, y):
  104.         Entity.__init__(self, camera, x, y, 100, "@")
  105.         self.level = 1
  106.  
  107.  
  108. class Camera:
  109.  
  110.     def __init__(self, x, y):
  111.         self.x = x
  112.         self.y = y
  113.  
  114.     def move(self, x, y):
  115.         self.x += x
  116.         self.y += y
  117.  
  118.  
  119. class Hud:
  120.  
  121.     def __init__(self, camera, player, width, height):
  122.         self.hp_hud = Sprite(camera, 0 - camera.x, 1 - camera.y, HP_HUD_STRING_WIDTH + 2, 3, " ")
  123.  
  124.     def update(self, player):
  125.         hp_hud_string = f"HP:{player.health}"
  126.  
  127.         if len(hp_hud_string) < HP_HUD_STRING_WIDTH:
  128.             hp_hud_string.append(" ")
  129.  
  130.         for i in range(0, HP_HUD_STRING_WIDTH):
  131.             self.hp_hud.area[1][i + 1] = hp_hud_string[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement