Example #1
0
def main():
    pygame.init()

    display = pygame.display.set_mode((800, 600), 0, 32)
    engine = PyGameEngine(display)

    pygame.display.set_caption(engine.Name)

    count = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.locals.QUIT:
                pygame.quit()
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=True)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=True)
            if event.type == pygame.KEYUP:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=False)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=False)
                if event.key == pygame.locals.K_r:
                    engine.restart()
                    count = 0

        if count == 40 and not engine.is_game_over:
            engine.tick()
            count = -1
        count += 1

        pygame.display.update()
Example #2
0
def main():
    pygame.init()

    display = pygame.display.set_mode((720, 576), 0, 32)
    engine = PyGameEngine(display)

    count = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.locals.QUIT:
                pygame.quit()
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=True)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=True)
                if event.key == pygame.locals.K_SPACE:
                    engine.fire()

            if event.type == pygame.KEYUP:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=False)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=False)

        if count == 20 and not engine.is_game_over:
            try:
                engine.tick()
            except GameOver:
                print('Game over')
            count = -1
        count += 1

        pygame.display.update()
Example #3
0
def text(where, *args):
    """Displays a text frame.

    Where can be either a point or an entity.
    TODO: update Things while the textbox is visible
    """
    portrait, text, side = None, '', ''

    if len(args) == 1:
        text = args[0]
    elif len(args) == 2:
        portrait, text = args
    elif len(args) == 3:
        portrait, side, text = args
    else:
        assert False, 'text recieves 1 or two arguments.'

    textBox = TextBox(where, portrait, side, text)

    engine.things.append(textBox)

    try:
        engine.beginCutScene()
        while not (controls.attack1() or controls.joy_attack1()
                   or controls.ui_accept()):
            engine.tick()
            engine.draw()

    finally:
        engine.endCutScene()
        engine.things.remove(textBox)
Example #4
0
def main():
    arguments = docopt(__doc__)
    filename = arguments['<filename>']
    empty = arguments['--empty'] is not None
    rand = arguments['--rand'] is not None
    dimensions = None
    if empty:
        dimensions = arguments['--empty']
    if rand:
        dimensions = arguments['--rand']
    if dimensions is not None:
        dimensions = [int(x) for x in dimensions.split('x')]
        if rand:
            cells = engine.randomized_cells(dimensions)
        else:
            cells = Cells(dimensions=dimensions)
        with open(filename, 'w') as f:
            f.write(str_from_cells(cells))
        return
    
    with open(filename, 'r') as f:
        cells = cells_from_str(f.read())
    engine.tick(cells)
    with open(filename, 'w') as f:
        f.write(str_from_cells(cells))
Example #5
0
def main():
    pygame.init()

    display = pygame.display.set_mode((800, 600), 0, 32)
    engine = PyGameEngine(display)

    pygame.display.set_caption(engine.Name)

    count = 0
    while True:
        for event in pygame.event.get():
            if event.type == pygame.locals.QUIT:
                pygame.quit()
                return
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=True)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=True)
            if event.type == pygame.KEYUP:
                if event.key == pygame.locals.K_LEFT:
                    engine.direction(left=True, pressed=False)
                if event.key == pygame.locals.K_RIGHT:
                    engine.direction(right=True, pressed=False)
                if event.key == pygame.locals.K_r:
                    engine.restart()
                    count = 0

        if count == 40 and not engine.is_game_over:
            engine.tick()
            count = -1
        count += 1

        pygame.display.update()
Example #6
0
def run():
    while True:
        data = sys.stdin.readline().strip()
        if data:
            args = json.loads(data)
            engine.tick()
            with engine_lock:
                func = getattr(midi_engine, {0x80: 'note_off', 0x90: 'note_on', 0xB0: 'damper'}[args[0]], None)
                if func:
                    func(*args[1:])
                else:
                    print "Unhandled MIDI event", args
            if args[0] == 0xB0 and args[1] == 0x42 and args[2] == 0:
                renderer.events.append('switch_viz')
