Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- HP_HUD_STRING_WIDTH = 6
- class Renderer:
- def clear(self):
- self.area.clear()
- for i in range(0, self.height):
- self.area.append([])
- for j in range(0, self.width):
- self.area[i].append(" ")
- def __init__(self, width, height):
- self.width = width
- self.height = height
- self.area = []
- self.clear()
- def present(self):
- for i in range(0, self.height):
- for j in range(0, self.width):
- print(self.area[i][j], end="")
- print()
- def draw_rect(self, rect):
- for i in range(0, rect.height):
- for j in range(0, rect.width):
- 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):
- self.area[rect.rel_y + i][rect.rel_x + j] = rect.char
- def draw_sprite(self, sprite):
- for i in range(0, sprite.height):
- for j in range(0, sprite.width):
- 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):
- self.area[sprite.rel_y + i][sprite.rel_x + j] = sprite.area[i][j]
- class Rect:
- def __init__(self, camera, x, y, width, height, char):
- self.x = x
- self.y = y
- self.rel_x = x + camera.x
- self.rel_y = y + camera.y
- self.width = width
- self.height = height
- self.char = char
- def update(self, camera):
- self.rel_x = self.x + camera.x
- self.rel_y = self.y + camera.y
- def move(self, x, y):
- self.x += x
- self.y += y
- class Sprite(Rect):
- def __init__(self, camera, x, y, width, height, char):
- Rect.__init__(self, camera, x, y, width, height, char)
- self.area = []
- for i in range(0, self.height):
- self.area.append([])
- for j in range(0, self.width):
- self.area[i].append(self.char)
- def set_from_file(self, path):
- with open(path, "r") as sprite_file:
- content = sprite_file.read()
- self.height = 0
- self.width = 0
- for i in range(0, len(content)):
- if content[i] == "\n":
- self.height += 1
- for i in range(0, len(content)):
- if content[i] == "\n":
- break
- self.width += 1
- self.area.clear()
- for i in range(0, self.height):
- self.area.append([])
- for j in range(0, self.width):
- self.area[i].append(" ")
- self.area[i][j] = content[(self.width + 1) * i + j]
- class Entity(Sprite):
- def __init__(self, camera, x, y, health, char):
- Sprite.__init__(self, camera, x, y, 1, 1, char)
- self.health = health
- class Player(Entity):
- def __init__(self, camera, x, y):
- Entity.__init__(self, camera, x, y, 100, "@")
- self.level = 1
- class Camera:
- def __init__(self, x, y):
- self.x = x
- self.y = y
- def move(self, x, y):
- self.x += x
- self.y += y
- class Hud:
- def __init__(self, camera, player, width, height):
- self.hp_hud = Sprite(camera, 0 - camera.x, 1 - camera.y, HP_HUD_STRING_WIDTH + 2, 3, " ")
- def update(self, player):
- hp_hud_string = f"HP:{player.health}"
- if len(hp_hud_string) < HP_HUD_STRING_WIDTH:
- hp_hud_string.append(" ")
- for i in range(0, HP_HUD_STRING_WIDTH):
- self.hp_hud.area[1][i + 1] = hp_hud_string[i]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement