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 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_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 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 restart_animation(): """ Shows restart animation on display. """ pew.show(ALL_ON) time.sleep(1) pew.show(ALL_OFF) time.sleep(0.5)
def end_game(): print('you are out of ammo') text = pew.Pix.from_text( 'Game over!') #text will be shown when game ends also shwoing stat for a in range(-8, text.width): screen.blit(text, -a, 1) pew.show(screen) return False
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 paint(): if choice == 0: for i in range(3): screen.box(i + 1, 2 * i + 1, 1, 2, 2) else: for i in range(3): screen.box(32 + i + 1, 2 * i + 1, 1, 2, 1) for c in range(1, 5): if choice == c: for i in range(3): screen.box(48 + c * 16 + i + 1, 2 * i + 1, 1 + c, 2, 2) else: screen.box(48 + c * 16 + 1, 1, 1 + c + (0 if choice > c else 1), 6, 1) pew.show(screen)
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 action(self, s): if s == 0: self.active = not self.active ap.active(self.active) if self.active: import ubinascii essid = b"MicroPython-%s" % ubinascii.hexlify(ap.config("mac")[-3:]) password = b"micropythoN" for i in range(8): screen.pixel(i, 7, 3) pew.show(screen) i += 1 time.sleep(0.1) if ap.active(): break ap.config(essid=essid, authmode=network.AUTH_WPA_WPA2_PSK, password=password, hidden=False) stack.append(Message('pass:{:s}\nname:{:s}'.format(password, essid))) elif s == 2: stack.append(Ifconfig(ap))
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 _connect(ssid, password): try: sta.disconnect() while sta.isconnected(): time.sleep(0.1) sta.connect(ssid, password) for i in range(8): screen.pixel(i, 7, 3) if sta.isconnected(): screen.box(1, 0, 7, 8, 1) break status = sta.status() if status != network.STAT_CONNECTING: screen.box(2, 0, 7, i, 1) pew.show(screen) time.sleep(1) stack.pop() stack.append(Message({network.STAT_WRONG_PASSWORD: '******', network.STAT_NO_AP_FOUND: 'no AP found', network.STAT_CONNECT_FAIL: 'connection failed'}.get(status, str(status)))) return pew.show(screen) time.sleep(2) else: screen.box(2, 0, 7, 8, 1) pew.show(screen) time.sleep(1) stack.pop() except OSError as e: stack.append(Message('Error: ' + str(e)))
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 show_scene(data): scene = new_scene() scene = show_blocks(scene, data['blocks']) scene = show_player(scene, data['player']) scene = show_ball(scene, data['ball']) pew.show(scene)
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)
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)
def draw(self): self.draw_paddle(self.left_paddle) self.draw_paddle(self.right_paddle) self.draw_ball() pew.show(self.screen)
# Very simple fill screen from l-r , top-bottom using 2 nested loop import pew # Intialise Device pew.init() # Create Blank Screen Image screen = pew.Pix() for y in range(8): for x in range(8): screen.pixel(x, y, 1) # plot pixel at x,y brightness 1 pew.show(screen) # Update Display pew.tick(.25) # pause .25 seconds
# Pad characteristics g_pad = { 'X': 2, 'Y': 7, 'WIDTH': 2, 'HEIGHT': 1, 'COLOR': 3 } # ball characteristics g_ball = { 'X': 4, 'Y': 2, 'VX': 1, 'VY': 1, 'WIDTH': 1, 'HEIGHT': 1, 'SIZE': 1, 'COLOR': 1 } while True: inputs(g_pad) updates(g_pad, g_ball) draw(g_screen, g_pad, g_ball) pew.show(g_screen) pew.tick(1 / 4)
] ] def fill_rect(source_image, x, y, wx, wy): x = int(x) y = int(y) wx = int(wx) wy = int(wy) for index_y in range(y, y + wy): for index_x in range(x, x + wx): source_image[index_y][index_x] = 0 screen = pew.Pix() pew.show(screen) blinkIndex = [1, 2, 3, 4, 3, 2, 1] # Blink bitmap sequence blinkCountdown = 100 # Countdown to next blink (in frames) gazeCountdown = 75 # Countdown to next eye movement gazeFrames = 50 # Duration of eye movement (smaller = faster) eyeX = 3 eyeY = 3 # Current eye position newX = 3 newY = 3 # Next eye position dX = 0 dY = 0 # Distance from prior to new position loop = True while loop: keys = pew.keys()
# Simple moving dot from random start position import pew from random import randint pew.init() # intialise device screen = pew.Pix() # create empty screen image x = randint(0, 7) # random intial x position y = randint(0, 7) # random intial y position dx = 1 # initial x step dy = 1 # initial y step while True: screen.pixel(x, y, 0) #make previuos pixel not lit if x < 0 or x > 7: #if next x position is off screen dx = -dx #reverse its direction if y < 0 or y > 7: #if next y position is off screen dy = -dy #reverse its direction x += dx #update x position y += dy #update y position screen.pixel(x, y, 2) #plot next pixel pew.show(screen) #update screen pew.tick(1 / 12) #pause approx 1/2 second
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)
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 show(p): for i in range(len(p.buffer)): c = p.buffer[i] screen2.buffer[i] = c ^ (c >> 1) pew.show(screen2)