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
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
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()
def print_text(self, raw_text): text = pew.Pix.from_text(raw_text) print(text) for dx in range(-8, text.width): self.screen.blit(text, -dx, 1) pew.show(self.screen) pew.tick(1 / 12)
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)
def print(string): _screen = pew.Pix() _text = pew.Pix.from_text(string, 3, 0) for dx in range(-8, _text.width): _screen.blit(_text, -dx, 1) pew.show(_screen) pew.tick(1 / 8)
def game(): data = init_data() while not has_won_the_game(data): data = update_game(data) show_scene(data) speed = data['player']['speed'] pew.tick(1 / speed)
def end_game(winner=False): scene = new_scene() message = 'You Win' if winner else 'Game Over' text = pew.Pix.from_text(message) for dx in range(-8, text.width): scene.blit(text, -dx, 1) pew.show(scene) pew.tick(1 / 12)
def main_loop(self): while True: self.keys() self.print_board() self.iterate_board() self.delayed_random_click() pew.show(self.screen) pew.tick(1 / 3)
def main_loop(self): while True: self.screen.pixel(0, 0, 0) self.screen.pixel(1, 1, 1) self.screen.pixel(2, 2, 2) self.screen.pixel(3, 3, 3) self.screen.box(2, 5, 0, 2, 2) pew.show(self.screen) pew.tick(1 / 3)
def gameOver(): for i in range(8): for j in range(8): screen.pixel(i, j, 0) message = pew.Pix.from_text("Gameover!") for i in range(-8, message.width): screen.blit(message, -i, 1) pew.show(screen) pew.tick(1 / 10) initGame()
def hint(lt): if lt: b = MORSE.get(lt, 0) if b: scr = pew.Pix() scr.blit(pew.Pix.from_text(lt, color=3), 2, 0) for j, v in enumerate(to_beep(b)): if v > 1: v = 3 scr.pixel(STARTPOS + j, 7, v) pew.show(scr) pew.tick(1)
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.")
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
def run_game(self): game_started = False # Title while True: for dx in range(-8, self.title.width): self.screen.blit(self.title, -dx, 1) pew.show(self.screen) pew.tick(1 / 12) game_started = self.check_for_start() if game_started: break if game_started: break # Game started self.clear_screen_for_start() try: while True: # Poll keys self.screen.pixel(*self.player, color=__ERASING_COLOR) self.check_and_move_player() self.screen.pixel(*self.player, color=__PLAYER_COLOR) ##################################### self.remove_fallen_raindrops() self.update_raindrops() self.generate_new_raindrops() self.check_for_player_collision() # Update screen elements here while len(self.to_erase): self.screen.pixel(*self.to_erase.pop(0), color=__ERASING_COLOR) while len(self.to_draw): self.screen.pixel(*self.to_draw.pop(0), color=__DRAWING_COLOR) pew.show(self.screen) pew.tick(1 / self.game_speed) self.game_speed *= self.speed_factor except pew.GameOver: # Game over screen self.clear_screen_for_start() for dx in range(-8, self.game_over.width): self.screen.blit(self.game_over, -dx, 1) pew.show(self.screen) pew.tick(1 / 17) score = pew.Pix.from_text("Score: " + str(self.raindrops_evaded)) for dx in range(-8, score.width): self.screen.blit(score, -dx, 1) pew.show(self.screen) pew.tick(1 / 13) self.reset_game_logic() self.debounce() # for any other button presses
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()
def debounce(): for i in range(100): pew.tick(1 / 100) if not pew.keys(): return
if buttons & pew.K_UP: y = -127 screen.pixel(2, 3, 3) screen.pixel(2, 5, 1) elif buttons & pew.K_DOWN: y = 127 screen.pixel(2, 3, 1) screen.pixel(2, 5, 3) else: y = 0 screen.pixel(2, 3, 1) screen.pixel(2, 5, 1) if buttons & pew.K_LEFT: x = -127 screen.pixel(1, 4, 3) screen.pixel(3, 4, 1) elif buttons & pew.K_RIGHT: x = 127 screen.pixel(1, 4, 1) screen.pixel(3, 4, 3) else: x = 0 screen.pixel(1, 4, 1) screen.pixel(3, 4, 1) struct.pack_into('<Hbbbb', report, 0, report_buttons, x, y, 0, 0) gamepad.send_report(report) pew.show(screen) pew.tick(1 / 12)
up, down, left, right = 0, 0, 0, 1 elif keys & pew.K_X: X() elif keys & pew.K_O: H() # --- MAIN --- initGame() while 1: # print head of snek x, y = snek[-1] screen.pixel(x, y, 3) pew.show(screen) pew.tick(1 / speed) # draw food screen.pixel(foodX, foodY, foodB) # get user input getUserInput() # update coords x = (x - left + right) % 8 y = (y - up + down) % 8 # 0, 0 is in the top left corner # game over if (x, y) in snek: gameOver() continue
brick = new_brick debounce() elif (keys & pew.K_X and not (brick == BRICKS[7])): new_brick = pew.Pix.from_iter([[ brick.pixel(y, brick.height - x - 1) 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 debounce() screen.blit(board, dx=0, dy=0, x=1, y=3, width=6, height=8) screen.blit(brick, dx=brick_x, dy=brick_y, key=0) pew.show(screen) if keys & pew.K_DOWN: break pew.tick(1 / 4) brick_y += 1 board.blit(brick, dx=brick_x + 1, dy=brick_y - 1 + 3, key=0) debounce() if brick_y < 0: break for row in range(11): if sum(1 for x in range(1, 7) if board.pixel(x, row)) != 6: continue for y in range(row, 0, -1): for x in range(1, 7): board.pixel(x, y, board.pixel(x, y - 1)) screen.box(0, 6, 0, 2, 5) for y in range(7, -1, -1): screen.box(3, x=0, y=y, width=6, height=1)
dy = 0 if keys & pew.K_UP: dy = -1 elif keys & pew.K_DOWN: dy = 1 elif keys & pew.K_LEFT: dx = -1 elif keys & pew.K_RIGHT: dx = 1 target = screen.pixel(x + dx, y + dy) behind = screen.pixel(x + dx + dx, y + dy + dy) if target in {0, 2}: x += dx y += dy elif target in {3, 7} and behind in {0, 2}: screen.pixel(x + dx + dx, y + dy + dy, 3 if behind == 0 else 7) x += dx y += dy count = 0 for b in range(8): for a in range(8): if screen.pixel(a, b) == 2: count += 1 if count == 0: break screen.pixel(x, y, (3 if blink else 2) + (4 if screen.pixel(x, y) in {2, 7} else 0)) blink = not blink pew.show(screen) pew.tick(1 / 6)
l = True while True: if l: gm_sp = 5 pts = 0 snake = [(3, 3)] dx, dy = 1, 0 apple_x, apple_y = 6, 6 screen.pixel(apple_x, apple_y, 3) l = False while True: x, y = snake[-1] screen.pixel(x, y, 3) pew.show(screen) pew.tick(1 / gm_sp) keys = pew.keys() if keys & pew.K_UP and dy == 0: dx, dy = 0, -1 elif keys & pew.K_LEFT and dx == 0: dx, dy = -1, 0 elif keys & pew.K_RIGHT and dx == 0: dx, dy = 1, 0 elif keys & pew.K_DOWN and dy == 0: dx, dy = 0, 1 x = (x + dx) y = (y + dy) s_b = 3 y_pos_b = 4
gates[0] = 'H' gates[1] = 'Z' gates[2] = 'M' g = 0 #add circuit while True: if len(snake) > 1: x, y = snake[-2] screen.pixel(x, y, 1) x, y = snake[-1] screen.pixel(x, y, 3) pew.show(screen) pew.tick(1 / game_speed) keys = pew.keys() if keys & pew.K_UP and dy == 0: dx, dy = 0, -1 elif keys & pew.K_LEFT and dx == 0: dx, dy = -1, 0 elif keys & pew.K_RIGHT and dx == 0: dx, dy = 1, 0 elif keys & pew.K_DOWN and dy == 0: dx, dy = 0, 1 x = (x + dx) % 8 y = (y + dy) % 8 if keys & pew.K_O: dis.blit(ima2, (0, 320))
def main_loop(self): while True: self.keys() self.print_dot() pew.show(self.screen) pew.tick(1 / 3)
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)
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()
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)
passcount = 0 else: error = 4 if keys & pew.K_X: turn = turn ^ 3 passcount += 1 if keys & pew.K_RIGHT: cursorx = (cursorx + 1) & 7 error = 0 if keys & pew.K_LEFT: cursorx = (cursorx - 1) & 7 error = 0 if keys & pew.K_UP: cursory = (cursory - 1) & 7 error = 0 if keys & pew.K_DOWN: cursory = (cursory + 1) & 7 error = 0 checkwin(board, passcount) blink = 0 if keys != 0 else (blink + 1) % 6 screen.blit(pew.Pix(8, 8, board)) if blink < 2 and turn == 1: screen.pixel( cursorx, cursory, turn if lookup(board, cursorx, cursory) == 0 and error == 0 else 3) if error != 0: error -= 1 show(screen) pew.tick(0.06)
else: if self.state[curr_y][curr_x] == value: movements.add((curr_y, curr_x)) if not game_over: pew.Pix.from_text("GAME OVER!") pew.init() screen = pew.Pix() blinking = 0 add_in = 0 game = Game2048() while True: if add_in == 0: game.add_random_items() add_in = None elif add_in: add_in -= 1 blinking = 0 if blinking == 9 else blinking + 1 board = pew.Pix.from_iter(game.get_board(blinking)) keys = pew.keys() if keys != 0 and not add_in: add_in = 10 # add new items after some delay game.move(keys) screen.blit(board) pew.show(screen) pew.tick(1 / 128)