Example #1
0
 def __init__(self, symbol, x, y, parent_name, print_priority, color):
     self.symbol = symbol
     self.rel_pos = v.Vector(x, y)
     self.empty = self.is_empty()
     self.parent_name = parent_name
     self.abs_pos = v.Vector(0, 0)
     self.print_priority = print_priority
     self.color = color
Example #2
0
 def __init__(self, text, name, max_width, color=curses.COLOR_WHITE):
     Unit.__init__(self, '', name, color=color, text=True)
     self.raw = text
     self.max_width = max_width
     self.centered = text.center(max_width)
     self.body = self.build_unit([self.centered])
     self.width = v.Vector(len(self.centered), 0)
     self.height = v.Vector(0, 1)
Example #3
0
 def __init__(self,
              text,
              name,
              max_ch,
              focus=False,
              color=curses.COLOR_WHITE):
     Option.__init__(self, text, name, len(text), focus, color)
     self.textbox = None
     self.max_ch = max_ch
     self.bl = v.Vector(0, 0)
     self.br = v.Vector(0, 0)
Example #4
0
    def __init__(self, stdscr):
        curses.curs_set(0)
        curses.start_color()
        y, x = stdscr.getmaxyx()

        self.field = stdscr
        self.height = v.Vector(0, y - 1)
        self.width = v.Vector(x, 0)
        self.main_unit = None
        self.units = []
        self.inputs = []
        self.bg_units = []
        self.floor_level = v.Vector(0, 0)
        self.bitmap = b.Bitmap(self.width.x, self.height.y - 1)
        self.palette = Palette()
        self.keyevent = k.KeyEventLogger()
Example #5
0
    def __init__(self,
                 path,
                 name,
                 collidable=True,
                 print_priority=999,
                 color=curses.A_NORMAL):
        Unit.__init__(self, path, name, collidable, print_priority, color)
        self.def_velocity = v.Vector(0, 0)
        self.collided = False
        self.squated = False
        self.squated_unit = Unit('./units/invadersquat',
                                 'invader',
                                 collidable=True,
                                 print_priority=999,
                                 color=curses.COLOR_RED)
        self.normal_unit = Unit(self.path,
                                'invader',
                                collidable=True,
                                print_priority=999,
                                color=curses.COLOR_RED)
        self.jump = []

        self.action_map = {
            '32': self.init_jump,
            '258': self.fast_landing,
            '-1': self.do_nothing
        }
        self.release_actions = {'258': self.standup, '-1': self.do_nothing}
Example #6
0
    def make_jump_arr(self):
        if self.is_jumping():
            return
        result = []
        for i in range(3):
            for x in range(2):
                result.append(v.Vector(0, -2))
        for s in range(4):
            result.append(v.Vector(0, -1))
        for s in range(4):
            result.append(v.Vector(0, 1))
        for i in range(3):
            for x in range(2):
                result.append(v.Vector(0, 2))

        self.jump = result
Example #7
0
 def fast_landing(self):
     if self.is_jumping():
         last = self.jump[-1]
         if last.y < 0:
             self.jump = list(map(lambda x: x - v.Vector(0, 3), self.jump))
     else:
         if not self.squated:
             self.body = self.squated_unit.body
             self.squated = True
Example #8
0
 def __init__(self,
              path,
              name,
              collidable=True,
              print_priority=0,
              color=curses.COLOR_WHITE,
              xpos=500,
              ypos=0):
     Unit.__init__(self, path, name, collidable, print_priority, color)
     self.pos = v.Vector(xpos, ypos)
Example #9
0
 def __init__(self,
              path,
              name,
              collidable=True,
              print_priority=0,
              color=curses.COLOR_WHITE,
              text=False):
     self.print_priority = print_priority
     self.name = name
     self.collidable = collidable
     self.path = path
     self.color = color
     self.pos = v.Vector(0, 0)
     if not text:
         f = open(path).readlines()
         f = [line[:-1] for line in f]
         self.raw = ''.join(f)
         self.body = self.build_unit(f)
         self.width = v.Vector(max([len(x) for x in f]), 0)
         self.height = v.Vector(0, len(f))
