Last active
November 5, 2022 17:34
-
-
Save loulou64490/5cef4899c037125e1d3ce03c2dfe2053 to your computer and use it in GitHub Desktop.
Game
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
from kandinsky import fill_rect as krect, draw_string as text | |
from ion import keydown | |
from random import randint | |
from time import * | |
SCREEN_W=320 | |
SCREEN_H=222 | |
GAME_W=200 | |
SCORE_OFFSET=5 | |
SCORE_W=100 | |
GAME_H=200 | |
OFFSET_X=(SCREEN_W-SCORE_W-GAME_W-SCORE_OFFSET)//2# game offset | |
OFFSET_Y=(SCREEN_H-GAME_H)//2 | |
KEY={"ok":4} | |
COLOR={ | |
"bg":(50,200,255), | |
"bird":(255,200,0), | |
"hurt":(255,50,0), | |
"jp":(150,150,150), | |
"mouth":(255,100,0), | |
"pipe":(100,255,20), | |
"line":(0,0,0), | |
"font1":(60,60,60), | |
"font2":(255,255,255)} | |
LINE_W=2 #line width | |
TARG_FPS=20 | |
TARG_SPF=1/TARG_FPS# targ second per frame | |
def sec(time): | |
return monotonic()-time | |
def rect(x,y,h_s,v_s,c): | |
krect(int(x),int(y),int(h_s),int(v_s),COLOR["line"]) | |
if h_s>2*LINE_W: krect(int(x)+LINE_W,int(y)+LINE_W,int(h_s)-2*LINE_W,int(v_s)-2*LINE_W,c) | |
def drawBird(x,y,s): | |
if sec(hurt_time)>1 or (sec(hurt_time)*5%2>0.5): | |
c=COLOR["bird"] | |
else: | |
c=COLOR["hurt"] | |
rect(x,y,s,s,c) | |
rect(x-s/2,y+s/8,s/2,s/2,COLOR["jp"]) | |
rect(x+s/2,y+s/2,s/1.5,s/3,COLOR["mouth"]) | |
rect(x+s/4,y+s/5,s/3,s/3,COLOR["font2"]) | |
o=s/2 if flap and int(sec(flap_time)*16)%2==0 else s/4 | |
rect(x-s/4,y+o,s/2,s/2,c) | |
if flap: | |
s2=min(s/2+o*2,OFFSET_Y+GAME_H-y-s/8*6) | |
rect(x-s/2,y+s/8*5,s/2,s2,COLOR["mouth"]) | |
krect(int(x-s/2+LINE_W*2),int(y+s/8*5.5),int(s/2-LINE_W*4),int(s2/2),COLOR["font2"]) | |
def drawPipe(i): | |
pipe=pipes[i] | |
x,y = min(max(pipe[0],OFFSET_X),OFFSET_X+GAME_W),pipe[1] | |
if x<=OFFSET_X: h_s = pipe[2]-(x-pipe[0]) | |
elif x>=OFFSET_X+GAME_W-pipe[2]: h_s = (GAME_W+OFFSET_X)-x | |
else: h_s=pipe[2] | |
v_s = pipe[3] | |
rect(x,y,h_s,v_s,COLOR["pipe"]) | |
def drawHeart(x,y,s,c): | |
heart=("01100","11110","01111","11110","01100") | |
for x2 in range(len(heart)): | |
for y2 in range(len(heart[x2])): | |
if int(heart[x2][y2]): krect(x+x2*s,y+y2*s,s,s,c) | |
def initScreen(): | |
rect(OFFSET_X,OFFSET_Y,GAME_W,GAME_H,COLOR["bg"]) | |
drawScorePannel() | |
actualizeScore() | |
actualizeLife() | |
def clearScreen(): | |
krect(OFFSET_X+LINE_W,OFFSET_Y+LINE_W,GAME_W-2*LINE_W,GAME_H-2*LINE_W,COLOR["bg"]) | |
def createPipe(x,y,h_s,v_s): | |
#h_s : horizontal size | |
#v_s : vertical size | |
pipe=[x,y,h_s,v_s] | |
pipes.append(pipe) | |
def addPipes(x,s): | |
space_size = GAME_H//2 - difficulty*2 | |
space_y= randint(OFFSET_Y,OFFSET_Y+GAME_H-space_size-20) | |
createPipe(x,OFFSET_Y,s,space_y) | |
createPipe(x,OFFSET_Y+space_size+space_y,s,GAME_H-(space_size+space_y)) | |
def hitBox(p1,p2): | |
x1=p1[0] | |
y1=p1[1] | |
hs1=p1[2] | |
vs1=p1[3] | |
x2=p2[0] | |
y2=p2[1] | |
hs2=p2[2] | |
vs2=p2[3] | |
if x1 <= x2+hs2 and x1+hs1 >= x2 and y1 <= y2+vs2 and y1+vs1 >= y2: | |
return True | |
else: | |
return False | |
def gameEngine(): | |
global bird, pipes, score, best_score, difficulty,flap,total_time,flap_time,fps,hurt_time,life | |
print(">initialisation: dead...") | |
difficulty=1 | |
life=3 | |
score=0 | |
best_score=readBestScore() | |
pipes=[] | |
pipe_size=50 | |
flap=0 | |
jump_speed=4 | |
max_speed=15 | |
fps=TARG_FPS | |
bird={ | |
"x":20, | |
"y":OFFSET_Y+GAME_H/2, | |
"spd_x":5, | |
"spd_y":-5, | |
"size":20, | |
"color":COLOR["bird"]} | |
addPipes(GAME_W+OFFSET_X,pipe_size) | |
addPipes(GAME_W/2-pipe_size/2+OFFSET_X,pipe_size) | |
initScreen() | |
spf=TARG_SPF | |
total_time=monotonic() | |
flap_time=monotonic() | |
hurt_time=monotonic()-1 | |
while not (life<1 and sec(hurt_time)>0.5): | |
if sec(spf)>=TARG_SPF: | |
#GAME DATA | |
fps=int(1/sec(spf)) | |
#print(fps) | |
spf=monotonic() | |
#PHYSICS | |
limits_y=[OFFSET_Y,OFFSET_Y+GAME_H] | |
bird["spd_y"]+=jump_speed/3 | |
bird["spd_y"]=max(-max_speed,min(max_speed,bird["spd_y"])) | |
bird["y"]+=bird["spd_y"] | |
if bird["y"]<=limits_y[0]: | |
bird["y"]=limits_y[0] | |
bird["spd_y"]=0 | |
if bird["y"]>=limits_y[1]-bird["size"]: | |
bird["y"]=limits_y[1]-bird["size"] | |
bird["spd_y"]=0 | |
if keydown(KEY["ok"]): | |
bird["spd_y"]-=abs(-max_speed-bird["spd_y"])/max_speed*jump_speed | |
if flap==0: | |
flap_time=monotonic() | |
flap=1 | |
else: flap=0 | |
remove_pipe=[] | |
last_speed=int(bird["spd_x"]) | |
for i in pipes: | |
i[0]-=last_speed | |
if hitBox((bird["x"],bird["y"],bird["size"],bird["size"]),i): | |
if sec(hurt_time)>1: | |
if life>0:hurt_time=monotonic() | |
life-=1 | |
difficulty+=randint(1,3) | |
actualizeLife() | |
print(">{}s:hurt! life:{}".format(int(sec(total_time)),life)) | |
if i[0]+i[2]<=OFFSET_X: | |
if i[1]!=OFFSET_Y: | |
bird["spd_x"]+=abs(max_speed-bird["spd_x"])/(4*max_speed) | |
score+=1 | |
best_score=max(score,best_score) | |
actualizeScore() | |
addPipes(GAME_W+OFFSET_X,pipe_size) | |
remove_pipe.append(i) | |
for i in remove_pipe: | |
pipes.remove(i) | |
#DISPLAY | |
clearScreen() | |
for i in range(len(pipes)): | |
drawPipe(i) | |
drawBird(bird["x"],bird["y"],bird["size"]) | |
print(">game end: dead...") | |
print(">fps : {}".format(fps)) | |
transition() | |
saveScore(best_score) | |
gameEngine() | |
def actualizeScore(): | |
x=OFFSET_X+GAME_W+SCORE_OFFSET+SCORE_W//2 | |
y=OFFSET_Y | |
c=COLOR["font2"] | |
text(str(score),x-len(str(score))*5,y+40,c,COLOR["bird"]) | |
text(str(best_score),x-len(str(best_score))*5,y+100,c,COLOR["bird"]) | |
def actualizeLife(): | |
x=OFFSET_X+GAME_W+SCORE_OFFSET+5 | |
y=OFFSET_Y+150 | |
for i in range(3): | |
if i>=life: c=COLOR["line"] | |
else: c=COLOR["hurt"] | |
drawHeart(x+i*30,y,5,c) | |
def drawScorePannel(): | |
x=OFFSET_X+GAME_W+SCORE_OFFSET | |
y=OFFSET_Y | |
rect(x,y,SCORE_W,GAME_H,COLOR["bird"]) | |
x=OFFSET_X+GAME_W+SCORE_W//2 | |
y=OFFSET_Y | |
text("SCORE",x-len("SCORE")*5,y+10,COLOR["font1"],COLOR["bird"]) | |
text("BEST",x-len("BEST")*5,y+70,COLOR["font1"],COLOR["bird"]) | |
def readBestScore(): | |
try: | |
file=open("jp_bird.sav","r") | |
best = file.readline() | |
file.close() | |
print(">score loaded !") | |
return int(best) | |
except: | |
print(">failed to read the score...") | |
return 0 | |
def saveScore(score): | |
try : | |
file=open("jp_bird.sav","w") | |
file.truncate(0) | |
file.write(str(score)) | |
file.close() | |
print(">score saved !") | |
except: | |
print(">failed to save the score...") | |
def transition(): | |
for c in [COLOR["font1"],COLOR["bg"]]: | |
for y in range(OFFSET_Y,OFFSET_Y+GAME_H,10): | |
sleep(0.016) | |
krect(OFFSET_X,y,GAME_W,10,c) | |
gameEngine() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from math import * | |
from ion import keydown | |
from kandinsky import fill_rect as drawRect, draw_string as drawTxt | |
from time import * | |
from random import randint,random | |
SCREEN_SIZE=(320,222) | |
LOGO=(29694911221719259620815,19) | |
NUMB=(31599,18740,29671,31207,18925,31183,31695,18727,31727,31215) | |
PARTY=(20266236847757577779063,19) | |
ENDED=(521406024322335892760215,20) | |
Mode,Diff,MaxPts,BallSpd,BallDetails,PadDetails,Col1,Col2,Col3,BgCol,Theme,Best=0,1,2,3,4,5,6,7,8,9,10,11 | |
X,Y,SpdX,SpdY,SizeX,SizeY,PreX,PreY,Col=0,1,2,3,4,5,6,7,8 | |
it="self" | |
base_conf=[0,1,1,3,0,0,(255,255,255),(255,200,0),(100,100,100),(60,60,60),0,0] | |
def kd(x): | |
if keydown(x): | |
while keydown(x):pass | |
return True | |
return False | |
def menu(x,y,elements,col=(0,0,0),bg_col=(255,255,255)): | |
kd(4) | |
el_size,select,txt_size,draw=25,0,[0 for i in range(len(elements))],1 | |
while True: | |
if kd(1): | |
draw=1 | |
select=max(0,select-1) | |
if kd(2): | |
draw=1 | |
select=min(len(elements)-1,select+1) | |
if kd(0): | |
draw=1 | |
type=elements[select][0] | |
if type=="sld":elements[select][-1]=max(elements[select][-1]-1,elements[select][2][0]) | |
if type=="lst":elements[select][-1]=max(elements[select][-1]-1,0) | |
if kd(3): | |
draw=1 | |
type=elements[select][0] | |
if type=="sld":elements[select][-1]=min(elements[select][-1]+1,elements[select][2][1]) | |
if type=="lst":elements[select][-1]=min(elements[select][-1]+1,len(elements[select][2])-1) | |
if kd(4) and elements[select][0]=="btn": | |
break | |
if draw: | |
for nb,el in enumerate(elements): | |
drawRect(x,y+el_size*nb,10*txt_size[nb],el_size,bg_col) | |
slcted=1 if nb==select else 0 | |
type=el[0] | |
name=el[1] | |
val=el[-1] | |
if type=="btn":disp_txt=name | |
if type=="sld":disp_txt=name+" : {}".format(val) | |
if type=="lst":disp_txt=name+" : {}".format(el[2][val]) | |
if slcted: disp_txt="> "+disp_txt | |
txt_size[nb]=len(disp_txt) | |
drawTxt(disp_txt,x,y+nb*el_size,col,bg_col) | |
draw=0 | |
return elements[select][1],{x[1]:x[-1] for x in elements if x[0]!="btn"} | |
def transition(conf=base_conf): | |
saveConf(conf) | |
c=8 | |
for col in (conf[Col3],conf[BgCol]): | |
for i in range(c-3,c+1): | |
for x in range(0,SCREEN_SIZE[0],c): | |
for y in range(0,SCREEN_SIZE[1],c): | |
drawRect(x,y,i,i,col) | |
drawRect(x+c//2,y+c//2,i,i,col) | |
def resetMenu(conf): | |
transition(conf) | |
drawTxt("Reset the Game ?",10,10,conf[Col1],conf[BgCol]) | |
if menu(10,40,[["btn","Yes"],["btn","No"]],conf[Col1],conf[BgCol])[0]=="Yes":resetGame() | |
else:gameMenu(conf) | |
def drawNuber(n,x,y,s,col): | |
for i,j in enumerate(str(n)): | |
drawSprite([NUMB[int(j)],3],x+s*4*i,y,s,col) | |
def drawSprite(sprite,x1,y1,s,col): | |
img=sprite[0] | |
row=sprite[1] | |
for i in range(len(bin(img))-2): | |
if img>>i & 1: drawRect(x1+(i%row)*s,y1+(i//row)*s,s,s,col) | |
def mainMenu(conf=base_conf): | |
transition(conf) | |
drawSprite(LOGO,10,10,12,conf[Col2]) | |
drawRect(10,80,300,142,conf[Col3]) | |
act_el=[["btn","Play"],["lst","Mode",("Solo","2 Player","Vs Comp."),conf[Mode]],["btn","Game Options"],["btn","Graphics Options"]] | |
get_act=menu(20,100,act_el,conf[Col1],conf[Col3]) | |
conf[Mode]=get_act[1]["Mode"] | |
if get_act[0]=="Game Options":gameMenu(conf) | |
elif get_act[0]=="Graphics Options":graphMenu(conf) | |
else:gameEngine(conf) | |
def gameMenu(conf=base_conf): | |
transition(conf) | |
drawTxt("GAME MENU",10,10,conf[Col2],conf[BgCol]) | |
drawRect(10,30,300,5,conf[Col3]) | |
act_el=[["sld","Max Points",(1,21),conf[MaxPts]],["sld","Ball Speed",(1,10),conf[BallSpd]],["sld","Difficulty",(1,10),conf[Diff]],["btn","Graphics Options"],["btn","Done"],["btn","Reset Game"]] | |
get_act=menu(20,50,act_el,conf[Col1],conf[BgCol]) | |
conf[MaxPts]=get_act[1]["Max Points"] | |
conf[BallSpd]=get_act[1]["Ball Speed"] | |
conf[Diff]=get_act[1]["Difficulty"] | |
if get_act[0]=="Done":mainMenu(conf) | |
elif get_act[0]=="Reset Game":resetMenu(conf) | |
else:graphMenu(conf) | |
def resetGame(): | |
conf=base_conf | |
saveConf(conf) | |
print("Game reset") | |
mainMenu(conf) | |
def graphMenu(conf=base_conf): | |
transition(conf) | |
drawTxt("GRAPHICS MENU",10,10,conf[Col2],conf[BgCol]) | |
drawRect(10,30,300,5,conf[Col3]) | |
act_el=[["lst","Ball Details",("No","Yes"),conf[BallDetails]],["lst","Pad Details",("No","Yes"),conf[PadDetails]],["lst","Theme",("Dark","Light","Omega","NsiOs"),conf[Theme]],["btn","Game Options"],["btn","Done"],["btn","Apply"]] | |
get_act=menu(20,50,act_el,conf[Col1],conf[BgCol]) | |
conf[BallDetails]=get_act[1]["Ball Details"] | |
conf[PadDetails]=get_act[1]["Pad Details"] | |
conf=setTheme(conf,get_act[1]["Theme"]) | |
if get_act[0]=="Done":mainMenu(conf) | |
if get_act[0]=="Apply":graphMenu(conf) | |
else:gameMenu(conf) | |
def setTheme(conf,nb): | |
conf[Theme]=nb | |
a,b,c,d=(255,255,255),(255,200,0),(100,100,100),(60,60,60) | |
if nb==1:a,b,c,d=d,(200,150,60),(200,200,200),a | |
elif nb==2:b=(220,50,50) | |
elif nb==3:b=(200,100,200) | |
conf[Col1:BgCol+1]=a,b,c,d | |
return conf | |
def saveConf(conf): | |
try : | |
with open("pong.conf","w") as f: | |
f.truncate(0) | |
f.write(str(conf)) | |
except: print("Saving configuration failed.") | |
def loadConf(): | |
try: | |
with open("pong.conf","r") as f:return eval(f.readline()) | |
except: | |
print("Loading configuration failed.") | |
return base_conf | |
def vec(s,a): | |
a=radians(a) | |
x=s*cos(a) | |
y=s*sin(a) | |
return x,y | |
def simp(a):return a%360 | |
def collide(a1,a2):return a2-simp(a1-a2) | |
class Entity(): | |
def __init__(it,x,y,w,h,col,bg_col): | |
it.x,it.y,it.w,it.h,it.col,it.bg_col=x,y,w,h,col,bg_col | |
it.spd_x,it.spd_y=0,0 | |
it.last_draw=(int(it.x-it.w//2),int(it.y-it.h//2),int(it.w),int(it.h),it.bg_col) | |
def hitBox(it,it2): | |
if it.x-it.w//2<it2.x+it2.w//2 and it.x+it.w//2>it2.x-it2.w//2 and it.y-it.h//2<it2.y+it2.h//2 and it.x+it.w>it2.x and it.y<it2.y+it2.h//2 and it.y+it.h//2>it2.y-it2.h//2:return True | |
else: return False | |
def applyVec(it): | |
it.x+=it.spd_x | |
it.y+=it.spd_y | |
def hideObj(it):drawRect(*it.last_draw) | |
def drawObj(it,detail=0): | |
it.last_draw=[int(it.x-it.w//2),int(it.y-it.h//2),int(it.w),int(it.h),it.bg_col] | |
if detail: | |
for x2,y2 in zip((2,0),(0,2)):drawRect(int(it.x-it.w//2)+x2,int(it.y-it.h//2)+y2,int(it.w)-x2*2,int(it.h)-y2*2,it.col) | |
else: drawRect(*it.last_draw[0:4]+[it.col]) | |
it.pre_x,it.pre_y=it.x,it.y | |
class Particule(Entity): | |
def __init__(it,x,y,s,col,bg_col,spd,a): | |
super().__init__(x,y,s,s,col,bg_col) | |
it.spd_x,it.spd_y=vec(spd,simp(randint(-90,90)+a)) | |
def playFrame(it): | |
it.hideObj() | |
it.spd_y+=random() | |
it.applyVec() | |
it.drawObj() | |
def s(t):return monotonic()-t | |
def addParticules(nb,*part_info): | |
lst=[] | |
for i in range(nb):lst.append(Particule(*part_info)) | |
return lst | |
def gameEngine(conf=base_conf): | |
def pause(conf): | |
kd(8) | |
drawTxt("Paused",SCREEN_SIZE[0]//2-30,SCREEN_SIZE[1]//2-10,conf[Col1],conf[BgCol]) | |
while not kd(8):pass | |
drawRect(SCREEN_SIZE[0]//2-30,SCREEN_SIZE[1]//2-10,100,20,conf[BgCol]) | |
def error(n,t):return n*(randint(-t,t)/100+1) | |
def resetScreen():drawRect(0,0,SCREEN_SIZE[0],SCREEN_SIZE[1],conf[BgCol]) | |
total_pts,pts,bounces,pad_size,ball_size,ball_spd,diff=0,[0,0],0,50,10,conf[BallSpd],conf[Diff] | |
spf,targ_spf,frame_nb=monotonic(),0.016,0 | |
pad1,pad2=Entity(10,SCREEN_SIZE[1]//2,ball_size,pad_size,conf[Col1],conf[BgCol]),Entity(SCREEN_SIZE[0]-10,SCREEN_SIZE[1]//2,ball_size,pad_size,conf[Col1],conf[BgCol]) | |
ball=Entity(SCREEN_SIZE[0]//2,SCREEN_SIZE[1]//2,ball_size,ball_size,conf[Col2],conf[BgCol]) | |
line=Entity(SCREEN_SIZE[0]//2,SCREEN_SIZE[1]//2,ball_size,SCREEN_SIZE[1],conf[Col3],conf[BgCol]) | |
ball.a=0 | |
particules,delete=[],[] | |
resetScreen() | |
while total_pts<conf[MaxPts]: | |
frame_nb+=1 | |
if frame_nb%5==0: | |
line.drawObj() | |
if conf[Mode]==0:drawNuber(bounces,10,10,10,conf[Col3]) | |
else: | |
for x,n in zip((20,SCREEN_SIZE[0]//2+20),pts):drawNuber(n,x,10,10,conf[Col3]) | |
ball.drawObj(conf[BallDetails]) | |
for pad in (pad1,pad2): | |
pad.hideObj() | |
pad.drawObj(conf[PadDetails]) | |
pad.applyVec() | |
pad.y=max(pad.h/2,min(SCREEN_SIZE[1]-pad.h/2,pad.y)) | |
pad.spd_y/=1.1 | |
if ball.hitBox(pad): | |
ball.a=collide(ball.a,simp(90-randint(-1,1)*diff)+10*(ball.y-pad.y)/pad.h) | |
bounces+=1 | |
if conf[BallDetails]:particules+=addParticules(5,ball.x,ball.y,2,conf[Col2],conf[BgCol],7,ball.a) | |
if conf[Mode]==0:drawRect(10,10,120,50,conf[BgCol]) | |
ball.x=pad.x-copysign(ball.w,pad.x-SCREEN_SIZE[0]//2) | |
ball.spd_x,ball.spd_y=vec(ball_spd,ball.a) | |
ball.a=simp(ball.a) | |
ball.applyVec() | |
if keydown(8):pause(conf) | |
if keydown(1):pad1.spd_y-=1 | |
if keydown(2):pad1.spd_y+=1 | |
if conf[Mode]==0: | |
pad2.y=pad1.y | |
elif conf[Mode]==1: | |
if keydown(39):pad2.spd_y-=1 | |
if keydown(45):pad2.spd_y+=1 | |
else: | |
pad2.spd_y+=max(min(0.5,(error(ball.y,50-diff*2)-pad2.y)/10),-1) | |
if ball.y-ball.h/2<=0 or ball.y+ball.h/2>=SCREEN_SIZE[1]: | |
ball.a=collide(ball.a,0) | |
if conf[BallDetails]:particules+=addParticules(5,ball.x,ball.y,2,conf[Col2],conf[BgCol],7,ball.a) | |
if ball.x<0 or ball.x>SCREEN_SIZE[0]: | |
resetScreen() | |
if conf[BallDetails]:particules+=addParticules(20,ball.x,ball.y,2,conf[Col2],conf[BgCol],7,270) | |
if conf[Mode]==0:break | |
else: | |
p=0 if ball.x>SCREEN_SIZE[0]//2 else 1 | |
pts[p]+=1 | |
total_pts+=1 | |
ball.x=SCREEN_SIZE[0]//2 | |
if frame_nb%4: | |
for part in particules: | |
part.playFrame() | |
if part.y>=SCREEN_SIZE[1]: | |
part.hideObj() | |
delete.append(part) | |
for i in delete:particules.remove(i) | |
delete=[] | |
while s(spf)<targ_spf:pass | |
spf=monotonic() | |
ball.hideObj() | |
for pad in (pad1,pad2):pad.hideObj() | |
if bounces>conf[Best]:conf[Best]=bounces | |
gameFinish(conf,pts,bounces) | |
def gameFinish(conf,pts,bounces): | |
transition(conf) | |
drawSprite(PARTY,90,10,8,conf[Col1]) | |
drawSprite(ENDED,125,50,4,conf[Col2]) | |
x,y=SCREEN_SIZE[0]//2,80 | |
if conf[Mode]==0: | |
txt="Score: {} | Best: {}".format(bounces,conf[Best]) | |
else : | |
if pts[0]==pts[1]:txt="Equality" | |
elif pts[1]>pts[0]: | |
if conf[Mode]==2:txt="Computer won" | |
else:txt="Player 2 won" | |
else:txt="Player 1 won" | |
txt+=" | {}-{}".format(*pts) | |
drawRect(0,80,SCREEN_SIZE[0],20,conf[Col3]) | |
drawTxt(txt,x-len(txt)*5,y,conf[Col1],conf[Col3]) | |
if menu(10,110,[("btn","Play Again"),("btn","Finish")],conf[Col1],conf[BgCol])[0]=="Play Again":gameEngine(conf) | |
else:mainMenu(conf) | |
mainMenu(loadConf()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Version 1.7 STABLE | |
#Tip: You should try to press | |
#some keys in the menu... | |
from random import * | |
from kandinsky import * | |
from ion import * | |
from time import * | |
#from pomme import * | |
def oscolor(): | |
try: | |
get_keys() | |
except: | |
return 'orange' | |
else: | |
return 'red' | |
def lastPos(i,x,y): | |
if i[-1]==3: | |
pos=[x-10,y] | |
elif i[-1]==2: | |
pos=[x,y-10] | |
elif i[-1]==0: | |
pos=[x+10,y] | |
elif i[-1]==1: | |
pos=[x,y+10] | |
pos[0],pos[1]=checkTeleport(pos[0],pos[1]) | |
return pos | |
def newApple(appleC,bgC): | |
applex=randint(0,31)*10+4 | |
appley=randint(0,21)*10+5 | |
while get_pixel(applex,appley)!=bgC: | |
applex=randint(0,31)*10+4 | |
appley=randint(0,21)*10+5 | |
fill_rect(applex-4,appley-4,10,10,appleC) | |
return applex,appley | |
def checkTeleport(x,y): | |
if x<4: | |
x=314 | |
if x>314: | |
x=4 | |
if y<5: | |
y=215 | |
if y>215: | |
y=5 | |
return x,y | |
def getMove(u): | |
for k in range(4): | |
if keydown(k)==True and u+k!=3: return k | |
return u | |
def clearDraw(): fill_rect(0,0,320,222,(255,255,255)) | |
def clearHome(): print("\n \n \n \n \n \n \n \n \n \n \n \n \n ") | |
def redraw(): | |
draw_string("(DELETE to exit)",0,0) | |
printLetter([1,1,1,1,0,0,1,1,1,0,0,1,1,1,1],70,80,10,(0,204,0)) | |
fill_rect(95,80,2,4,(0,0,0)) | |
fill_rect(95,86,2,4,(0,0,0)) | |
fill_rect(100,84,4,2,(255,0,0)) | |
fill_rect(104,82,2,2,(255,0,0)) | |
fill_rect(104,86,2,2,(255,0,0)) | |
printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) | |
printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) | |
printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) | |
printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) | |
def printLetter(letter,x,y,size,color): | |
for yi in range(5): | |
for xi in range(3): | |
if letter[yi*3+xi]==1: | |
fill_rect(x+(xi*size),y+(yi*size),size,size,color) | |
def menu(): | |
clearDraw() | |
printLetter([1,1,1,1,0,1,1,0,1,1,0,1,1,0,1],110,80,10,(0,0,0)) | |
printLetter([1,1,1,1,0,1,1,1,1,1,0,1,1,0,1],150,80,10,(0,0,0)) | |
printLetter([1,0,1,1,0,1,1,1,0,1,0,1,1,0,1],190,80,10,(0,0,0)) | |
printLetter([1,1,1,1,0,0,1,1,1,1,0,0,1,1,1],230,80,10,(0,0,0)) | |
anim=[1,1,1,1,1,1,1,1,1,4,4,3,3,4,4,1,1] | |
ax=0 | |
ay=120 | |
aendx=-110 | |
aendy=120 | |
u=1 | |
aback=0 | |
for i in range(len(anim)): | |
ax=ax+((anim[i]==1)-(anim[i]==3))*10 | |
ay=ay+((anim[i]==2)-(anim[i]==4))*10 | |
if aendx<0: | |
aendx=aendx+10 | |
else: | |
aendx=aendx+((anim[i-11]==1)-(anim[i-11]==3))*10 | |
aendy=aendy+((anim[i-11]==2)-(anim[i-11]==4))*10 | |
fill_rect(aendx,aendy,10,10,(255,255,255)) | |
fill_rect(ax,ay,10,10,(0,204,0)) | |
# aback=lastPos(anim,ax,ay) | |
# if u==26 or u==24: | |
# fill_rect(ax-1,ay-1,3,1,(0,0,0)) | |
# fill_rect(ax-1,ay+1,3,1,(0,0,0)) | |
# fill_rect(aback[0],aback[1],10,10,(0,204,0)) | |
# elif u==34 or u==25: | |
# fill_rect(ax-1,ay-1,1,3,(0,0,0)) | |
# fill_rect(ax+1,ay-1,1,3,(0,0,0)) | |
# fill_rect(aback[0]-2,aback[1]-2,5,5,(0,204,0)) | |
sleep(0.05) | |
fill_rect(ax+5,ay,2,4,(0,0,0)) | |
fill_rect(ax+5,ay+6,2,4,(0,0,0)) | |
fill_rect(ax+10,ay+4,4,2,(255,0,0)) | |
fill_rect(ax+14,ay+2,2,2,(255,0,0)) | |
fill_rect(ax+14,ay+6,2,2,(255,0,0)) | |
draw_string("(DELETE to exit)",0,0) | |
draw_string("> Play <",125,140,oscolor()) | |
draw_string(" Options ",110,165) | |
darkMode=0 | |
Speed=0.05 | |
power=5 | |
score=1 | |
exit=0 | |
sel=1 | |
while keydown(KEY_OK)!=True and exit==0: | |
if keydown(KEY_DOWN) and sel==1: | |
draw_string(" Play ",125,140) | |
draw_string("> Options <",110,165,oscolor()) | |
sel=2 | |
elif keydown(KEY_UP) and sel==2: | |
draw_string("> Play <",125,140,oscolor()) | |
draw_string(" Options ",110,165) | |
sel=1 | |
if keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS): | |
draw_string("Dark mode enabled !",80,195) | |
darkMode=1 | |
if keydown(KEY_BACKSPACE): | |
exit=1 | |
sleep(0.1) | |
if sel==2 and exit!=1: | |
fill_rect(0,130,300,60,(255,255,255)) | |
Speed=0.05 | |
power=5 | |
score=1 | |
draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') | |
draw_string("Power:+"+str(power),200,140) | |
draw_string("Score:+"+str(score),50,170) | |
draw_string("Play",220,170) | |
sel=1 | |
sleep(0.2) | |
while keydown(KEY_OK)!=True or sel!=4: | |
if keydown(KEY_RIGHT): | |
sel=sel+1 | |
elif keydown(KEY_DOWN): | |
sel=sel+2 | |
elif keydown(KEY_LEFT): | |
sel=sel-1 | |
elif keydown(KEY_UP): | |
sel=sel-2 | |
if sel<0: | |
sel=0 | |
if sel>4: | |
sel=4 | |
if sel==1: | |
draw_string("Speed:"+str(Speed),50,140,oscolor(),'white') | |
draw_string("Power:+"+str(power),200,140) | |
draw_string("Score:+"+str(score),50,170) | |
draw_string("Play",220,170) | |
if keydown(KEY_OK): | |
clearHome() | |
Speed=input("Speed:") | |
redraw() | |
elif sel==2: | |
draw_string("Speed:"+str(Speed),50,140) | |
draw_string("Power:+"+str(power),200,140,oscolor(),'white') | |
draw_string("Score:+"+str(score),50,170) | |
draw_string("Play",220,170) | |
if keydown(KEY_OK): | |
clearHome() | |
power=int(input("Power:+")) | |
redraw() | |
elif sel==3: | |
draw_string("Speed:"+str(Speed),50,140) | |
draw_string("Power:+"+str(power),200,140) | |
draw_string("Score:+"+str(score),50,170,oscolor(),'white') | |
draw_string("Play",220,170) | |
if keydown(KEY_OK): | |
clearHome() | |
score=int(input("Score:")) | |
redraw() | |
elif sel==4: | |
draw_string("Speed:"+str(Speed),50,140) | |
draw_string("Power:+"+str(power),200,140) | |
draw_string("Score:+"+str(score),50,170) | |
draw_string("Play",220,170,oscolor(),'white') | |
if (keydown(KEY_LEFTPARENTHESIS) and keydown(KEY_RIGHTPARENTHESIS)) or darkMode==1: | |
draw_string("Dark mode enabled !",80,195) | |
darkMode=1 | |
if keydown(KEY_BACKSPACE): | |
exit=1 | |
break | |
sleep(0.1) | |
if exit!=1: | |
if darkMode==1: | |
launch(1,Speed,power,score) | |
elif darkMode==0: | |
launch(0,Speed,power,score) | |
elif exit==1: | |
clearDraw() | |
return | |
def launch(darkmode=0,speed=0.05,applePower=5,appleScore=1): | |
bgC=(248,252,248) | |
borderC=(0,0,0) | |
snakeC=(0,204,0) | |
appleC=(248,0,0) | |
if darkmode==1: | |
bgC=(0,0,0) | |
borderC=(0,0,204) | |
fill_rect(0,0,320,222,bgC) | |
# fill_rect(315,0,5,222,borderC) | |
# fill_rect(0,0,5,222,borderC) | |
# fill_rect(0,0,320,1,(197,52,49)) | |
fill_rect(0,221,320,1,(0,0,0)) | |
try: | |
get_keys() | |
except: | |
fill_rect(0,0,320,1,(255,181,49)) | |
else: | |
fill_rect(0,0,320,1,(197,52,49)) | |
snake=[3,3,3,3,3] | |
x=154 | |
y=115 | |
endx=104 | |
endy=115 | |
u,v=3,3 | |
length=5 | |
applex,appley=newApple(appleC,bgC) | |
score,touched=0,0 | |
while touched!=borderC and touched!=snakeC: | |
if keydown(0) or keydown(1) or keydown(2) or keydown(3): | |
u=getMove(u) | |
if keydown(KEY_BACKSPACE): | |
while keydown(KEY_BACKSPACE): | |
sleep(0.1) | |
while keydown(KEY_BACKSPACE)!=True: | |
sleep(0.1) | |
while keydown(KEY_BACKSPACE): | |
sleep(0.1) | |
snake.append(u) | |
if x==applex and y==appley: | |
length=length+float(applePower) | |
applex,appley=newApple(appleC,bgC) | |
score=score+int(appleScore) | |
x=x+((u==3)-(u==0))*10 | |
y=y+((u==2)-(u==1))*10 | |
x,y=checkTeleport(x,y) | |
if length: | |
length=length-1 | |
else: | |
snake.remove(snake[0]) | |
endx=endx+((v==3)-(v==0))*10 | |
endy=endy+((v==2)-(v==1))*10 | |
endx,endy=checkTeleport(endx,endy) | |
v=snake[0] | |
fill_rect(endx-4,endy-4,10,10,bgC) | |
touched=get_pixel(x,y) | |
if x<0 or x>320 or y<0 or y>220: | |
touched=borderC | |
if touched!=appleC and touched!=bgC: | |
touched=borderC | |
fill_rect(x-4,y-4,10,10,snakeC) | |
back=lastPos(snake,x,y) | |
if u==3 or u==0: | |
fill_rect(x,y-4,2,4,(0,0,0)) | |
fill_rect(x,y+2,2,4,(0,0,0)) | |
fill_rect(back[0]-4,back[1]-4,10,10,snakeC) | |
elif u==2 or u==1: | |
fill_rect(x-4,y,4,2,(0,0,0)) | |
fill_rect(x+2,y,4,2,(0,0,0)) | |
fill_rect(back[0]-4,back[1]-4,10,10,snakeC) | |
sleep(float(speed)) | |
# EPILEPSY WARNING !!! | |
# snakeC=(randint(0,255),randint(0,255),randint(0,255)) | |
while snakeC==appleC or snakeC==bgC: | |
snakeC=(randint(0,255),randint(0,255),randint(0,255)) | |
# beau() | |
if len(snake)==640: | |
if darkmode==1: | |
draw_string("You win !",120,100,'white','black') | |
draw_string("(You reached the max length)",20,120,'white','black') | |
else: | |
draw_string("You win !",120,100) | |
draw_string("(You reached the max length)",20,120) | |
touched=borderC | |
if darkmode==1: | |
draw_string("Score:"+str(score),10,10,'white','black') | |
draw_string("(OK=play again, DELETE=Menu)",10,30,'white','black') | |
else: | |
draw_string("Score:"+str(score),10,10) | |
draw_string("(OK=play again, DELETE=Menu)",10,30) | |
choice=0 | |
while choice==0: | |
if keydown(KEY_OK): | |
choice=1 | |
launch(darkmode,speed,applePower,appleScore) | |
elif keydown(KEY_BACKSPACE): | |
choice=2 | |
menu() | |
print("Score:",score) | |
menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment