Beispiel #1
0
def checkwin(board, passcount):
    c = [0, 0, 0]
    for y in range(8):
        for x in range(8):
            c[board[y * 8 + x]] += 1
    if c[0] == 0 or c[1] == 0 or c[2] == 0 or passcount >= 2:
        winner = 0
        if c[1] >= c[2]:
            winner |= 1
        if c[2] >= c[1]:
            winner |= 2
        while pew.keys():
            pew.tick(0.1)
        while not pew.keys():
            for y in range(8):
                for x in range(8):
                    p = lookup(board, x, y)
                    if p & winner:
                        r = random.getrandbits(3)
                        if y == 7 or lookup(board, x, y + 1) != p:
                            if r & 3 == 0:
                                p = 3
                        else:
                            if r & 6:
                                p = screen.pixel(x, y + 1)
                            elif r & 1:
                                p = 3
                    screen.pixel(x, y, p)
            show(screen)
            pew.tick(0.06)
        raise pew.GameOver()
Beispiel #2
0
def main():
    pew.init()
    screen = pew.Pix()

    while True:
        screen.box(0, 0, 0)
        board = Board(screen)
        board.draw()

        while not pew.keys():
            pew.tick(0.25)

        while True:
            pew.tick(0.25)

            keys = pew.keys()
            if keys & pew.K_UP:
                board.move_paddle(board.left_paddle, Direction.UP)
            if keys & pew.K_DOWN:
                board.move_paddle(board.left_paddle, Direction.DOWN)
            if keys & pew.K_O:
                board.move_paddle(board.right_paddle, Direction.UP)
            if keys & pew.K_X:
                board.move_paddle(board.right_paddle, Direction.DOWN)

            try:
                board.refresh()
            except BallOut:
                break
Beispiel #3
0
def draw_round(p, q):
    for i in count():
        pew.keys()
        p = rotate(p)
        check(p)

        if p == q:
            continue

        mids = list(get_intermediate_points(p, q))

        screen.box(color=0)

        screen.pixel(*p, color=3)
        screen.pixel(*q, color=3)

        for j, m in enumerate(mids):
            c = (j % 3) + 1
            screen.pixel(*m, color=c)

        pew.show(screen)
        pew.tick(1 / 32)

        # after a few iterations, randomly swap the points
        if i > 10 and not random.randint(0, 30):
            return q, p
Beispiel #4
0
def hold_keys():
    global hold

    try:
        keys = pew.keys()
    except pew.GameOver:
        keys = 0
    if keys:
        hold = 0
    while pew.keys() and time.monotonic() - hold < 0.25:
        return 0
    hold = time.monotonic()
    return keys
Beispiel #5
0
def keyevents():
    global keyhistory
    keys = pew.keys()
    events = keys & (~keyhistory | (keyhistory & (keyhistory >> 8) &
                                    (keyhistory >> 16) & (keyhistory >> 24)))
    keyhistory = ((keyhistory & 0x3FFFFF) << 8) | keys
    return events
Beispiel #6
0
 def act(self, level):
     keys = pew.keys()
     kx, ky = 0, 0
     if keys & pew.K_UP:
         ky = -1
     elif keys & pew.K_DOWN:
         ky = 1
     if keys & pew.K_LEFT:
         kx = -1
     elif keys & pew.K_RIGHT:
         kx = 1
     if keys & pew.K_O and self.cooldown == 0:
         level.mobs.append(Bullet(self.x, self.y, self.dx, self.dy))
         self.cooldown += 1
     else:
         self.cooldown = max(self.cooldown - 1, 0)
     if kx == self.dx and ky == self.dy and self.step == 0:
         tile = level.tile(self.x + kx, self.y + ky)
         if not tile.block:
             self.x += kx
             self.y += ky
             self.step = 2
     else:
         self.step = max(self.step - 1, 0)
         if kx or ky:
             self.dx = kx
             self.dy = ky
Beispiel #7
0
    def run(self):
        screen = pew.Pix()
        apple = Apple(self._snake)
        apple.paint(screen)

        while True:
            self.paint(screen)
            pew.show(screen)
            pew.tick(1 / self._game_speed)

            self._keyboard.update(pew.keys())
            x = (self._snake[-1][0] + self._x_delta) % 8
            y = (self._snake[-1][1] + self._y_delta) % 8

            if (x, y) in self._snake:
                return False

            if len(self._snake) >= self._win_length:
                return True

            self._snake.append((x, y))

            if x == apple.x_location and y == apple.y_location:
                apple.rub_out(screen)
                apple = Apple(self._snake)
                apple.paint(screen)
                self._game_speed += 0.2
            else:
                self.rub_out(screen, 0)