Example #10
0
class Game:

    FLOOR_LVL = 5
    velocity = v.Vector(-3, 0)

    def __init__(self, stage):

        self.options = {}
        self.inputs = {}

        self.stage = stage
        self.game_over = False
        self.main_unit = None
        self.generator = None
        self.menu = None
        self.gameover_menu = None
        self.connection = c.Connection('./data/data.db')
        last = self.connection.get_last()
        if last:
            self.player = Player(last[1], self.stage)
        else:
            self.newuser()
        self.mainmenu()

    def newuser(self):
        self.stage.inputs = []
        self.stage.units = []
        self.options = {}
        self.inputs = {'Name:': self.validate_name}
        self.menu = m.InputMenu(self.stage, self.inputs, self.options,
                                './units/logo')
        selected = False
        while not selected:
            key = self.menu.draw_options(int(self.stage.width.length() / 2),
                                         15, self.menu.logo)
            selected = self.menu.action_map.get(str(key.pressed),
                                                self.menu.next)()
        inp = self.inputs[self.menu.inputs[self.menu.selected_index].raw]
        inp(self.menu.inputs[self.menu.selected_index].value)

    def validate_name(self, name):
        if not self.connection.insert_value(name, 0):
            self.newuser()
        else:
            last = self.connection.get_last()
            logging.debug(last)
            self.player = Player(last[1], self.stage)
            self.mainmenu()

    def mainmenu(self):
        self.stage.inputs = []
        self.stage.units = []
        self.options = {
            'New Game': self.startup,
            'Rankings': self.quit,
            'New User': self.newuser,
            'Quit  Game': self.quit
        }
        self.menu = m.Menu(self.stage, self.options.keys(), './units/logo')
        selected = False
        while not selected:
            key = self.menu.draw_options(int(self.stage.width.length() / 2),
                                         15, self.menu.logo)
            selected = self.menu.action_map.get(str(key.pressed),
                                                self.menu.do_nothing)()
        self.options[self.menu.options[self.menu.selected_index].raw]()

    def gameover(self):
        sleep(2)
        self.stage.units = []
        # self.stage.field.clear()
        # self.stage.bitmap.clear_bitmap()
        self.options = {
            'Restart': self.startup,
            'Main Menu': self.mainmenu,
            'Quit Game': self.quit
        }
        self.gameover_menu = m.Menu(self.stage, self.options.keys(),
                                    './units/gameover')
        selected = False
        while not selected:
            key = self.gameover_menu.draw_options(
                int(self.stage.width.length() / 2), 15,
                self.gameover_menu.logo)
            selected = self.gameover_menu.action_map.get(
                str(key.pressed), self.gameover_menu.do_nothing)()
        self.options[self.gameover_menu.options[
            self.gameover_menu.selected_index].raw]()

    def startup(self):
        self.game_over = False
        self.stage.units = []
        self.stage.field.clear()
        self.main_unit = u.MainUnit('./units/invader',
                                    'invader',
                                    color=curses.COLOR_RED)
        self.player = Player(self.player.name, self.stage)
        self.stage.build_floor(self.FLOOR_LVL)
        self.stage.add_unit(self.main_unit,
                            20,
                            self.stage.floor_level.y,
                            main=True)
        self.generator = Generator(self.stage, self.main_unit.width.length(),
                                   self.velocity)
        self.stage.bitmap.print_units(self.stage.units, self.main_unit)
        # send enter key to start moving the unit
        curses.ungetch(10)
        self.stage.draw_units2()

    @staticmethod
    def quit():
        exit(0)

    def check_collisions(self):
        self.stage.bitmap.print_units(self.stage.units, self.main_unit)
        if self.main_unit.collided:
            self.game_over = True

    def make_step(self):
        self.generator.generate()

        for unit in self.stage.units:
            if unit.name != 'floor':
                self.stage.mv_unit(unit, self.velocity.x, self.velocity.y)

        self.main_unit.update_velocity()
        self.stage.mv_unit(self.main_unit, self.main_unit.def_velocity.x,
                           self.main_unit.def_velocity.y)

        # terminate jump if unit landed
        if self.main_unit.pos.y == self.stage.floor_level.y + 3:
            self.main_unit.clear_jump_arr()

        self.check_collisions()

        if self.player is not None:
            logging.debug(self.player.name)
            self.player.update_score(abs(self.velocity.x))
        key = self.stage.draw_units2()

        self.check_border_units()

        self.main_unit.action_map.get(str(key.pressed),
                                      self.main_unit.do_nothing)()
        self.main_unit.release_actions.get(str(key.released),
                                           self.main_unit.do_nothing)()

    def check_border_units(self):
        for unit in self.stage.units:
            if unit.name != 'floor' and unit.pos.x <= abs(self.velocity.x):
                self.stage.units.remove(unit)
Example #11
0
 def build_floor(self, floor_level):
     self.floor_level = v.Vector(0, floor_level)
     for pixel in range(self.width.length()):
         unit = u.Unit('./units/block', 'floor', False, 0)
         self.add_unit(unit, pixel, floor_level)
Example #12
0
 def __init__(self, win, ux, uy, bx, by):
     self.textbox = Textbox(win)
     self.ul = v.Vector(ux, uy)
     self.br = v.Vector(bx, by)
     self.value = ''