<commit_before>#! /usr/bin/env python

import sys
import time
import curses
from collections import namedtuple


from render import Renderer
from weapon import Weapon
from utils import Point, Event


KEY = "KEY"
K_A = ord("a")
K_D = ord("d")
K_SPACE = ord(" ")
K_ESCAPE = 27
#fix freezes [issue#1]
KEYS = [K_A, K_D, K_SPACE, K_ESCAPE]


class Spaceship(object):
    def __init__(self, border):
        self.__image = "<i>"
        self.__dx = 1
        self.__border = border
        self.__pos = Point(self.__border.x // 2 - len(self.__image), self.__border.y - 1)
        self.__fire = False
        self.__weapon = Weapon()("Blaster")
        self.__hull = 100
        self.__shield = 100
        
        self.h_bar = Bar(self.__hull, 100, title="Hull")
        self.s_bar = Bar(self.__shield, 100, title="Shield")

        #self.__weapon_bay = WeaponBay()
        #self.__weapon_bay.add_weapon(wp)
        #self.__weapon_bay.remove_veapon(wp)


    def move_left(self):
        self.__dx = -1


    def move_right(self):
        self.__dx = 1

    def toggle_fire(self):
        self.__fire = not self.__fire


    def update(self):
        if self.__pos.x == self.__border.x - len(self.__image) - 1 and self.__dx > 0:
            self.__pos.x = 0
        elif self.__pos.x == 1 and self.__dx < 0:
            self.__pos.x = self.__border.x - len(self.__image)

        self.__pos.x += self.__dx
        self.__dx = 0

        self.__weapon.update()
        if self.__fire:
            self.__weapon.make_shot(Point(x=self.__pos.x + 1, y=self.__pos.y))

        #!!!!
        self.h_bar.update_value(self.__hull)
        self.s_bar.update_value(self.__shield)

    @property
    def image(self):
        return self.__image

    @property
    def pos(self):
        return self.__pos


    def get_weapon_info(self):
        return "Weapon: {w} | [{c}/{m}".format(w=self.__weapon.type, c=self.__weapon.ammo, m=self.__weapon.max_ammo)

    def get_health_info(self):
        return (self.__hull, self.__shield)



LOW = 3
MEDIUM = 4
HIGH = 5
BLANK = 6

class Bar(object):
    def __init__(self, value, max_value, title=None):
        self.__title = title + ": " if title else None
        self.__value = value
        self.__max_value = max_value
        self.__elems = 0 if self.__value <= 0 else round(self.__value * 10 / self.__max_value)
        self.__start = "["
        self.__end = "]"
        self.__elem = " "
        self.__style = curses.A_BOLD

        curses.init_pair(LOW, curses.COLOR_WHITE, curses.COLOR_RED)
        curses.init_pair(MEDIUM, curses.COLOR_WHITE, curses.COLOR_YELLOW)
        curses.init_pair(HIGH, curses.COLOR_WHITE, curses.COLOR_GREEN)
        curses.init_pair(BLANK, curses.COLOR_BLACK, curses.COLOR_BLACK)


    def update_value(self, val):
        self.__value = 0 if val < 0 else val



    def render(self, screen, pos):
        if self.__title:
            screen.addstr(pos.y, pos.x, self.__title, self.__style)
            pos.x += len(self.__title)

        screen.addstr(pos.y, pos.x, self.__start, self.__style)
        for i in range(1, self.__elems + 1):
            pos.x += 1
            if i in (1, 2, 3): #RED
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(LOW) | self.__style)
            elif i in (4,5,6):
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(MEDIUM) | self.__style)
            else:
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(HIGH) | self.__style)
        pos.x += 1
        screen.addstr(pos.y, pos.x, self.__end, self.__style)

        return Point(x=pos.x, y=pos.y)


class App(object):
    def __init__(self):
        curses.initscr()
        curses.start_color()
        curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK)
        curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_RED)

        self.border = Point(x=80, y=24)
        self.field  = Point(x=self.border.x, y=self.border.y-1)
        self.screen = curses.newwin(self.border.y, self.border.x, 0, 0)
        self.screen.keypad(1)
        self.screen.nodelay(1)
        curses.noecho()
        curses.cbreak()
        curses.curs_set(0)
        self.renderer = Renderer()
        self.spaceship = Spaceship(self.field)
        self.renderer.add_object(self.spaceship)



        self._objects = []
        self._objects.append(self.spaceship)

    def deinit(self):
        self.screen.nodelay(0)
        self.screen.keypad(0)
        curses.nocbreak()
        curses.echo()
        curses.curs_set(1)
        curses.endwin()


    def events(self):
        c = self.screen.getch()
        if c == K_ESCAPE:
            self.deinit()
            sys.exit(1)
        elif c == K_A:
            self.spaceship.move_left()
        elif c == K_D:
            self.spaceship.move_right()
        elif c == K_SPACE:
            self.spaceship.toggle_fire()


    def update(self):
        self.spaceship.update()


    def render(self):
        self.screen.erase()<commit_msg>move color pairs and window creating into App's method create_window<commit_after>#! /usr/bin/env python

import sys
import time
import curses
from collections import namedtuple


from render import Renderer
from weapon import Weapon
from utils import Point, Event