Beispiel #8
0
def play():

    sc = pew.Pix()
    keys = pew.keys()

    player_x = 1
    p = Player(x=player_x)
    o = Obstacle()

    score = 0
    player_collided = False

    # Game loop
    while not keys & pew.K_X and not player_collided:
        # Parse events
        p.parse_keys(keys)
        if o.get_pos() == player_x:
            if p.collides(o.get_collisions()):
                player_collided = True
        elif o.get_pos() == player_x - 1:
            score += 1

        p.fall()

        score_array = get_score_array(score)[::-1]
        for i in range(len(score_array)):
            sc.pixel(7 - i, 0, 3 * score_array[i])

        p.blit(sc)
        o.blit(sc)

        pew.show(sc)
        pew.tick(1 / 2)

        # Clear last player position
        p.blit(sc, clear=True)
        o.blit(sc, clear=True)
        o.move()
        p.clear_jump()
        keys = pew.keys()

    if p.collides(o.get_collisions()):
        # Display score
        print(f"Game ended with a score of {score}!!")
    else:
        print("Quit game.")
Beispiel #9
0
 def keys(self):
     keys = pew.keys()
     print('Pressed {}'.format(self.size))
     if keys & pew.K_O and self.size < 8:
         self.size += 1
         print('Pressed O')
     elif keys & pew.K_X and self.size > 1:
         self.size -= 1
         print('Pressed X')
Beispiel #10
0
 def keys(self):
     keys = pew.keys()
     if keys & pew.K_UP and self.cursory > 0:
         self.cursory -= 1
     elif keys & pew.K_DOWN and self.cursory < 7:
         self.cursory += 1
     if keys & pew.K_LEFT and self.cursorx > 0:
         self.cursorx -= 1
     elif keys & pew.K_RIGHT and self.cursorx < 7:
         self.cursorx += 1
     elif keys & pew.K_X:
         print('Pressed X')
     elif keys & pew.K_O:
         print('Pressed O')
Beispiel #11
0
def getUserInput():
    global up, down, left, right
    keys = pew.keys()
    if keys & pew.K_UP:
        up, down, left, right = 1, 0, 0, 0
    elif keys & pew.K_DOWN:
        up, down, left, right = 0, 1, 0, 0
    elif keys & pew.K_LEFT:
        up, down, left, right = 0, 0, 1, 0
    elif keys & pew.K_RIGHT:
        up, down, left, right = 0, 0, 0, 1
    elif keys & pew.K_X:
        X()
    elif keys & pew.K_O:
        H()
Beispiel #12
0
def move_player(data):
    x1player, y1player = data['player']['position'][0]
    x2player, y2player = data['player']['position'][1]

    keys = pew.keys()

    if keys & pew.K_LEFT and x1player > 0:
        x1player -= 1
        x2player -= 1
    elif keys & pew.K_RIGHT and x2player < 7:
        x1player += 1
        x2player += 1
    
    data['player']['position'] = [(x1player, y1player), (x2player, y2player)]
    return data
Beispiel #13
0
 def check_and_move_player(self):
     keys = pew.keys()
     self.debounce()
     x = self.player[0]
     dx = 0
     if keys & pew.K_UP:
         self.handle_quantum_tunnelling()
     else:
         if keys & pew.K_LEFT:
             dx -= 1
         elif keys & pew.K_RIGHT:
             dx += 1
         if x + dx > __SCREEN_MAX_X:
             dx = -7 if __WRAP_AROUND else 0
         if x + dx < __SCREEN_MIN_X:
             dx = 7 if __WRAP_AROUND else 0
         self.old_player = self.player[0], self.player[1]
         if dx:  # actual movement
             self.player = x + dx, self.player[1]
Beispiel #14
0
 def keys(self):
     keys = pew.keys()
     if not self.pressing:
         if keys & pew.K_UP and self.cursory > 0:
             self.cursory -= 1
         elif keys & pew.K_DOWN and self.cursory < 7:
             self.cursory += 1
         if keys & pew.K_LEFT and self.cursorx > 0:
             self.cursorx -= 1
         elif keys & pew.K_RIGHT and self.cursorx < 7:
             self.cursorx += 1
         elif keys & pew.K_X:
             self.press_confirm()
         elif keys & pew.K_O:
             self.press_decline()
         if keys:
             self.pressing = True
     else:
         if not keys:
             self.pressing = False
