# Treasure v1 NW 17/10/2020
# https://nsi.xyz/treasure
# par Robert Vincent (cent20)
frommathimportsqrtfromkandinskyimport*fromionimport*fromrandomimportrandintfromtimeimportsleepposition=[2,2]mvt=[2,2]score=666tresor=[0,0]defwait(buttons=(0,1,2,3,4,5,52)):# Attends qu'une des touches précisées soit pressée
whileTrue:foriinbuttons:ifkeydown(i):whilekeydown(i):True# Fonction anti-rebond
returnideftexte_center(texte,x=160,y=110,col1=(255,255,255),col2=(255,255,255)):'''Cette fonction affiche un texte et le centre'''draw_string(texte,x-len(texte)*10//2,y,col1,col2)defcol_os(test=0):'''Coloration de l'interface, respecte le thème Omega ou Epsilon
test = 1 pour utilisation dans get_pixel (debug)'''try:get_keys()return (192,53-1*test,53-5*test)except:return (255-7*test,183-3*test,52-4*test)defpersonnage(aff=0):'''Affiche ou efface le personnage'''ifaff==0:fill_rect(position[0],position[1],10,10,(255,255,255))else:fill_rect(position[0],position[1],10,10,col_os())fill_rect(position[0]+3,position[1]+3,1,2,(255,255,255))fill_rect(position[0]+6,position[1]+3,1,2,(255,255,255))fill_rect(position[0]+3,position[1]+7,4,1,(255,255,255))fill_rect(position[0]+2,position[1]+6,1,1,(255,255,255))fill_rect(position[0]+7,position[1]+6,1,1,(255,255,255))# On peut d'ailleurs faire une vrai dessin
defstart():'''Lancement du jeu. Texte introductif, génération des obstacles'''globaltresortexte_center("A treasure has been hidden",160,12,col_os())texte_center("Move around to look for it,",160,42,(120,120,120))texte_center("avoid rocks and holes,",160,62,(120,120,120))texte_center("Press OK to dig a hole",160,82,(120,120,120))texte_center("Press on an arrow",160,122,col_os())wait()fill_rect(1,1,320,204,(255,255,255))foriinrange(32):# On place 32 obstacles
fill_rect(2+10*randint(2,29),2+10*randint(2,18),10,10,(42,42,42))foriinrange(16):# On place 16 trous
x=randint(2,29)y=randint(2,18)forjinrange(4):fill_rect(2+10*x+j,2+10*y+j,10-2*j,10-2*j,(196-j*32,196-j*32,196-j*32))whiletresor==[0,0]:x=2+10*randint(2,29)y=2+10*randint(2,18)ifget_pixel(x,y)==(248,252,248):tresor=[x,y]personnage(1)definfo(penalite=0,init=0):globalscorescore-=penaliteifinit==1:fill_rect(1,1,320,204,(255,255,255))fill_rect(0,204,320,20,col_os())texte_center("nsi.xyz/treasure par cent20 ",160,184,col_os())texte_center("x",20,204,(255,255,255),col_os())texte_center("y",50,204,(255,255,255),col_os())texte_center("score",100,204,(255,255,255),col_os())texte_center("event",160,204,(255,255,255),col_os())texte_center("distance",250,204,(255,255,255),col_os())else:fill_rect(0,204,320,20,col_os())texte_center(str(position[0]//10),20,204,(255,255,255),col_os())texte_center(str(position[1]//10),50,204,(255,255,255),col_os())texte_center(str(score),100,204,(255,255,255),col_os())# Afficher le score par ex
defcollision():'''on vérifie les colisions, si colisions, on retourne False, sinon True
On teste mvt[0] et mvt[1]
'''# Rocher
ifget_pixel(mvt[0],mvt[1])==(40,40,40):info(10)texte_center("rock",160,204,(255,255,255),col_os())returnFalse# Trou
elifget_pixel(mvt[0],mvt[1])==(192,196,192):info(32)texte_center("hole",160,204,(255,255,255),col_os())returnFalse# texte_center(str(get_pixel(mvt[0], mvt[1])), 80, 2, (120,120,120))
returnTruedefgame():'''gère les actions sur les touches'''globalscore,position,mvtbordure=(2,2,192,302)# x min, x max, y min, y max
win=Falsecreuse=Falsewhilenotkeydown(5)andnotwin:is_key_pressed=Falseifkeydown(1):# fleche haut
mvt[1]=max(position[1]-10,bordure[1])mvt[0]=position[0]is_key_pressed=Truesleep(0.120)elifkeydown(2):# fleche bas
mvt[1]=min(position[1]+10,bordure[2])mvt[0]=position[0]is_key_pressed=Truesleep(0.120)elifkeydown(3):# fleche Droite
mvt[0]=min(position[0]+10,bordure[3])mvt[1]=position[1]is_key_pressed=Truesleep(0.120)elifkeydown(0):# fleche Gauche
mvt[0]=max(position[0]-10,bordure[0])mvt[1]=position[1]is_key_pressed=Truesleep(0.120)elifkeydown(4)orkeydown(52):# Touche OK
ifposition[0]==tresor[0]andposition[1]==tresor[1]:texte_center("win !",160,204,(255,255,255),col_os())win=Trueelse:texte_center("nothing",160,204,(255,255,255),col_os())info(32)distance=int(sqrt((position[0]//10-tresor[0]//10)**2+(position[1]//10-tresor[1]//10)**2))texte_center(str(bin(distance)[2:]),250,204,(255,255,255),col_os())sleep(0.666)creuse=Truesleep(0.120)ifis_key_pressedandcollision():personnage(0)ifcreuse==True:forjinrange(4):fill_rect(position[0]+j,position[1]+j,10-2*j,10-2*j,(196-j*32,196-j*32,196-j*32))creuse=Falseposition[0]=mvt[0]position[1]=mvt[1]personnage(1)info(1)fill_rect(80,60,160,80,col_os())texte_center("You have won",160,80,(255,255,255),col_os())texte_center("score = "+str(score),160,105,(255,255,255),col_os())defplay(nb):globalposition,mvt,score,tresorwhilenb>0:position=[2,2]mvt=[2,2]score=666tresor=[0,0]info(0,1)start()wait()info()game()nb-=1wait()play(1)
During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:
Ensure the proper functioning of the site (essential cookies); and
Track your browsing to send you personalized communications if you have created a professional account on the site and can be contacted (audience measurement cookies).
With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.