from kandinsky import set_pixel, color, draw_string, fill_rect from ion import keydown, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_OK from time import * from random import randint, choice level=1 playing=False def game(): global level, playing playing=True COLORS={ "o1":(250,153,50), "o2":(247,124,22), "o3":(245,104,22), "g1":(184,184,184), "g2":(110,115,118), "b1":(100,200,225), "b2":(0, 135, 211), "bg":(0,128,255), "b":(0,0,0), "w":(255,255,255)} player_x=160 player_y=205 direction=1 FISH_STEPS=4 sharks = [{ "x": randint(25, 310), "y": i*20, "dir": choice((-1,1)), "steps": randint(level,level*4)} for i in range(2,10)] def is_eaten(): fish_width=14 fish_height=8 shark_width=21 shark_height=10 for shark in sharks: if (player_x<shark['x'] + shark_width and player_x + fish_width > shark['x'] and player_y < shark['y'] + shark_height and player_y + fish_height > shark['y']): reset(False) def clear_screen(): fill_rect(0, 0, 320, 240, COLORS["bg"]) def render_fish(fish_x, fish_y, dir, fish_type): def fish_pixel(x, y, color): set_pixel(fish_x + dir * x, fish_y + y, COLORS[color]) fish_data={ "clownfish":[ (0, -3, "b"),(1, -3, "b"),(2, -3, "b"),(-2, -2, "b"),(-1, -2, "b"), (0, -2, "w"),(1, -2, "o1"),(2, -2, "o1"),(3, -2, "b"),(4, -2, "b"), (-6, -1, "b"),(-5, -1, "b"),(-3, -1, "b"),(-2, -1, "o1"),(-1, -1, "w"), (0, -1, "w"),(1, -1, "w"),(2, -1, "o2"),(3, -1, "o1"),(4, -1, "w"), (5, -1, "b"),(-7, 0, "b"),(-6, 0, "w"),(-5, 0, "o1"),(-4, 0, "b"), (-3, 0, "o1"),(-2, 0, "o2"),(-1, 0, "o2"),(0, 0, "w"),(1, 0, "w"), (2, 0, "o2"),(3, 0, "w"),(4, 0, "b"),(5, 0, "o1"),(6, 0, "b"), (-7, 1, "b"),(-6, 1, "w"),(-5, 1, "o2"),(-4, 1, "o2"),(-3, 1, "o2"), (-2, 1, "o2"),(-1, 1, "o2"),(0, 1, "w"),(1, 1, "o2"),(2, 1, "o2"), (3, 1, "w"),(4, 1, "o2"),(5, 1, "o2"),(6, 1, "b"),(-7, 2, "b"), (-6, 2, "g1"),(-5, 2, "o2"),(-4, 2, "b"),(-3, 2, "o3"),(-2, 2, "o3"), (-1, 2, "g1"),(0, 2, "o3"),(1, 2, "o3"),(2, 2, "o3"),(3, 2, "o2"), (4, 2, "g1"),(5, 2, "b"),(-6, 3, "b"),(-5, 3, "b"),(-3, 3, "b"), (-2, 3, "o2"),(-1, 3, "g1"),(0, 3, "b"),(1, 3, "b"),(2, 3, "o2"), (3, 3, "o2"),(4, 3, "b"),(-3, 4, "b"),(-2, 4, "b"),(-1, 4, "b"), (1, 4, "b"),(2, 4, "b"),(3, 4, "b")], "shark":[ (0,-4,"b"),(1,-4,"b"),(-10,-3,"b"),(-9,-3,"b"),(0,-3,"b"),(1,-3,"g2"), (2,-3,"b"),(-9,-2,"b"),(-8,-2,"b"),(-3,-2,"b"),(-2,-2,"b"),(-1,-2,"b"), (0,-2,"b"),(1,-2,"g2"),(2,-2,"g2"),(3,-2,"b"),(4,-2,"b"),(5,-2,"b"), (6,-2,"b"),(-9,-1,"b"),(-8,-1,"g2"),(-7,-1,"b"),(-6,-1,"b"),(-5,-1,"b"), (-4,-1,"b"),(-3,-1,"g2"),(-2,-1,"g2"),(-1,-1,"g2"),(0,-1,"g2"), (1,-1,"g2"),(2,-1,"g2"),(3,-1,"g2"),(4,-1,"g2"),(5,-1,"w"),(6,-1,"w"), (7,-1,"b"),(8,-1,"b"),(9,-1,"b"),(10,-1,"b"),(-8,0,"b"),(-7,0,"g2"), (-6,0,"g2"),(-5,0,"g2"),(-4,0,"g2"),(-3,0,"g2"),(-2,0,"g2"),(-1,0,"g2"), (0,0,"g2"),(1,0,"g2"),(2,0,"g2"),(3,0,"g2"),(4,0,"g2"),(5,0,"w"), (6,0,"b"),(7,0,"g2"),(8,0,"g2"),(9,0,"g2"),(10,0,"b"),(-9,1,"b"), (-8,1,"g2"),(-7,1,"b"),(-6,1,"g1"),(-5,1,"g2"),(-4,1,"g2"),(-3,1,"g2"), (-2,1,"g2"),(-1,1,"g2"),(0,1,"g2"),(1,1,"g2"),(2,1,"g2"),(3,1,"w"), (4,1,"g2"),(5,1,"g2"),(6,1,"g2"),(7,1,"g2"),(8,1,"g2"),(9,1,"b"), (-9,2,"b"),(-8,2,"b"),(-6,2,"b"),(-5,2,"g1"),(-4,2,"g1"),(-3,2,"g1"), (-2,2,"g1"),(-1,2,"g1"),(0,2,"g1"),(1,2,"g2"),(2,2,"g2"),(3,2,"g2"), (4,2,"w"),(5,2,"w"),(6,2,"w"),(7,2,"w"),(8,2,"b"),(-10,3,"b"), (-9,3,"b"),(-5,3,"b"),(-4,3,"b"),(-3,3,"b"),(-2,3,"b"),(-1,3,"b"), (0,3,"g2"),(1,3,"g2"),(2,3,"g2"),(3,3,"g1"),(4,3,"g1"),(5,3,"w"), (6,3,"w"),(7,3,"b"),(-2,4,"b"),(-1,4,"g2"),(0,4,"g2"),(1,4,"b"), (2,4,"b"),(3,4,"b"),(4,4,"b"),(5,4,"b"),(6,4,"b"),(-1,5,"b"),(0,5,"b"), (2,5,"b"),(3,5,"b")]} pixels=fish_data[fish_type] for pixel in pixels: fish_pixel(pixel[0],pixel[1],pixel[2]) def tick(): nonlocal direction, player_x, player_y if keydown(KEY_UP): player_y -= FISH_STEPS if keydown(KEY_DOWN) and player_y < 215: player_y += FISH_STEPS if keydown(KEY_RIGHT) and player_x < 310: direction = 1 player_x += FISH_STEPS if keydown(KEY_LEFT) and player_x > 10: direction = -1 player_x -= FISH_STEPS if player_y<-5: reset(True) for shark in sharks: if shark['x'] <= 25: shark['dir'] = 1 if shark['x'] >= 300: shark['dir'] = -1 shark['x'] += shark['dir'] * shark['steps'] is_eaten() clear_screen() for _ in range(2): size=randint(2,4) fill_rect(randint(15,310),randint(15,230),size,size,COLORS["b1"]) for shark in sharks: render_fish(shark['x'],shark['y'],shark['dir'],"shark") render_fish(player_x, player_y, direction, "clownfish") count_down=round(15-(monotonic()-start_time)) if count_down<=0: reset(False) draw_string(str(count_down),5,5,COLORS["b1"],COLORS["bg"]) draw_string(str(level),295,7,COLORS["b1"],COLORS["bg"]) def reset(won): global level global playing if won: level+=1 else: level=1 playing=False clear_screen() for _ in range(10): size=randint(2,4) fill_rect(randint(15,310),randint(15,230),size,size,COLORS["b1"]) draw_string("Crossy Oceans",95,90,COLORS["b1"],COLORS["bg"]) draw_string("press OK to continue",60,115,COLORS["b1"],COLORS["bg"]) draw_string(str(level),295,7,COLORS["b1"],COLORS["bg"]) render_fish(60,50,1,"shark") render_fish(260,170,-1,"clownfish") for i in range(2): render_fish(50,40+i*20,1,"shark") render_fish(270,163+i*15,-1,"clownfish") while not keydown(KEY_OK): pass start_time=monotonic() while playing: tick() sleep(0.06) while True: if not playing: game()