Beispiel #15
0
def show_text(t):
    if not t.strip():
        return
    scr = pew.Pix()
    rpt = False
    text = pew.Pix.from_text(t, color=3)
    while True:
        for dx in range(-8, text.width):
            scr.blit(text, -dx, 1)
            pew.show(scr)
            keys = pew.keys()
            if keys:
                if keys & pew.K_X:
                    return
                if keys & pew.K_O:
                    pew.tick(1 / 24)
                    rpt = True
            else:
                pew.tick(1 / 6)
        if not rpt:
            return
Beispiel #16
0
def loop():
    screen = pew.Pix()

    # Run until user input
    while not pew.keys():
        current_time = time.localtime()
        h = current_time[3] % 12
        m = current_time[4] // 5
        s = current_time[5] // 5

        # Paint pixels
        enable_hand(screen, h, 1)
        enable_hand(screen, m, 2)
        enable_hand(screen, s, 3)

        # Update display
        pew.show(screen)
        # Wait until next iteration
        pew.tick(1 / 2)

        # Clear pixels
        disable_hand(screen, h)
        disable_hand(screen, m)
        disable_hand(screen, s)
def main():

    # -- animations ----

    def blink(row):
        while len(animations) > 1:
            yield
        while True:
            yield
            for x, y in row:
                screen.pixel(x, y + 2, 0)
            yield

    def drop(color, x, y):
        for i in range(1, y):
            screen.pixel(x, y, 0)
            screen.pixel(x, i, color)
            yield

    # -- initialization ----

    pew.init()
    screen = pew.Pix()
    board = pew.Pix(7, 6)
    cursor = 3
    opcursor = 3
    turn = 1
    prevk = 0b111111
    won = False
    animations = []

    with open('four-name', 'rb') as f:
        myname = f.read()
    lobbyprefix = b'fourinarow/lobby/'
    lobbytopic = lobbyprefix + myname
    joinprefix = b'fourinarow/join/'
    jointopic = joinprefix + myname
    client = mqtt.MQTTClient('', 'mqtt.kolleegium.ch')
    client.set_last_will(lobbytopic, b'', True)
    client.connect()
    try:

        # -- lobby initialization ----

        client.publish(lobbytopic, b'1', True)
        joined = None
        lobby = set()
        lobbylist = ['>exit']
        menu = menugen(screen, lobbylist)

        def onMessageLobby(topic, message):
            nonlocal joined, mycolor
            if topic.startswith(lobbyprefix):
                username = topic[len(lobbyprefix):]
                if message:
                    lobby.add(username)
                    screen.box(1, 0, 7, 8, 1)
                else:
                    lobby.discard(username)
                    screen.box(2, 0, 7, 8, 1)
                lobbylist[:-1] = [
                    str(n, 'ascii') for n in lobby if n != myname
                ]
            elif topic == jointopic:
                joined = message
                mycolor = 2

        client.set_callback(onMessageLobby)
        client.subscribe(lobbyprefix + b'+')
        client.subscribe(jointopic)

        # -- lobby loop ----

        for selected in menu:
            client.check_msg()
            if joined:
                break
            pew.show(screen)
            pew.tick(1 / 24)
        else:
            if selected < len(lobbylist) - 1:
                joined = bytes(lobbylist[selected], 'ascii')
                client.publish(joinprefix + joined, myname)
                mycolor = 1
        client.publish(lobbytopic, b'', True)
        screen.box(0)
        pew.show(screen)

        if not joined:
            return

        # -- game initialization ----

        mygameprefix = b'fourinarow/game/' + myname + b'/'
        mycursortopic = mygameprefix + b'cursor'
        mydroptopic = mygameprefix + b'drop'
        opgameprefix = b'fourinarow/game/' + joined + b'/'
        opcursortopic = opgameprefix + b'cursor'
        opdroptopic = opgameprefix + b'drop'

        def move(cursor):
            nonlocal won, turn
            y = 0
            while y < 6 and board.pixel(cursor, y) == 0:
                y += 1
            if y != 0:
                board.pixel(cursor, y - 1, turn)
                animations.append(drop(turn, cursor, y + 1))
                won = check(board)
                if won:
                    animations.append(blink(won))
                turn = 3 - turn

        def onMessageGame(topic, message):
            nonlocal opcursor
            if topic == opcursortopic and len(message) == 1:
                opcursor = message[0]
            elif topic == opdroptopic and len(
                    message) == 1 and turn == 3 - mycolor:
                move(message[0])

        client.set_callback(onMessageGame)
        client.subscribe(opgameprefix + b'#')
        client.publish(mycursortopic, bytes((cursor, )), True)

        # -- game loop ----

        while True:

            # -- input handling ----

            k = pew.keys()
            if not won:
                if k & pew.K_LEFT:
                    if cursor > 0:
                        cursor -= 1
                        client.publish(mycursortopic, bytes((cursor, )), True)
                if k & pew.K_RIGHT:
                    if cursor < 6:
                        cursor += 1
                        client.publish(mycursortopic, bytes((cursor, )), True)
                if k & ~prevk & (pew.K_DOWN | pew.K_O
                                 | pew.K_X) and turn == mycolor:
                    move(cursor)
                    client.publish(mydroptopic, bytes((cursor, )), False)
            else:
                if prevk == 0 and k != 0 and len(animations) == 1:
                    return
            prevk = k

            client.check_msg()

            # -- drawing ----

            screen.box(0, 0, 0, 8, 2)
            if not won:
                screen.pixel(cursor, 0, mycolor)
                screen.pixel(opcursor, 0,
                             3 if cursor == opcursor else 3 - mycolor)
                if turn == mycolor:
                    screen.pixel(7, 1, turn)
            screen.blit(board, 0, 2)
            for i in range(len(animations) - 1, -1, -1):
                try:
                    next(animations[i])
                except StopIteration:
                    del animations[i]
            pew.show(screen)
            pew.tick(0.15)

    finally:
        client.publish(lobbytopic, b'', True)
        client.disconnect()