KEY = "KEY"
K_A = ord("a")
K_D = ord("d")
K_SPACE = ord(" ")
K_ESCAPE = 27
#fix freezes [issue#1]
KEYS = [K_A, K_D, K_SPACE, K_ESCAPE]


class Spaceship(object):
    def __init__(self, border):
        self.__image = "<i>"
        self.__dx = 1
        self.__border = border
        self.__pos = Point(self.__border.x // 2 - len(self.__image), self.__border.y - 1)
        self.__fire = False
        self.__weapon = Weapon()("Blaster")
        self.__hull = 100
        self.__shield = 100
        
        self.h_bar = Bar(self.__hull, 100, title="Hull")
        self.s_bar = Bar(self.__shield, 100, title="Shield")

        #self.__weapon_bay = WeaponBay()
        #self.__weapon_bay.add_weapon(wp)
        #self.__weapon_bay.remove_veapon(wp)


    def move_left(self):
        self.__dx = -1


    def move_right(self):
        self.__dx = 1

    def toggle_fire(self):
        self.__fire = not self.__fire


    def update(self):
        if self.__pos.x == self.__border.x - len(self.__image) - 1 and self.__dx > 0:
            self.__pos.x = 0
        elif self.__pos.x == 1 and self.__dx < 0:
            self.__pos.x = self.__border.x - len(self.__image)

        self.__pos.x += self.__dx
        self.__dx = 0

        self.__weapon.update()
        if self.__fire:
            self.__weapon.make_shot(Point(x=self.__pos.x + 1, y=self.__pos.y))

        #!!!!
        self.h_bar.update_value(self.__hull)
        self.s_bar.update_value(self.__shield)

    @property
    def image(self):
        return self.__image

    @property
    def pos(self):
        return self.__pos


    def get_weapon_info(self):
        return "Weapon: {w} | [{c}/{m}".format(w=self.__weapon.type, c=self.__weapon.ammo, m=self.__weapon.max_ammo)

    def get_health_info(self):
        return (self.__hull, self.__shield)



LOW = 3
MEDIUM = 4
HIGH = 5
BLANK = 6

class Bar(object):
    def __init__(self, value, max_value, title=None):
        self.__title = title + ": " if title else None
        self.__value = value
        self.__max_value = max_value
        self.__elems = 0 if self.__value <= 0 else round(self.__value * 10 / self.__max_value)
        self.__start = "["
        self.__end = "]"
        self.__elem = " "
        self.__style = curses.A_BOLD



    def update_value(self, val):
        self.__value = 0 if val < 0 else val



    def render(self, screen, pos):
        if self.__title:
            screen.addstr(pos.y, pos.x, self.__title, self.__style)
            pos.x += len(self.__title)

        screen.addstr(pos.y, pos.x, self.__start, self.__style)
        for i in range(1, self.__elems + 1):
            pos.x += 1
            if i in (1, 2, 3): #RED
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(LOW) | self.__style)
            elif i in (4,5,6):
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(MEDIUM) | self.__style)
            else:
                screen.addstr(pos.y, pos.x, self.__elem, curses.color_pair(HIGH) | self.__style)
        pos.x += 1
        screen.addstr(pos.y, pos.x, self.__end, self.__style)

        return Point(x=pos.x, y=pos.y)


class App(object):
    def __init__(self):

        self.border = Point(x=80, y=24)
        self.field  = Point(x=self.border.x, y=self.border.y-1)
        self.screen = self.create_window(x=self.border.x, y=self.border.y)

        self.renderer = Renderer()

        self.spaceship = Spaceship(self.field)
        self.renderer.add_object(self.spaceship)


    def create_window(self, x, y, a=0, b=0):
        curses.initscr()
        curses.start_color()
        curses.init_pair(1, curses.COLOR_YELLOW, curses.COLOR_BLACK)
        curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_RED)
        curses.init_pair(LOW, curses.COLOR_WHITE, curses.COLOR_RED)
        curses.init_pair(MEDIUM, curses.COLOR_WHITE, curses.COLOR_YELLOW)
        curses.init_pair(HIGH, curses.COLOR_WHITE, curses.COLOR_GREEN)
        curses.init_pair(BLANK, curses.COLOR_BLACK, curses.COLOR_BLACK)
        screen = curses.newwin(y, x, 0, 0)
        screen.keypad(1)
        screen.nodelay(1)
        curses.noecho()
        curses.cbreak()
        curses.curs_set(0)
        return screen


    def deinit(self):
        self.screen.nodelay(0)
        self.screen.keypad(0)
        curses.nocbreak()
        curses.echo()
        curses.curs_set(1)
        curses.endwin()


    def events(self):
        c = self.screen.getch()
        if c == K_ESCAPE:
            self.deinit()
            sys.exit(1)
        elif c == K_A:
            self.spaceship.move_left()
        elif c == K_D:
            self.spaceship.move_right()
        elif c == K_SPACE:
            self.spaceship.toggle_fire()


    def update(self):
        self.spaceship.update()


    def render(self):
        self.screen.erase()
{'fraction_non_alphanumeric': 0.12668250197941408, 'fraction_numerical': 0.012668250197941409, 'mean_word_length': 2.1275147013308575, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3640686', 'n_tokens_mistral': 3654, 'n_tokens_neox': 3469, 'n_words': 699}