from kandinsky import * from ion import keydown as kd,KEY_UP,KEY_DOWN,KEY_LEFT,KEY_RIGHT,KEY_OK from time import * from random import * SCREEN_W=320 SCREEN_H=222 BIRD_W=16 BIRD_H=13 ENEMY_W=22 ENEMY_H=13 FPS=22 start=monotonic() hearts=5 bird_y=SCREEN_H/2 bird_x=40 state=1 enemies=[] heal=[] COLORS={ "b":"black", "r":"red", "w":"white", "o":"orange", "e":"green", "g":color(88,107,139), "a":color(61,71,111), "bg":color(77,195,255)} def initialise(): hearts=5 bird_y=SCREEN_H/2 bird_x=40 state=1 start=monotonic() enemies=[{"x":SCREEN_W-15,"y":randint(30,SCREEN_H-30),"speed":(3+(monotonic()-start)/8)*3*random(),"type":choice(["yellow","gray","black"]),"state":1}] heal=[] print("New game") def render(x,y,sprite): sprites={ "heart":"0200b0300b0400b0800b0900b1000b0101b0201r0301r0401r0501b0701b0801r0901r1001r1101b0002b0102r0202w0302w0402r0502r0602b0702r0802r0902r1002r1102r1202b0003b0103r0203w0303r0403r0503r0603r0703r0803r0903r1003r1103r1203b0004b0104r0204r0304r0404r0504r0604r0704r0804r0904r1004r1104r1204b0105b0205r0305r0405r0505r0605r0705r0805r0905r1005r1105b0206b0306r0406r0506r0606r0706r0806r0906r1006b0307b0407r0507r0607r0707r0807r0907b0408b0508r0608r0708r0808b0509b0609r0709b0610b", "bird1":"0500b0600b0700b0201b0301b0401b0501b0601b0701b0801b0901b1201b1301b1401b0202b0302b0402b0502b0602b0702b0802b0902b1002b1102b1202b1302b1402b1502b0203b0303b0403b0503b0603b0703b0803b0903b1003b1103b1203b1303b1403b1503b0304b0404b0504b0604b0704b0804b0904b1004b1104b1204b1304b1404b0105b0205b0305b0405b0505b0605b0705b0805b0905r1005r1105r1205b1305b0006b0106b0206b0306b0406b0506b0606b0706r0806r0906r1006r1106r1206b0007b0107b0207b0307b0407b0507r0607r0707r0807r0907r1007r1107b0408b0508b0608r0708r0808r0908b1008b0509b0609b0709b0809b0510b0610b", "bird2":"0400b0500b0600b0700b0301b0401b0501b0601b0701b0801b0901b1001b1101b1201b1301b1401b0202b0302b0402b0502b0602b0702b0802b0902b1002b1102b1202b1302b1402b1502b0203b0303b0403b0503b0603b0703b0803b0903b1003b1103b1203b1303b1403b1503b0204b0304b0404b0504b0604b0704b0804b0904b1004r1104r1204b1304b1404b0105b0205b0305b0405b0505b0605b0705b0805b0905r1005r1105r1205b1305b0006b0106b0206b0306b0406b0506b0606b0706b0806r0906r1006r1106b0007b0107b0207b0307b0407b0507b0607b0707b0807r0907b1007b0308b0408b0508b0608b0708b0808b0409b0509b0609b", "bird3":"1200b1300b1400b0901b1001b1101b1201b1301b1401b1501b0602b0702b0802b0902b1002b1102b1202b1302b1402b1502b0103b0203b0303b0403b0503b0603b0703b0803b0903b1003b1103r1203b1303b1403b0004b0104b0204b0304b0404b0504b0604b0704b0804b0904b1004b1104r1204b1304b0005b0105b0205b0305b0405b0505b0605b0705b0805b0905b1005r1105b0206b0306b0406b0506b0606b0706b0806b0906b1006b0307b0407b0507b0607b0707b0807b0907b0408b0508b0608b0708b0808b0908b0509b0609b0709b0809b0909b0710b0810b0910b", "bird4":"1200b1300b1400b0801b0901b1001b1101b1201b1301b1401b1501b0602b0702b0802b0902b1002b1102b1202b1302b1402b1502b0003b0103b0203b0403b0503b0603b0703b0803b0903b1003b1103b1203b1303b1403b0004b0104b0204b0304b0404b0504b0604b0704b0804b0904b1004b1104r1204b1304b0105b0205b0305b0405b0505b0605b0705b0805b0905r1005r1105r1205b0206b0306b0406b0506r0606r0706r0806r0906r1006r1106b0407b0507b0607r0707r0807r0907b1007b0508b0608b0708b0808b0509b0609b", "bird5":"0500b0600b0700b0201b0301b0401b0501r0601r0701r0801b0901b1201b1301b1401b0202b0302r0402r0502r0602r0702r0802r0902r1002b1102b1202r1302r1402r1502b0203b0303b0403b0503r0603r0703r0803r0903r1003r1103r1203r1303r1403r1503b0304b0404r0504r0604r0704r0804r0904r1004r1104r1204r1304r1404b0105b0205b0305r0405r0505r0605r0705r0805r0905r1005r1105r1205r1305b0006b0106r0206r0306r0406r0506r0606r0706r0806r0906r1006r1106r1206b0007b0107b0207b0307b0407r0507r0607r0707r0807r0907r1007r1107b0408b0508b0608r0708r0808r0908b1008b0509b0609r0709b0809b0510b0610b", "yellow1":"1100b1200b1300b1400b1500b1600b0201b0301b0401b0901b1001b1101b1201b1301b1401b1501b1601b0102b0202o0302o0402o0502b0702b0802b0902b1002b1102b1202b1302b1402b1502b1602b0003b0103o0203o0303o0403o0503o0603b0703b0803b0903b1003b1103b1203b1303b1403b1503b0004b0104o0204o0304o0404o0504o0604b0704b0804b0904b1004b1104b1204b1304b1404b1504b0105b0205o0305o0405o0505o0605b0705b0805b0905b1005b1105b1205b1305b1405b0206b0306b0406o0506o0606o0706b0806b0906b1006b1106b1206b1306b0407b0507o0607o0707o0807o0907o1007o1107o1207o1307o1407b1507b1607b1707b0408b0508o0608o0708o0808o0908o1008o1108o1208o1308o1408o1508o1608o1708b0509b0609o0709o0809o0909o1009o1109o1209o1309o1409b1509b1609b0610b0710b0810b0910b1010b1110b1210b1310b0711b0811b0911b1111b1211b1311b0812b0912b1212b1312b", "yellow2":"0200b0300b0400b0101b0201o0301o0401o0501b0601b0002b0102o0202o0302o0402o0502o0602o0702b0802b1102b1202b1302b1402b1502b1602b0003b0103b0203o0303o0403o0503o0603b0703b0803b0903b1003b1103b1203b1303b1403b1503b1603b0104b0204o0304o0404o0504o0604b0704b0804b0904b1004b1104b1204b1304b1404b1504b1604b0205b0305b0405o0505o0605b0705b0805b0905b1005b1105b1205b1305b1405b1505b1605b0406b0506o0606o0706b0806b0906b1006b1106b1206b1306b1406b1506b1606b0507b0607o0707b0807b0907b1007b1107b1207b1307b1407b1507o1607o1707b0608b0708b0808b0908b1008b1108b1208b1308b1408b1508b1608b1708b0609b0709b0809b0909b1009b1109b1209b1309b1409b0710b0810b1010b1110b1210b1310b", "yellow3":"0200b0300b0400b0101b0201o0301o0401o0501b0002b0102o0202o0302o0402o0502o0602b0702b0003b0103b0203o0303o0403o0503o0603b0703b0803b0903b0104b0204o0304o0404o0504o0604b0704b0804b0904b1004b1104b0205b0305b0405o0505o0605b0705b0805b0905b1005b1105b1205b1305b0406b0506o0606o0706b0806b0906b1006b1106b1206o1306o1406b1506b1606b1706b0407b0507o0607o0707b0807b0907b1007b1107b1207b1307o1407o1507o1607o1707b0508b0608o0708b0808b0908b1008b1108b1208b1308o1408b1508b1608b0609b0709b0809b0909b1009b1109b1209b1309b0710b0810b0910b1010b1110b1210b1310b0711b0811b0911b1011b1111b1211b1311b0712b0812b0912b1012b1112b0713b0813b0913b", "yellow4":"0200b0300b0101b0201o0301o0401b0501b0002b0102o0202o0302o0402o0502o0602b0702b0003b0103b0203o0303o0403o0503o0603b0703b0803b0903b0104b0204o0304o0404o0504o0604b0704b0804b0904b1004b1104b1204b0205b0305b0405o0505o0605b0705b0805b0905b1005b1105b1205b1305b1405b1505b1605b1705b0406b0506o0606o0706b0806b0906b1006b1106b1206b1306b1406b1506o1606o1706b0407b0507o0607o0707o0807o0907b1007b1107b1207o1307o1407o1507b1607b0508b0608o0708o0808o0908o1008o1108o1208o1308o1408b0609b0709b0809b0909b1009b1109b1209b1309b0710b0810b0910b1110b1210b1310b0811b0911b1211b1311b", "gray1":"1000b1100b1200b1300b1400b0801b0901b1001a1101a1201a1301a1401b0102b0202b0302b0702b0802g0902g1002g1102g1202a1302a1402b0003b0103a0203a0303a0403b0503b0603b0703g0803g0903g1003g1103g1203g1303b0004b0104a0204a0304a0404a0504a0604a0704g0804g0904g1004g1104g1204b0105b0205b0305e0405e0505e0605a0705g0805g0905g1005g1105g1205b1305b0306b0406b0506e0606e0706g0806g0906g1006g1106g1206g1306a1406b1506b1606b1706b0507b0607b0707g0807g0907g1007g1107g1207g1307a1407a1507a1607a1707a1807b0708b0808b0908g1008g1108g1208b1308b1408b1508b1608b1708b1808b0909b1009b1109r1209b1110b1210r1310b1211b1311b", "gray2":"0100b0200b0300b0001b0101a0201a0301a0401b0501b0601b0701b0801b0901b1001b1101b1201b1301b1401b0002b0102a0202a0302a0402a0502a0602a0702g0802g0902g1002g1102g1202a1302a1402b0103b0203b0303e0403e0503e0603a0703g0803g0903g1003g1103g1203g1303a1403b1603b1703b0304b0404b0504e0604e0704g0804g0904g1004g1104a1204g1304a1404b1504b1604a1704a1804b0505b0605b0705a0805g0905g1005g1105g1205g1305a1405a1505a1605b1705b1805b0706b0806b0906g1006g1106g1206b1306b1406b1506b0907b1007b1107r1207b1108b1208r1308b1209b1309b", "gray3":"0600b0700b0800b0900b0101b0201b0301b0401b0501b0601a0701a0801g0901g1001b1101b0002b0102a0202a0302a0402e0502e0602a0702g0802g0902g1002g1102g1202b0003b0103a0203a0303a0403e0503e0603e0703a0803g0903g1003g1103g1203g1303b1403b0104b0204b0304b0404b0504b0604b0704b0804g0904g1004g1104g1204g1304g1404a1504b1604b1704b0805b0905g1005g1105g1205g1305g1405a1505a1605a1705a1805b0806b0906g1006g1106g1206g1306g1406b1506b1606b1706b1806b0807b0907a1007g1107g1207a1307g1407b0908b1008a1108g1208b1308b1408b0909b1009a1109a1209b0910b1010b1110b", "gray4":"0100b0200b0300b0001b0101a0201a0301a0401b0501b0002b0102a0202a0302a0402a0502a0602b0702b0802b0902b1002b0103b0203b0303e0403a0503a0603a0703a0803g0903g1003a1103b1203b0304b0404e0504e0604a0704g0804g0904g1004g1104g1204g1304b0405b0505e0605e0705g0805g0905a1005g1105g1205a1305b1405b1505b1605b0506b0606b0706a0806g0906g1006g1106a1206a1306a1406a1506a1606a1706b0707b0807b0907g1007g1107g1207b1307b1407b1507b1607a1707a1807b0908b1008b1108r1208b1608b1708b1808b1109b1209r1309b1210b1310b", "black1":"1100b1200b1300b1400b1001b1101a1201a1301a1401a1501b1601b1701b0902b1002a1102a1202a1302a1402a1502a1602a1702a1802b1902b2002b0903b1003a1103a1203a1303a1403a1503a1603a1703a1803a1903a2003a2103b0904b1004a1104a1204a1304a1404a1504a1604a1704a1804a1904a2004a2104b0205b0305b0405b0505b0605b0805b0905a1005a1105a1205a1305a1405a1505a1605a1705a1805a1905b2005b2105b0106b0206g0306g0406a0506a0606a0706b0806a0906a1006a1106a1206a1306a1406a1506a1606a1706a1806a1906b0007b0107g0207g0307g0407a0507a0607a0707a0807a0907a1007a1107a1207a1307a1407a1507a1607a1707b1807b1907b0008b0108b0208b0308b0408a0508a0608a0708a0808a0908a1008a1108a1208a1308a1408a1508a1608b0409b0509b0609a0709a0809a0909a1009a1109a1209a1309a1409a1509a1609a1709b0610b0710a0810a0910a1010a1110a1210a1310a1410a1510a1610a1710a1810b0711b0811a0911a1011a1111a1211a1311a1411a1511a1611a1711a1811b0812b0912b1012b1112a1212a1312a1412a1512a1612a1712a1812a1912b2012b2112b1113b1213b1313a1413a1513a1613a1713b1813a1913a2013a2113a2213b2313b2413b1314b1414r1514b1614a1714a1814b1914b2014b2114a2214a2314a2414a2514b1415b1515r1615b1715a1815b2115b2215b2315b2415b2515b1416b1516r1616b1716a1816b1517b1617b1717b1817b", "black2":"0200b0300b0400b0500b0600b1000b1100b1200b1300b1400b1500b0101b0201g0301g0401a0501a0601a0701b0801b0901b1001a1101a1201a1301a1401a1501a1601b1701b0002b0102g0202g0302g0402a0502a0602a0702a0802a0902a1002a1102a1202a1302a1402a1502a1602a1702a1802b0003b0103b0203b0303b0403a0503a0603a0703a0803a0903a1003a1103a1203a1303a1403a1503a1603a1703a1803a1903b0404b0504b0604a0704a0804a0904a1004a1104a1204a1304a1404a1504a1604a1704a1804a1904b0605b0705a0805a0905a1005a1105a1205a1305a1405a1505a1605a1705a1805b0706b0806a0906a1006a1106a1206a1306a1406a1506a1606a1706a1806a1906b2006b2106b0807b0907b1007a1107a1207a1307a1407a1507a1607a1707a1807a1907a2007a2107a2207b2307b2407b2507b0908b1008a1108a1208a1308a1408a1508a1608a1708a1808a1908a2008a2108a2208a2308a2408a2508a2608b1009b1109a1209a1309a1409a1509b1609a1709b1809b1909b2009b2109a2209a2309a2409a2509a2609b1010b1110a1210a1310b1410b1510r1610b1710a1810b2110b2210b2310b2410b2510b2610b1011b1111b1211b1411b1511r1611b1711a1811b1512b1612b1712b1812b", "black3":"0700b0800b0900b1000b1100b1200b1300b0201b0301b0401b0501b0601b0701a0801a0901a1001a1101a1201a1301a1401b1501b0102b0202g0302g0402a0502a0602a0702a0802a0902a1002a1102a1202a1302a1402a1502a1602b0003b0103g0203g0303g0403a0503a0603a0703a0803a0903a1003a1103a1203a1303a1403a1503a1603a1703b0004b0104b0204b0304b0404a0504a0604a0704a0804a0904a1004a1104a1204a1304a1404a1504a1604a1704a1804b0405b0505b0605b0705b0805a0905a1005a1105a1205a1305a1405a1505a1605a1705a1805a1905b2005b0806b0906a1006a1106a1206a1306a1406a1506a1606a1706a1806a1906a2006a2106b2206b0807b0907a1007a1107a1207a1307a1407a1507a1607a1707b1807a1907a2007a2107a2207a2307b2407b0808b0908a1008a1108a1208a1308a1408a1508a1608a1708b1808b1908b2008a2108a2208a2308a2408a2508b0809b0909a1009a1109a1209a1309a1409a1509a1609a1709a1809b2009b2109b2209a2309a2409a2509b0910b1010a1110a1210a1310a1410a1510a1610a1710a1810b2210b2310b2410b2510b1011b1111a1211a1311a1411a1511a1611a1711b1811b1911b1012b1112a1212a1312a1412a1512a1612b1013b1113a1213a1313a1413a1513a1613b1014b1114a1214a1314b1414b1514b1015b1115b1215b", "black4":"0200b0300b0400b0500b0101b0201g0301g0401a0501a0601b0901b1001b1101b1201b1301b1401b0002b0102g0202g0302g0402a0502a0602a0702b0802b0902a1002a1102a1202a1302a1402a1502b1602b1702b0003b0103b0203b0303b0403a0503a0603a0703a0803a0903a1003a1103a1203a1303a1403a1503a1603a1703a1803b1903b0404b0504a0604a0704a0804a0904a1004a1104a1204a1304a1404a1504a1604a1704a1804a1904a2004b0505b0605a0705a0805a0905a1005a1105a1205a1305a1405a1505a1605a1705a1805a1905a2005b0606b0706a0806a0906a1006a1106a1206a1306a1406a1506a1606a1706a1806a1906b0707b0807a0907a1007a1107a1207a1307a1407a1507a1607a1707a1807a1907b2007b2107b2207b2307b2407b0808b0908b1008b1108a1208a1308a1408a1508a1608a1708a1808a1908a2008a2108a2208a2308a2408a2508b1109b1209b1309a1409a1509a1609a1709b1809a1909a2009a2109a2209a2309a2409a2509b1310b1410r1510b1610a1710b1810b1910b2010b2110b2210b2310b2410b2510b1411b1511r1611b1711a1811b1412b1512r1612b1712a1812b1513b1613b1713b1813b"} for i in range(0,len(sprites[sprite]),5): pixel=sprites[sprite][i:i+5] set_pixel(x+int(pixel[:2]),y+int(pixel[2:4]),COLORS[pixel[4]]) def inputs(): global bird_y,bird_x if kd(KEY_UP) and bird_y>5: bird_y-=65*(1/FPS) if kd(KEY_DOWN) and bird_y<SCREEN_H-5: bird_y+=65*(1/FPS) if kd(KEY_RIGHT) and bird_x<SCREEN_W-5: bird_x+=65*(1/FPS) if kd(KEY_LEFT) and bird_x>5: bird_x-=65*(1/FPS) def collisions(bird_x, bird_y, enemy_x, enemy_y, enemy_w, enemy_h): if ( bird_x<enemy_x+enemy_w and bird_x+BIRD_W>enemy_x and bird_y<enemy_y+enemy_h and bird_y+BIRD_H>enemy_y): return True return False def tick(): global state,hearts inputs() fill_rect(0,0,SCREEN_W,SCREEN_H,COLORS["bg"]) for heart in range(hearts): render(SCREEN_W-80+heart*15,10,"heart") draw_string(str(int(monotonic()-start)),8,7,"black",COLORS["bg"]) render(int(bird_x),int(bird_y),"bird"+str(state)) if randint(1,100)<3: heal.append({"x":SCREEN_W,"y":randint(5,SCREEN_H-25)}) print("A heart appears!") for heart in heal: if collisions(bird_x,bird_y,heart["x"],heart["y"],15,13): if hearts<5: hearts+=1 print("+1 hearts") heal.remove(heart) elif heart["x"]<=0: heal.remove(heart) else: heart["x"]-=10 render(heart["x"],heart["y"],"heart") if state<4: state+=1 else: state=1 if random()<0.05*(((monotonic()-start)+10)/10): enemies.append({"x":SCREEN_W-15,"y":randint(5,SCREEN_H-30),"speed":(3+(monotonic()-start)/8)*3*random(),"type":choice(["yellow","gray","black"]),"state":1}) for enemy in enemies: enemy["x"]-=enemy["speed"] render(int(enemy["x"]),int(enemy["y"]),str(enemy["type"])+str(enemy["state"])) if collisions(bird_x, bird_y, enemy["x"], enemy["y"], ENEMY_W, ENEMY_H): enemies.remove(enemy) state=5 hearts-=1 print("Hit! -1 hearts") elif enemy["x"]<0: enemies.remove(enemy) else: enemy["speed"]+=0.1 if enemy["state"]==4: enemy["state"]=1 else: enemy["state"]+=1 sleep(1/FPS) initialise() while True: if hearts>0: tick() else: draw_string("Game Over",int(SCREEN_W/2-45),int(SCREEN_H/2-24),"black",COLORS["bg"]) draw_string("you survived "+str(int(monotonic()-start))+" seconds",int(SCREEN_W/2-105-len(str(int(monotonic()-start)))*5),int(SCREEN_H/2),"black",COLORS["bg"]) print("you survived "+str(int(monotonic()-start))+" seconds") while not kd(KEY_OK): pass initialise()