Beispiel #18
0
def debounce():
    for i in range(100):
        pew.tick(1 / 100)
        if not pew.keys():
            return
Beispiel #19
0
import struct
import usb_hid
import pew

pew.init()
screen = pew.Pix()
for gamepad in usb_hid.devices:
    if gamepad.usage_page == 0x01 and gamepad.usage == 0x05:
        break
else:
    raise RuntimeError("Gamepad HID device not found")
report = bytearray(6)

while True:
    buttons = pew.keys()
    report_buttons = 0

    if buttons & pew.K_O:
        screen.pixel(6, 3, 3)
        report_buttons |= 0x01
    else:
        screen.pixel(6, 3, 1)

    if buttons & pew.K_X:
        screen.pixel(6, 5, 3)
        report_buttons |= 0x02
    else:
        screen.pixel(6, 5, 1)

    if buttons & pew.K_UP:
        y = -127
import pew

from config.demo_config import configuration
from lib.game import GameController

##
#   @name  -
#   @type  main
##
pew.init()
screen = pew.Pix()

controller = GameController(configuration)

while True:
    controller.update(screen, pew.keys())
    pew.show(screen)
    pew.tick(1 / 12)

# end main
Beispiel #21
0
    brick_y = -3
    while True:
        if is_colliding(board, brick, brick_x, brick_y):
            if brick == BRICKS[7]:
                if qRand(1):
                    brick = SINGLE
                else:
                    brick = SINGLE
                    brick_x += 2
                if is_colliding(board, brick, brick_x, brick_y):
                    break
            else:
                break

        for turn in range(4):
            keys = pew.keys()
            if (keys & pew.K_LEFT
                    and not is_colliding(board, brick, brick_x - 1, brick_y)):
                brick_x -= 1
                debounce()
            elif (keys & pew.K_RIGHT
                  and not is_colliding(board, brick, brick_x + 1, brick_y)):
                brick_x += 1
                debounce()
            if (keys & pew.K_O and not (brick == BRICKS[7])):
                new_brick = pew.Pix.from_iter([[
                    brick.pixel(brick.width - y - 1, x)
                    for x in range(brick.height)
                ] for y in range(brick.width)])
                if not is_colliding(board, new_brick, brick_x, brick_y):
                    brick = new_brick
Beispiel #22
0
 def check_for_start(self):
     keys = pew.keys()
     return keys & pew.K_O or keys & pew.K_X
