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 ls.keys(): ls.tick(0.1) while not ls.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) ls.tick(0.06) raise ls.GameOver()
def end_game(winner=False): scene = new_scene() message = 'You Win' if winner else 'Game Over' text = ls.Pix.from_text(message, color=ls.RED, bgcolor=ls.BLUE) for dx in range(-8, text.width): scene.blit(text, -dx, 1) ls.show(scene) ls.tick(1 / 12)
def game(): data = init_data() while not has_won_the_game(data): data = update_game(data) show_scene(data) speed = data['player']['speed'] ls.tick(1 / speed)
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: screen.pixel(x + dx + dx, y + dy + dy, ROCK) screen.pixel(x + dx, y + dy, VOID) x += dx y += dy count = 0 for a in range(8): for b in range(7, -1, -1): if (screen.pixel(a, b) == ROCK and screen.pixel(a, b + 1) == VOID and (a, b + 1) != (x, y)): screen.pixel(a, b, VOID) screen.pixel(a, b + 1, ROCK) if (a, b + 2) == (x, y): dead = True if screen.pixel(a, b) in {GEM1, GEM2}: screen.pixel(a, b, GEM1 if blink else GEM2) count += 1 if count == 0: break screen.pixel(x, y, 3 if blink else HERO) blink = not blink ls.show(screen) ls.tick(1 / 6)
ls.init() screen = ls.Pix() while True: ################################################################################ # First Phase text = ls.Pix.from_text("Hello world!", color=ls.RED, bgcolor=ls.BLUE) screen.box(color=ls.BLUE, x=0, y=0, width=8, height=8) start = time.monotonic() while time.monotonic() - start < 5: for dx in range(-8, text.width): screen.blit(text, -dx, 1) ls.show(screen) ls.tick(1/12) ################################################################################ # Second Phase # Try orange for undrawn pixels # screen.box(color=52, x=0, y=0, width=8, height=8) for x in range(4): screen.pixel(x, 0, color=red(x)) screen.pixel(x+4, 0, color=red(3-x)) screen.pixel(x, 1, color=green(x)) screen.pixel(x+4, 1, color=green(3-x)) screen.pixel(x, 2, color=blue(x)) screen.pixel(x+4, 2, color=blue(3-x))
game_speed = 4 snake = [(ls.GREEN, ls.BLUE)] dx, dy = 1, 0 apple_x, apple_y = 6, 4 screen.pixel(apple_x, apple_y, ls.RED) while True: if len(snake) > 1: x, y = snake[-2] screen.pixel(x, y, 1) x, y = snake[-1] screen.pixel(x, y, 3) ls.show(screen) ls.tick(1 / game_speed) keys = ls.keys() if keys & ls.K_UP and dy == 0: dx, dy = 0, -1 elif keys & ls.K_LEFT and dx == 0: dx, dy = -1, 0 elif keys & ls.K_RIGHT and dx == 0: dx, dy = 1, 0 elif keys & ls.K_DOWN and dy == 0: dx, dy = 0, 1 x = (x + dx) % 8 y = (y + dy) % 8 if (x, y) in snake: break
passcount = 0 else: error = 4 if keys & ls.K_X: turn = turn ^ 3 passcount += 1 if keys & ls.K_RIGHT: cursorx = (cursorx + 1) & 7 error = 0 if keys & ls.K_LEFT: cursorx = (cursorx - 1) & 7 error = 0 if keys & ls.K_UP: cursory = (cursory - 1) & 7 error = 0 if keys & ls.K_DOWN: cursory = (cursory + 1) & 7 error = 0 checkwin(board, passcount) blink = 0 if keys != 0 else (blink + 1) % 6 screen.blit(ls.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) ls.tick(0.06)
y -= 1 elif keys & ls.K_DOWN and y < 7: y += 1 if keys & ls.K_LEFT and x > 0: x -= 1 elif keys & ls.K_RIGHT and x < 7: x += 1 if keys: pressing = True else: if not keys: pressing = False screen.blit(pedestrians, (traffic // 2) % 8 - 8, 1) screen.blit(cars, -((traffic // 2) % 8), 2) screen.blit(cars, traffic // 4 - 8, 5) screen.blit(pedestrians, -traffic // 4, 6) old_pixel = screen.pixel(x, y) if old_pixel == ls.RED or old_pixel == ls.YELLOW: alive = False traffic = (traffic + 1) % 32 screen.pixel(x, y, ls.BLUE) ls.show(screen) ls.tick(1/6) text = ls.Pix.from_text("Game over!", color=ls.RED, bgcolor=ls.WHITE) for dx in range(-8, text.width): screen.blit(text, -dx, 1) ls.show(screen) ls.tick(1/12)
def debounce(): for i in range(100): ls.tick(1 / 100) if not ls.keys(): return
brick = new_brick debounce() elif keys & ls.K_X: new_brick = ls.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) ls.show(screen) if keys & ls.K_DOWN: break ls.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)