Example #7
0
    def __activate(self):
        p = engine.player
        p.state = p.defaultState()
        engine.tick()

        try:
            self.activate()
        finally:
            # Bump the player away, so he does not instantly activate the Npc again.
            #p = engine.player
            #d = dir.invert[p.direction]
            #deltaX, deltaY = dir.delta[d]
            #p.x += deltaX
            #p.y += deltaY

            engine.synchTime()
Example #8
0
 def request_update(self):
     engine.tick()
     midi_engine.update()
     (cx, cy) = midi_engine.center
     scale = 1.2 / (math.hypot(cx, cy) + 1)
     (self.cx, self.cy) = (scale * cx, scale * cy)
     for note in midi_engine.notes.itervalues():
         note.render_decay = min(note.weight, 1.0)
     elapsed = engine.now - self.last_update
     self.last_update = engine.now
     midi_engine.notes_need_update = False
     # now find second weightiest note
     note_weights = sorted(n.render_decay * n.volume for n in midi_engine.notes.itervalues())
     top_2nd_note_weight = (note_weights[-2:] + [0])[0]
     self.top_2nd_note_weight = max(self.top_2nd_note_weight * math.exp(-elapsed/5.0),
                                    top_2nd_note_weight, 0.3)
Example #9
0
def main():
    scale = 4

    x, y = 100, 100
    cells = engine.randomized_cells((x,y))
    white, black = (255, 255, 255), (0, 0, 0)

    def process_living(cells):
        for (x1, y1) in cells:
            for x_add, y_add in itertools.permutations(xrange(4), 2):
                p(screen, x1 * scale + x_add, y1 * scale + y_add, white)

    pygame.init()
    screen = pygame.display.set_mode((x * scale, y * scale))

    # game loop
    while True:
        pygame.display.flip()
        pygame.display.get_surface().fill(black)
        process_living(cells)
        engine.tick(cells)
Example #10
0
    def test_three_neighbors(self):
        start = """
+----+
|**  |
|**  |
|    |
|    |
+----+
"""
        finish = """
+----+
|**  |
|**  |
|    |
|    |
+----+
"""
        cells = cells_from_str(start)
        engine.tick(cells)
        finish_cells = cells_from_str(finish)
        self.assertEqual(cells, finish_cells)
Example #11
0
    def test_bottomright(self):
        start = """
+----+
|    |
|    |
|   *|
|  **|
+----+
"""
        finish = """
+----+
|    |
|    |
|  **|
|  **|
+----+
"""
        cells = cells_from_str(start)
        engine.tick(cells)
        finish_cells = cells_from_str(finish)
        self.assertEqual(cells, finish_cells)
Example #12
0
 def on_draw(self):
     #callback needs self
     window.clear()
     process_living(cells)
     engine.tick(cells)
Example #13
0
def textMenu(where, *args, **kwargs):
    """Displays a text frame with list of options.

    """

    options = kwargs.get('options', [])
    menu = Menu(textctrl=ScrollableTextFrame())
    menu.addText(*options)
    menu.autoSize()

    frame = gui.Frame()

    portrait, text, side = None, '', ''

    if len(args) == 1:
        text = args[0]
    elif len(args) == 2:
        portrait, text = args
    elif len(args) == 3:
        portrait, side, text = args
    else:
        assert False, 'text recieves 1 or two arguments.'

    textBox = TextBox(where, portrait, side, text)

    menu.width += 16  #hack!
    #menu.dockBottom(textBox.frame)
    menu.y = textBox.frame.y
    if where == 'right':
        menu.dockRight(textBox.frame)
        #menu.dockTop(textBox.frame)
        #menu.x = textBox.frame.x - menu.width - 20

        menu.x -= 20  #hack hack!
    else:
        menu.dockLeft(textBox.frame)
        menu.x += 20  #hack hack!

    #menu.y-=24 #hack hack haaaaack!!

    engine.things.append(textBox)
    engine.things.append(menu)

    result = None
    try:
        engine.beginCutScene()
        while True:  #not (controls.attack1() or controls.joy_attack1() or controls.ui_accept()):
            engine.tick()
            engine.draw()
            menu.draw()
            result = menu.update()
            if result is None:
                continue
            else:

                break

    finally:
        engine.endCutScene()
        engine.things.remove(textBox)
        engine.things.remove(menu)
        return result