Beispiel #23
0
    (1, 1, 1, 1, 1, 1, 1, 1),
))

blink = 1
dead = False
for y in range(8):
    for x in range(8):
        if screen.pixel(x, y) == 4:
            break;
    else:
        continue
    break

while not dead:
    screen.pixel(x, y, VOID)
    pressed = pew.keys()
    dx = 0
    dy = 0
    if pressed & pew.K_UP:
        dy = -1
    elif pressed & pew.K_DOWN:
        dy = 1
    elif pressed & pew.K_LEFT:
        dx = -1
    elif pressed & pew.K_RIGHT:
        dx = 1
    if screen.pixel(x + dx, y + dy) not in {WALL, ROCK}:
        x += dx
        y += dy
    elif screen.pixel(x + dx, y + dy) == ROCK and dy == 0:
        if screen.pixel(x + dx + dx, y + dy + dy) == VOID:
Beispiel #24
0
        u2 *= u2
        for y in range(8):
            for x in range(8):
                rx = (x << 4) - cx1
                ry = (y << 4) - cy1
                rr1 = rx * rx + ry * ry
                rx = (x << 4) - cx2
                ry = (y << 4) - cy2
                rr2 = rx * rx + ry * ry
                screen.pixel(
                    x, y,
                    blend[((3 if rr1 < l1 else
                            2 if rr1 < m1 else 1 if rr1 < u1 else 0) << 2) |
                          (3 if rr2 < l2 else
                           2 if rr2 < m2 else 1 if rr2 < u2 else 0)])
        r1 += dr1
        dr1 -= 1
        cx1 += dx1
        cy1 += dy1
        r2 += dr2
        dr2 -= 1
        cx2 += dx2
        cy2 += dy2
        pew.show(screen)
        pew.tick(0.05)


while pew.keys():
    pew.tick(0.1)
