from kandinsky import * from ion import keydown, KEY_LEFT, KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_OK from time import sleep, monotonic from random import randint, choice level=1 playing=False def game(): global level global playing playing=True COLORS={ "orange1":(250,153,50), "orange2":(247,124,22), "orange3":(245,104,22), "gray1":(184,184,184), "gray2":(110,115,118), "blue1":(179,242,255), "blue2":(0,128,255), "black":(0,0,0), "white":(255,255,255)} player_x=160 player_y=202 direction=1 FISH_STEPS=4 sharks = [{ "x": randint(25, 310), "y": i*20, "dir": choice((-1,1)), "steps": randint(level*2,level*5)} 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 fill_screen(color): fill_rect(0, 0, 320, 240, color) def render_fish(fish_x, fish_y, dir, fish): def fish_pixel(x, y, color): set_pixel(fish_x + dir * x, fish_y + y, COLORS[color]) if fish == "fish": fish_pixel(0, -3, "black") fish_pixel(1, -3, "black") fish_pixel(2, -3, "black") fish_pixel(-2, -2, "black") fish_pixel(-1, -2, "black") fish_pixel(0, -2, "white") fish_pixel(1, -2, "orange1") fish_pixel(2, -2, "orange1") fish_pixel(3, -2, "black") fish_pixel(4, -2, "black") fish_pixel(-6, -1, "black") fish_pixel(-5, -1, "black") fish_pixel(-3, -1, "black") fish_pixel(-2, -1, "orange1") fish_pixel(-1, -1, "white") fish_pixel(0, -1, "white") fish_pixel(1, -1, "white") fish_pixel(2, -1, "orange2") fish_pixel(3, -1, "orange1") fish_pixel(4, -1, "white") fish_pixel(5, -1, "black") fish_pixel(-7, 0, "black") fish_pixel(-6, 0, "white") fish_pixel(-5, 0, "orange1") fish_pixel(-4, 0, "black") fish_pixel(-3, 0, "orange1") fish_pixel(-2, 0, "orange2") fish_pixel(-1, 0, "orange2") fish_pixel(0, 0, "white") fish_pixel(1, 0, "white") fish_pixel(2, 0, "orange2") fish_pixel(3, 0, "white") fish_pixel(4, 0, "black") fish_pixel(5, 0, "orange1") fish_pixel(6, 0, "black") fish_pixel(-7, 1, "black") fish_pixel(-6, 1, "white") fish_pixel(-5, 1, "orange2") fish_pixel(-4, 1, "orange2") fish_pixel(-3, 1, "orange2") fish_pixel(-2, 1, "orange2") fish_pixel(-1, 1, "orange2") fish_pixel(0, 1, "white") fish_pixel(1, 1, "orange2") fish_pixel(2, 1, "orange2") fish_pixel(3, 1, "white") fish_pixel(4, 1, "orange2") fish_pixel(5, 1, "orange2") fish_pixel(6, 1, "black") fish_pixel(-7, 2, "black") fish_pixel(-6, 2, "gray1") fish_pixel(-5, 2, "orange2") fish_pixel(-4, 2, "black") fish_pixel(-3, 2, "orange3") fish_pixel(-2, 2, "orange3") fish_pixel(-1, 2, "gray1") fish_pixel(0, 2, "orange3") fish_pixel(1, 2, "orange3") fish_pixel(2, 2, "orange3") fish_pixel(3, 2, "orange2") fish_pixel(4, 2, "gray1") fish_pixel(5, 2, "black") fish_pixel(-6, 3, "black") fish_pixel(-5, 3, "black") fish_pixel(-3, 3, "black") fish_pixel(-2, 3, "orange2") fish_pixel(-1, 3, "gray1") fish_pixel(0, 3, "black") fish_pixel(1, 3, "black") fish_pixel(2, 3, "orange2") fish_pixel(3, 3, "orange2") fish_pixel(4, 3, "black") fish_pixel(-3, 4, "black") fish_pixel(-2, 4, "black") fish_pixel(-1, 4, "black") fish_pixel(1, 4, "black") fish_pixel(2, 4, "black") fish_pixel(3, 4, "black") if fish=="shark": fish_pixel(0,-4,"black") fish_pixel(1,-4,"black") fish_pixel(-10,-3,"black") fish_pixel(-9,-3,"black") fish_pixel(0,-3,"black") fish_pixel(1,-3,"gray2") fish_pixel(2,-3,"black") fish_pixel(-9,-2,"black") fish_pixel(-8,-2,"black") fish_pixel(-3,-2,"black") fish_pixel(-2,-2,"black") fish_pixel(-1,-2,"black") fish_pixel(0,-2,"black") fish_pixel(1,-2,"gray2") fish_pixel(2,-2,"gray2") fish_pixel(3,-2,"black") fish_pixel(4,-2,"black") fish_pixel(5,-2,"black") fish_pixel(6,-2,"black") fish_pixel(-9,-1,"black") fish_pixel(-8,-1,"gray2") fish_pixel(-7,-1,"black") fish_pixel(-6,-1,"black") fish_pixel(-5,-1,"black") fish_pixel(-4,-1,"black") fish_pixel(-3,-1,"gray2") fish_pixel(-2,-1,"gray2") fish_pixel(-1,-1,"gray2") fish_pixel(0,-1,"gray2") fish_pixel(1,-1,"gray2") fish_pixel(2,-1,"gray2") fish_pixel(3,-1,"gray2") fish_pixel(4,-1,"gray2") fish_pixel(5,-1,"white") fish_pixel(6,-1,"white") fish_pixel(7,-1,"black") fish_pixel(8,-1,"black") fish_pixel(9,-1,"black") fish_pixel(10,-1,"black") fish_pixel(-8,0,"black") fish_pixel(-7,0,"gray2") fish_pixel(-6,0,"gray2") fish_pixel(-5,0,"gray2") fish_pixel(-4,0,"gray2") fish_pixel(-3,0,"gray2") fish_pixel(-2,0,"gray2") fish_pixel(-1,0,"gray2") fish_pixel(0,0,"gray2") fish_pixel(1,0,"gray2") fish_pixel(2,0,"gray2") fish_pixel(3,0,"gray2") fish_pixel(4,0,"gray2") fish_pixel(5,0,"white") fish_pixel(6,0,"black") fish_pixel(7,0,"gray2") fish_pixel(8,0,"gray2") fish_pixel(9,0,"gray2") fish_pixel(10,0,"black") fish_pixel(-9,1,"black") fish_pixel(-8,1,"gray2") fish_pixel(-7,1,"black") fish_pixel(-6,1,"gray1") fish_pixel(-5,1,"gray2") fish_pixel(-4,1,"gray2") fish_pixel(-3,1,"gray2") fish_pixel(-2,1,"gray2") fish_pixel(-1,1,"gray2") fish_pixel(0,1,"gray2") fish_pixel(1,1,"gray2") fish_pixel(2,1,"gray2") fish_pixel(3,1,"white") fish_pixel(4,1,"gray2") fish_pixel(5,1,"gray2") fish_pixel(6,1,"gray2") fish_pixel(7,1,"gray2") fish_pixel(8,1,"gray2") fish_pixel(9,1,"black") fish_pixel(-9,2,"black") fish_pixel(-8,2,"black") fish_pixel(-6,2,"black") fish_pixel(-5,2,"gray1") fish_pixel(-4,2,"gray1") fish_pixel(-3,2,"gray1") fish_pixel(-2,2,"gray1") fish_pixel(-1,2,"gray1") fish_pixel(0,2,"gray1") fish_pixel(1,2,"gray2") fish_pixel(2,2,"gray2") fish_pixel(3,2,"gray2") fish_pixel(4,2,"white") fish_pixel(5,2,"white") fish_pixel(6,2,"white") fish_pixel(7,2,"white") fish_pixel(8,2,"black") fish_pixel(-10,3,"black") fish_pixel(-9,3,"black") fish_pixel(-5,3,"black") fish_pixel(-4,3,"black") fish_pixel(-3,3,"black") fish_pixel(-2,3,"black") fish_pixel(-1,3,"black") fish_pixel(0,3,"gray2") fish_pixel(1,3,"gray2") fish_pixel(2,3,"gray2") fish_pixel(3,3,"gray1") fish_pixel(4,3,"gray1") fish_pixel(5,3,"white") fish_pixel(6,3,"white") fish_pixel(7,3,"black") fish_pixel(-2,4,"black") fish_pixel(-1,4,"gray2") fish_pixel(0,4,"gray2") fish_pixel(1,4,"black") fish_pixel(2,4,"black") fish_pixel(3,4,"black") fish_pixel(4,4,"black") fish_pixel(5,4,"black") fish_pixel(6,4,"black") fish_pixel(-1,5,"black") fish_pixel(0,5,"black") fish_pixel(2,5,"black") fish_pixel(3,5,"black") def tick(): nonlocal direction nonlocal player_x nonlocal 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<10: 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() fill_screen(COLORS["blue2"]) for shark in sharks: render_fish(shark['x'],shark['y'],shark['dir'],"shark") render_fish(player_x, player_y, direction, "fish") for _ in range(1): size=randint(2,4) fill_rect(randint(15,310),randint(15,230),size,size,COLORS["blue1"]) count_down=round(20-(monotonic()-start_time)) draw_string(str(count_down),5,5) draw_string(str(level),295,5) def reset(won): global level global playing if won: level+=1 else: level=1 playing=False fill_screen(COLORS["blue2"]) draw_string("press OK to continue",60,100) render_fish(60,50,1,"shark") render_fish(260,170,-1,"fish") for i in range(2): render_fish(50,40+i*20,1,"shark") render_fish(270,163+i*15,-1,"fish") while not keydown(KEY_OK): pass start_time=monotonic() while playing: tick() sleep(0.05) while True: if not playing: game()