main()
Beispiel #25
0
def main():
    screen = pew.Pix()
    r1 = -1
    r2 = -1
    stop = False
    stopped = 0
    blend = bytearray(16)
    t1r = t1g = t2r = t2g = 0
    while stopped != 3:
        stop = stop or pew.keys()
        if r1 < 10:
            if stop:
                stopped |= 1
                cx1 = 500
                r1 = 10
                dr1 = 20
            else:
                cx1 = random.getrandbits(7)
                cy1 = random.getrandbits(7)
                dx1 = random.getrandbits(4) - 8
                dy1 = random.getrandbits(4) - 11
                r1 = 10
                dr1 = random.getrandbits(4) + 5
                t1r = random.getrandbits(8) % 23
                t1g = 7
                if t1r >= 16:
                    t1g = 22 - t1r
                    t1r = 15
                for a1 in range(4):
                    for a2 in range(4):
                        blend[(a1 << 2) | a2] = 0x80 | ((
                            (3 * a1 * t1r +
                             (3 - a1) * a2 * t2r + 4) // 9) << 3) | (
                                 (3 * a1 * t1g + (3 - a1) * a2 * t2g + 4) // 9)
        if r2 < 10:
            if stop:
                stopped |= 2
                cx2 = 500
                r2 = 10
                dr2 = 20
            else:
                cx2 = random.getrandbits(7)
                cy2 = random.getrandbits(7)
                dx2 = random.getrandbits(4) - 8
                dy2 = random.getrandbits(4) - 11
                r2 = 10
                dr2 = random.getrandbits(4) + 5
                t2r = random.getrandbits(8) % 23
                t2g = 7
                if t2r >= 16:
                    t2g = 22 - t2r
                    t2r = 15
                for a1 in range(4):
                    for a2 in range(4):
                        blend[(a1 << 2) | a2] = 0x80 | ((
                            (3 * a1 * t1r +
                             (3 - a1) * a2 * t2r + 4) // 9) << 3) | (
                                 (3 * a1 * t1g + (3 - a1) * a2 * t2g + 4) // 9)
        r = r1 >> 1
        l1 = r - 5
        l1 *= l1
        m1 = r * r
        u1 = r + 5
        u1 *= u1
        r = r2 >> 1
        l2 = r - 5
        l2 *= l2
        m2 = r * r
        u2 = r + 5
        u2 *= u2
        for y in range(8):
            for x in range(8):
                rx = (x << 4) - cx1
                ry = (y << 4) - cy1
                rr1 = rx * rx + ry * ry
                rx = (x << 4) - cx2
                ry = (y << 4) - cy2
                rr2 = rx * rx + ry * ry
                screen.pixel(
                    x, y,
                    blend[((3 if rr1 < l1 else
                            2 if rr1 < m1 else 1 if rr1 < u1 else 0) << 2) |
                          (3 if rr2 < l2 else
                           2 if rr2 < m2 else 1 if rr2 < u2 else 0)])
        r1 += dr1
        dr1 -= 1
        cx1 += dx1
        cy1 += dy1
        r2 += dr2
        dr2 -= 1
        cx2 += dx2
        cy2 += dy2
        pew.show(screen)
        pew.tick(0.05)
Beispiel #26
0
def main_loop(ins):
    """
    main loop of the program. Takes an instruction
    set and calls it iteratively to process the
    pushed buttons and update the display.
    Button 'O' exits the loop
    """
    # initialize PewPew console
    pew.init()

    # Load start screens
    for start_screen in start_screens:
        pew.show(pew.Pix.from_iter(start_screen))
        pew.tick(0.2)
    pew.show(pew.Pix.from_iter(blank_screen))
    pew.tick(0.5)

    # initialization stage
    pew.show(ins.get_current_screen())

    # flags used throughout the loop
    bool_loop = True
    old_keys = 0

    while bool_loop:
        keys = pew.keys()

        if keys != 0 and keys != old_keys:
            old_keys = keys

            # dispatch the pushed buttons
            if keys & pew.K_X:
                value = pew.K_X
            elif keys & pew.K_DOWN:
                value = pew.K_DOWN
            elif keys & pew.K_LEFT:
                value = pew.K_LEFT
            elif keys & pew.K_RIGHT:
                value = pew.K_RIGHT
            elif keys & pew.K_UP:
                value = pew.K_UP
            elif keys & pew.K_O:
                value = pew.K_O
                bool_loop = False
            else:
                value = 0

            ins.key_pressed(value)

        elif keys == 0:
            # this is necessary to be able to push
            # a button twice in a row
            old_keys = keys

        # update the screen and wait for 20ms
        pew.show(ins.get_current_screen())
        pew.tick(0.02)

    # the program has been terminated.
    # display the final sequence
    for final_screen in final_screens:
        pew.show(pew.Pix.from_iter(final_screen))
        pew.tick(0.2)
    pew.show(pew.Pix.from_iter(blank_screen))
    pew.tick(0.2)
Beispiel #27
0
def debounce(rng=50):
    for _ in range(rng):
        pew.tick(1 / 100)
        if not pew.keys():
            return
Beispiel #28
0
def run():

    cos = [
        4, 4, 3, 3, 2, 1, 0, -1, -2, -3, -3, -4, -4, -4, -3, -3, -2, -1, 0, 1,
        2, 3, 3, 4
    ]
    sin = cos[18:] + cos[:18]

    with open('m3dlevel.bmp', 'rb') as f:
        f.seek(54)
        textures = f.read(64)
        textures = bytes(textures[4 * i] for i in range(16))
        level = f.read()

    def bresenham(ax, ay, bx, by):
        x = ax
        y = ay
        dx = bx - ax
        if dx < 0:
            dx = -dx
            ix = -1
        else:
            ix = 1
        dy = by - ay
        if dy < 0:
            dy = -dy
            iy = -1
        else:
            iy = 1
        dx <<= 1
        dy <<= 1
        sx = dx
        sy = dy
        if dx >= dy:
            sx >>= 1
            while x != bx:
                if sx < sy:
                    y += iy
                    sx += dx
                x += ix
                sy += dy
                yield x, y
        else:
            sy >>= 1
            while y != by:
                if sy < sx:
                    x += ix
                    sy += dy
                y += iy
                sx += dx
                yield x, y

    def lookup(x, y):
        b = level[(y << (_LOG_LEVEL_W - 1 - 2)) & (((1 << _LOG_LEVEL_H) - 1) <<
                                                   (_LOG_LEVEL_W - 1)) |
                  ((x >> 3) & ((1 << (_LOG_LEVEL_W - 1)) - 1))]
        return (b & 0xF) if (x & 4) else (b >> 4)

    pew.init()
    screen = pew.Pix()

    x = (1 << (_LOG_LEVEL_W + 1))
    y = (1 << (_LOG_LEVEL_H + 1))
    nx = None
    b = 6
    sb = sin[b]
    cb = cos[b]

    while pew.keys():
        pew.tick(0.1)
    while True:
        keys = pew.keys()
        if keys & pew.K_X:
            break
        if keys & pew.K_RIGHT:
            if keys & pew.K_O:
                nx = x + sb
                ny = y - cb
            else:
                b = (b + 23) % 24
                sb = sin[b]
                cb = cos[b]
                #print('xy:', x, y, 'b:', b)
        if keys & pew.K_LEFT:
            if keys & pew.K_O:
                nx = x - sb
                ny = y + cb
            else:
                b = (b + 1) % 24
                sb = sin[b]
                cb = cos[b]
                #print('xy:', x, y, 'b:', b)
        if keys & pew.K_UP:
            nx = x + cb
            ny = y + sb
        if keys & pew.K_DOWN:
            nx = x - cb
            ny = y - sb
        if nx is not None and lookup(nx, ny) == 0:
            x = nx
            y = ny
            nx = None
            #print('xy:', x, y, 'b:', b)

        rx = x + 16 * cb - 14 * sb
        ry = y + 16 * sb + 14 * cb
        for c in range(8):
            p = 0
            for bx, by in bresenham(x, y, rx, ry):
                p = lookup(bx, by)
                if p != 0:
                    bxy = max(abs(bx - x), abs(by - y))
                    rxy = max(abs(rx - x), abs(ry - y))
                    for r in range(8):
                        t = (7 * rxy - 8 * bxy * (5 - 2 * r)) // (4 * rxy) - 1
                        screen.pixel(
                            c, r, 0 if t < 0 else 73 - 8 * r if t >= 4 else
                            ((textures[p] >>
                              (t << 1)) & 3) + 4 * (bxy * 15 // rxy))
                    #if keys != 0:
                    #	print(rx, ',', ry, ':', bx, ',', by, '=', p, 'z', 64*bxy/rxy)
                    break
            else:
                for r in range(8):
                    screen.pixel(c, r, 0 if r < 3 else 73 - 8 * r)

            rx += 4 * sb
            ry -= 4 * cb

        pew.show(screen)
        pew.tick(0.06)
Beispiel #29
0
                      (3, 0, 0, 0, 0, 2, 0, 3), (0, 3, 3, 3, 3, 3, 3, 0)),
                     ((0, 3, 3, 3, 3, 3, 3, 0), (3, 0, 0, 1, 2, 0, 0, 3),
                      (3, 0, 1, 0, 2, 1, 0, 3), (3, 1, 0, 2, 0, 0, 1, 3),
                      (3, 3, 0, 2, 0, 0, 3, 3), (3, 0, 3, 3, 3, 3, 0, 3),
                      (3, 0, 2, 0, 0, 0, 0, 3), (0, 3, 3, 3, 3, 3, 3, 0)),
                     ((0, 3, 3, 3, 3, 3, 3, 0), (3, 0, 0, 1, 1, 0, 2, 3),
                      (3, 0, 1, 0, 0, 2, 0, 3), (3, 1, 0, 0, 2, 0, 1, 3),
                      (3, 3, 0, 2, 0, 0, 3, 3), (3, 0, 3, 3, 3, 3, 0, 3),
                      (3, 2, 0, 0, 0, 0, 0, 3), (0, 3, 3, 3, 3, 3, 3, 0)))
    i = 0  # frame counter
    value = 0  # holds the selected level (0 = none)
    pew.init()  # initialize the PewPew console

    while not value:
        # check for pressed keys
        keys = pew.keys()
        if keys & pew.K_UP:
            value = 1
        elif keys & pew.K_RIGHT:
            value = 2
        elif keys & pew.K_DOWN:
            value = 3
        elif keys & pew.K_LEFT:
            value = 4

        # display the next frame of the animation
        animation = (0, 0, 1, 1, 2, 2, 3, 3, 2, 2, 1, 1)
        i = (i + 1) % len(animation)
        screen = qiskit_images[animation[i]]
        pew.show(pew.Pix.from_iter(screen))
Beispiel #30
0
def inputs(pad):
    keys = pew.keys()
    if keys & pew.K_LEFT and pad['X'] > 0:
        pad['X'] -= 1
    elif keys & pew.K_RIGHT and pad['X'] < 8 - pad['WIDTH']:
        pad['X'] += 1