Skip to content

Instantly share code, notes, and snippets.

@melon-husk
Last active November 29, 2021 08:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melon-husk/9c3a193d093782bfe6f3b698187e1b9c to your computer and use it in GitHub Desktop.
Save melon-husk/9c3a193d093782bfe6f3b698187e1b9c to your computer and use it in GitHub Desktop.
calculator_python
from math import *
from kandinsky import *
from random import *
from time import *
font_x=300
font_clock=0
text="Around the world"
rects_clock=0
rects_x=0
rects_y=222
rects_w=1
rects_h=40
rects_c="white"
bg=(randint(0,55),randint(0,55),randint(0,55))
R2=(randint(120,255),randint(120,255),randint(120,255))
fill_rect(0,0,322,222,bg)
rects_x=(range(1,322,10))
colors=(randint(0,255),randint(0,255),randint(0,255))
rects_h=randint(-100,1)
for i in rects_x:
rects_h=randint(-60,-10)
fill_rect(i,222,6,rects_h,R2)
while 1:
font_clock+=1
if font_clock>20:
font_clock=0
font_x-=1
if font_x+len(text)*9 < -20:font_x=321
draw_string(text,font_x,80,R2,bg)
rects_clock+=1
if rects_clock>400:
fill_rect(0,0,322,222,bg)
for i in rects_x:
rects_h=randint(-60,-10)
fill_rect(i,222,6,rects_h,R2)
rects_clock=0
R=(randint(0,55),randint(0,55),randint(0,55))
R2=(randint(100,255),randint(100,255),randint(100,255))
rects_x=(range(1,322,10))
colors=(randint(0,255),randint(0,255),randint(0,255))
rects_h=randint(-60,-10)
fill_rect(i,222,6,rects_h,R2)
from math import *
def test(n):
prime = [True for i in range(n + 1)]
p = 2
while (p * p <= n):
if (prime[p] == True):
for i in range(p * 2, n + 1, p):
prime[i] = False
p += 1
prime[0]= False
prime[1]= False
for p in range(n + 1):
if prime[p]:
print(p)
# "Cubic Duel"
# Written By: Wilson
# wilsony175@gmail.com
from math import *
from ion import *
from time import *
from random import *
from kandinsky import *
from ion import keydown as KEY
from ion import KEY_LEFT as LEFT
from ion import KEY_RIGHT as RIGHT
from ion import KEY_UP as UP
from ion import KEY_DOWN as DOWN
from ion import KEY_OK as OK
from ion import KEY_BACKSPACE as BS
from kandinsky import fill_rect as FILL
from kandinsky import draw_string as STRING
from random import randint as R
time=0
def start_menu():
global time
FILL(0,0,322,222,(0,)*3)
while not KEY(KEY_EXE):
STRING("CUBIC DUEL",100,160,(255,)*3,(20,0,200))
time+=1
if time>0 and time<400:
STRING("""
HISTORICAL:
Duel: A contest between two
people in order to settle a
point of honor...
""",10,5,"white",(0,0,140))
STRING("PRESS [EXE] KEY to start",40,200,(0,255,255),(0,)*3)
if time>400 and time<600:
STRING(" ",40,200,(0,20,100),(0,)*3)
if time>600:
time=0
if KEY(KEY_EXE):
guide_menu()
def guide_menu():
FILL(0,0,322,222,(0,)*3)
FILL(0,20,322,5,(200,)*3)
FILL(0,180,322,5,(200,)*3)
while not KEY(OK):
STRING("KEYs [Arrow] = 8 Directions",6,60,(200,)*3,(0,)*3)
STRING("KEY [OK] = fast LEFT/RIGHT.",6,100,(200,)*3,(0,)*3)
STRING("KEY [Backspace] = Pause/UnPause",6,140,(200,)*3,(0,)*3)
STRING("Press [OK] KEY",80,200,(100,)*3,(0,)*3)
start_menu()
game=True
bg=(0,0,140)
p_attacks=False
penergy=80
pscore=0
pdir=0
px=4
py=213
ps=8
pc=(119,25,71)
echase=0
e_run=False
echase_clock=0
edir=0
eclock=0
e_energy=200
ex=R(200,300)
ey=R(60,200)
es=10
ec=(0,)*3
FILL(0,0,322,222,bg)
FILL(0,18,322,2,(100,)*3)
FILL(0,208,20,4,(0,)*3)
FILL(3,3,es,es,(0,)*3)
FILL(10,5,2,2,(255,0,0))
while game:
FILL(0,208,20,4,(0,)*3)
FILL(0,211,20,12,(38,71,107))
FILL(px,py,ps,ps,pc)
FILL(ex,ey,es,es,ec)
FILL(px+2,py+2,1,1,(0,255,255))
FILL(px+5,py+2,1,1,(0,255,255))
pscore+=2
eclock+=1
echase_clock+=1
if echase_clock>R(50,100):
echase=R(0,2)
echase_clock=0
if echase:
if px+ps-2<ex:
edir=1
if px>ex+es-2:
edir=2
if py+ps-2<ey:
edir=3
if py>ey+es-2:
edir=4
if eclock > R(50,100):
edir=R(1,4)
eclock=R(-50,0)
if edir==1:
sleep(.005)
ex-=1
FILL(ex+10,ey,1,es,bg)
FILL(ex+2,ey+2,3,1,(255,0,0))
FILL(ex,ey+6,3,2,(50,150,0))
if echase:
for q in range(3):
ex-=1
FILL(ex+es+2,ey,2,es,bg)
if edir==2:
sleep(.005)
ex+=1
FILL(ex-1,ey,1,es,bg)
FILL(ex+6,ey+2,3,1,(255,0,0))
FILL(ex+7,ey+6,3,2,(50,150,0))
if echase:
for q in range(3):
ex+=1
FILL(ex-2,ey,2,es,bg)
if edir==3:
sleep(.005)
ey-=1
FILL(ex,ey+es,es,1,bg)
FILL(ex+2,ey+1,4,1,(255,0,0))
if edir==4:
sleep(.005)
ey+=1
FILL(ex,ey-1,es,1,bg)
FILL(ex+2,ey+9,4,1,(255,0,0))
if pdir==1:
for w in range(2):
px-=1
FILL(px+ps,py,1,ps,bg)
if KEY(OK):
p_attacks=True
for w in range(8):
px-=1
FILL(px+ps,py,2,ps,bg)
if pdir==2:
for w in range(2):
px+=1
FILL(px-1,py,1,ps,bg)
if KEY(OK):
p_attacks=True
for w in range(8):
px+=1
FILL(px-2,py,2,ps,bg)
if pdir==3:
for w in range(2):
py-=1
FILL(px,py+ps,ps,1,bg)
if KEY(OK):
p_attacks=True
for w in range(4):
py-=1
FILL(px,py+ps,ps,2,bg)
if pdir==4:
for w in range(2):
py+=1
FILL(px,py-1,ps,1,bg)
if KEY(OK):
p_attacks=True
for w in range(4):
py+=1
FILL(px,py-2,ps,2,bg)
#player moves diagonal
if pdir==5:
px-=1
FILL(px+ps,py,1,ps,bg)
py-=1
FILL(px,py+ps,ps,1,bg)
if pdir==6:
px+=1
FILL(px-1,py,1,ps,bg)
py-=1
FILL(px,py+ps,ps,1,bg)
if pdir==7:
px-=1
FILL(px+ps,py,1,ps,bg)
py+=1
FILL(px,py-1,ps,1,bg)
if pdir==8:
px+=1
FILL(px-1,py,1,ps,bg)
py+=1
FILL(px,py-1,ps,1,bg)
if KEY(LEFT) or KEY(KEY_FOUR):
pdir=1
if KEY(RIGHT) or KEY(KEY_SIX):
pdir=2
if KEY(UP) or KEY(KEY_EIGHT):
pdir=3
if KEY(DOWN) or KEY(KEY_TWO):
pdir=4
if KEY(KEY_SEVEN):pdir=5
if KEY(KEY_NINE):pdir=6
if KEY(KEY_ONE):pdir=7
if KEY(KEY_THREE):pdir=8
if KEY(KEY_FIVE):pdir=0
if KEY(LEFT) and KEY(UP):
pdir=5
if KEY(RIGHT) and KEY(UP):
pdir=6
if KEY(LEFT) and KEY(DOWN):
pdir=7
if KEY(RIGHT) and KEY(DOWN):
pdir=8
if px<1:
px=1
if px+ps>321:
px=321-ps
if py<20:
py=20
if py+ps>222:
py=222-ps
if ex<1:
ex=1
edir=choice([2,3,4])
if ex+es>321:
ex=321-es
edir=choice([1,3,4])
if ey<20:
ey=20
edir=choice([1,2,4])
if ey+es>222:
ey=222-es
edir=choice([1,2,3])
if KEY(BS):
STRING("(PAUSED)",100,50,(255,)*3,bg)
STRING("KEYs [1,3,7,9] = outsmart cube.",14,80,(255,)*3,bg)
STRING("KEY [OK] = fast LEFT/RIGHT.",14,110,(255,)*3,bg)
STRING("HEALTH:"+str(int(penergy)),80,140,(255,)*3,bg)
STRING("ENEMY:"+str(int(e_energy)),80,170,(255,)*3,bg)
while KEY(BS):
pass
while not KEY(BS):
pass
while KEY(BS):
STRING(" ",100,50,bg,bg)
STRING(" ",10,80,bg,bg)
STRING(" ",14,110,"white",bg)
STRING(" ",80,140,(255,)*3,bg)
STRING(" ",80,170,(255,)*3,bg)
if px<=16:
if py>211:
penergy+=0.001
if py+ps >= 207 and py+ps <= 210:
py=206-ps
pdir=0
if py <= 211 and py >= 210:
py=212
pdir=0
if py+ps>=207 and py<=210 or py+ps>=207 and py+ps<=210:
px=18
pdir=0
if ex<=17:
if ey+es >= 208:
ex=17
edir=choice([2,3,4])
echase=R(0,1)
if px+ps >= ex and px <= ex+es and py+ps >= ey and py <= ey+es:
FILL(px,py,ps,ps,(105,0,0))
FILL(px-choice([0,1,2,3,-1,-2,-3,]),py+choice([1,2,3]),2,2,(105,0,0))
FILL(px,py,ps,ps,(105,0,0))
penergy-=0.2
FILL(px-choice([0,1,2,-8,-9,-10]),py+ps,2,2,(0,50,0))
if p_attacks:
p_attacks=False
edir=choice([1,2,3,4])
#enemy stops for a sec and
#loses energy.
echase=False
e_energy-=1
penergy+=.3
pscore+=R(5,20)
FILL(20,5,322,5,bg)
FILL(ex+R(-10,20),ey+R(-10,20),R(1,2),R(1,2),(0,)*3)
FILL(px,py,ps,ps,(105,0,0))
penergy-=0.05
FILL(px-choice([0,1,2,-8,-9,-10]),py+ps,2,2,(0,50,0))
if echase:
for q in range(3):
ex-=1
FILL(ex+es+2,ey,2,es,bg)
STRING(str(int(penergy)),300,0)
if penergy<1:game=0
if KEY(OK):
if pdir==1:
for w in range(322):
FILL(px-12,py+3,R(8,14),R(1,2),(255,)*3)
if pdir==2:
for w in range(322):
w=px+R(10,15)
FILL(px+5,py+3,R(8,14),R(1,2),(255,)*3)
FILL(20,5,int(e_energy),5,(255,0,0))
for i in range(px-20):
for j in range(px-20):
i=R(0,321)
j=R(0,222)
FILL(i,j,R(3,9),R(3,9),(50,0,0))
STRING("GAME OVER",100,70,(255,)*3,(125,40,50))
STRING("Score: "+str(pscore),100,120,(255,)*3,(125,40,50))
FILL(0,0,322,40,(269,)*3)
FILL(0,182,322,40,(269,)*3)
from math import *
from kandinsky import *
from ion import *
from time import *
from random import *
big_font=[(20,20,8,50,(255,)*3)
]
time=0
def menu():
global time
fill_rect(0,0,322,222,(0,20,100))
while not keydown(KEY_EXE):
for i in big_font:
fill_rect(*i)
time+=1
if time>0 and time<500:
draw_string("PRESS [EXE] key to start",40,160,(0,255,255),(0,20,100))
if time>500 and time<800:
draw_string(" ",40,160,(0,20,100),(0,20,100))
if time>800:
time=0
if keydown(KEY_EXE):
menu=0
menu()
paused=False
score=0
clock=0
star_size=1
star_colors=["white",
"yellow",
"orange",
"red",
"blue"
]
C=(randint(0,255),randint(0,255),randint(0,255))
R=(randint(0,255),randint(0,255),randint(0,255))
c=randint(0,322)
d=randint(0,222)
R=randint(0,70)
G=randint(0,70)
B=randint(0,70)
rr=40
gg=25
bb=100
r=randint(0,80)
g=randint(10,50)
b=randint(15,25)
r2=randint(5,55)
g2=randint(5,55)
b2=randint(5,55)
energy=50
energy_x=randint(5,250)
energy_y=randint(10,200)
energy_width=9
energy_height=15
energy_timer=0
background_color=(rr,gg,bb)
RC=(randint(0,230),randint(0,230),randint(0,230))
move=True
game_end=False
p_timer=0
p_dir_LEFT=1
p_dir_RIGHT=2
p_dir_UP=3
p_dir_DOWN=4
p_x=10
p_y=100
sleep_slow=1/10**4
p_speed_fast=1/10**9
p_pace=1
p_speed_range=2
p_width=10
p_height=4
p_color="white"
SCREEN_width=321
SCREEN_height=222
bar_width=randint(15,40)
bar_height=randint(170,210)
bar_x=randint(170,265-bar_width)
bar_y=randint(10,200)
bar_color=(r,g,b)
bar_width2=randint(15,40)
bar_height2=randint(170,210)
bar_x2=randint(170,265-bar_width)
bar_y2=randint(10,200)
bar_color2=(r,g,b)
bar_dir_LEFT=False
bar_dir_RIGHT=False
bar_dir_UP=False
bar_dir_DOWN=False
bar_UP=False
timer_bar=0
fill_rect(0,0,322,222,background_color)
fill_rect(265,0,2,222,(225,)*3)
while not game_end and not paused:
score+=randint(0,1)
clock+=1
if clock>0 and clock<30:
c=randint(0,322)
d=randint(0,90)
C=star_colors[randint(0,4)]
fill_rect(c,d,star_size,star_size,C)
draw_string("S:"+str(score),260,6,(169,169,169),background_color)
draw_string("+",energy_x,energy_y,(255,)*3,(255,0,0))
draw_string("E:"+str(energy),260,24,(169,)*3,background_color)
energy+=0.002
bar_x-=1
bar_x2-=1
energy_timer+=1
fill_rect(0,0,265,5,(40,0,0))
fill_rect(0,217,265,4,(40,0,0))
if bar_x < -bar_width-1:
bar_x=randint(170,265-bar_width)
bar_y=randint(-50,150)
bar_width=randint(15,40)
bar_height=randint(120,200)
r=randint(5,255)
g=randint(5,255)
b=randint(5,255)
bar_color=(r,g,b)
clock=0
c=randint(0,322)
d=randint(0,90)
C=star_colors[randint(0,4)]
fill_rect(c,d,star_size,star_size,C)
if bar_x2 < -bar_width2-1:
bar_x2=randint(150,200-bar_width)
bar_y2=randint(-30,0)
bar_width2=randint(20,50)
bar_height2=randint(80,150)
r2=randint(5,55)
g2=randint(5,55)
b2=randint(5,55)
bar_color2=(r2,g2,b2)
fill_rect(bar_x,bar_y,bar_width,bar_height,bar_color)
fill_rect(bar_x+bar_width+1,bar_y,1,bar_height,background_color)
fill_rect(bar_x2,bar_y2,bar_width2,bar_height2,bar_color2)
fill_rect(bar_x2+bar_width2+1,bar_y2,1,bar_height2,background_color)
fill_rect(p_x,p_y,p_width,p_height,p_color)
fill_rect(p_x,p_y,p_width,p_height,p_color)
if p_x+10 > bar_x and p_x<=bar_x+bar_width and p_y+4 >= bar_y and p_y <= bar_y+bar_height or p_x+10 > bar_x2 and p_x<=bar_x2+bar_width2 and p_y+4 >= bar_y2 and p_y <= bar_y2+bar_height2:
energy-=0.5
if energy<1:
game_end=True
if p_x+10>265:
p_x=265-10
move=False
else:
move=True
#could just use 0
if p_x+p_width >= energy_x and p_y+p_height >= energy_y and p_y <= energy_y+energy_height or p_x <= bar_x+bar_width and p_y+p_height >= bar_y and p_y <= energy_y+energy_height:
energy+=2
draw_string(".",energy_x,energy_y,background_color,background_color)
energy_x=randint(-20,250)
energy_y=randint(-20,300)
energy_timer=0
score+=randint(5,20)
if energy_timer>randint(400,600):
draw_string(" ",energy_x,energy_y,background_color,background_color)
energy_x=randint(5,250)
energy_y=randint(5,210)
energy_timer=0
if p_x<(SCREEN_width-SCREEN_width):
move=False
p_x=SCREEN_width-SCREEN_width
else:
move=True
if p_y<5:
move=False
p_y=5
else:
move=True
if p_y>SCREEN_height-10:
move=False
p_y=SCREEN_height-10
else:
move=True
if move:
if keydown(KEY_RIGHT):
move=True
energy-=0.002
sleep(sleep_slow)
p_x+=p_pace
fill_rect(p_x-1,p_y,1,4,background_color)
if keydown(KEY_OK):
move=True
for i in range(p_speed_range):
p_x+=1
fill_rect(p_x-1,p_y,1,10,background_color)
if keydown(KEY_LEFT):
move=True
energy-=0.002
sleep(sleep_slow)
p_x-=p_pace
fill_rect(p_x+10,p_y,1,10,background_color)
if keydown(KEY_OK):
move=True
energy-=0.005
for i in range(p_speed_range):
p_x-=1
fill_rect(p_x+10,p_y,1,10,background_color)
if keydown(KEY_UP):
move=True
energy-=0.002
sleep(sleep_slow)
p_y-=1
fill_rect(p_x,p_y+4,10,1,background_color)
if keydown(KEY_OK):
move=True
energy-=0.005
for i in range(p_speed_range):
p_y-=1
fill_rect(p_x,p_y+4,10,1,background_color)
if keydown(KEY_DOWN):
move=True
energy-=0.002
sleep(sleep_slow)
p_y+=1
fill_rect(p_x,p_y-1,10,1,background_color)
if keydown(KEY_OK):
move=True
energy-=0.005
for i in range(p_speed_range):
p_y+=1
fill_rect(p_x,p_y-1,10,1,background_color)
if keydown(KEY_BACKSPACE):
draw_string("(PAUSED)",100,10,"white",background_color)
draw_string("OK Key = Fast Speed",70,50,"white",background_color)
while keydown(KEY_BACKSPACE):
pass
while not keydown(KEY_BACKSPACE):
pass
while keydown(KEY_BACKSPACE):
draw_string(" ",100,10,background_color,background_color)
draw_string(" ",70,50,background_color,background_color)
pass
if keydown(KEY_SHIFT):
draw_string("(PAUSED)",100,10,"white",background_color)
draw_string("OK Key = Fast Speed",70,50,"white",background_color)
while keydown(KEY_SHIFT):
pass
while not keydown(KEY_SHIFT):
pass
while keydown(KEY_SHIFT):
draw_string(" ",100,10,background_color,background_color)
draw_string(" ",70,50,background_color,background_color)
pass
for i in range(320):
for j in range(220):
fill_rect(i,j,5,5,"black")
draw_string("GAME OVER",100,70,(255,)*3,(0,)*3)
draw_string("Score: "+str(score),100,120,(0,255,0),(0,)*3)
fill_rect(0,0,322,40,(269,)*3)
fill_rect(0,182,322,40,(269,)*3)
from math import *
from math import *
from kandinsky import *
from ion import *
from time import *
from random import *
R=randint(0,255)
G=randint(0,255)
B=randint(0,255)
num=1
denom=1
fill_rect(0,0,322,222,(R,G,B))
fill_rect(0,0,322,80,"black")
while 1:
fract=num/denom
draw_string("Numerator : "+str(num),10,5,"cyan","black")
draw_string("Denominator : "+str(denom),10,25,"pink",(0,0,0))
draw_string(" = "+str(fract),10,52,"white",(0,0,0))
draw_string("Use Arrow keys",5,147,"white","red")
draw_string("To change values.",5,167,"black","white")
draw_string("BackSpace = Reset",5,186,"cyan","blue")
draw_string("RGB="+str(R)+","+str(G)+","+str(B),164,89)
draw_string("[EXE] = Random RGB",4,128,"black","pink")
if keydown(KEY_EXE):
R,G,B = (randint(0,255),randint(0,255),randint(0,255))
fill_rect(0,70,322,222,(R,G,B))
fill_rect(0,0,322,80,"black")
sleep(0.2)
if keydown(KEY_UP):
fill_rect(0,0,322,80,"black")
sleep(0.2)
num+=1
if keydown(KEY_DOWN):
fill_rect(0,0,322,80,"black")
sleep(0.2)
num-=1
if keydown(KEY_RIGHT):
fill_rect(0,0,322,80,"black")
sleep(0.2)
denom+=1
if keydown(KEY_LEFT):
fill_rect(0,0,322,80,"black")
sleep(0.2)
denom-=1
if keydown(KEY_BACKSPACE):
fill_rect(0,0,322,80,"black")
num=1
denom=1
sleep(0.2)
if denom<1:
denom=1
if num<1:
num=1
if num>denom:
draw_string("Mix Number",2,89,(0,0,100),(153,204,255))
if num==denom:
draw_string("Equal 1",2,89,(0,0,100),(153,204,255))
if num<denom:
draw_string("Fraction",2,89,(0,0,100),(153,204,255))
from kandinsky import fill_rect as krect, draw_string as text
from ion import keydown
from random import randint
from time import *
# Code by Fime, nsi.xyz/jetpackbird
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()
from math import cos,sin,pi
from kandinsky import *
from ion import *
from random import random,randint
#menu
def menu():
global vitesse,Vraquette
draw_string("Mode",140,20,'orange')
draw_string("1 joueur 2 joueurs",60,40,'orange')
mode=1
while not (keydown(KEY_OK) or keydown(KEY_EXE)):
draw_string("[",40+120*mode,40,'blue')
draw_string("]",150+130*mode,40,'blue')
if keydown(KEY_LEFT) or keydown(KEY_RIGHT):
mode=1-mode
draw_string(" ",40+120*(1-mode),40)
draw_string(" ",150+130*(1-mode),40)
while keydown(KEY_LEFT) or keydown(KEY_RIGHT):True
while keydown(KEY_OK) or keydown(KEY_EXE):True
draw_string("Vitesse du jeu",90,70,'orange')
draw_string("lent moyen rapide",60,90,'orange')
vitesse=2
while not (keydown(KEY_OK) or keydown(KEY_EXE)):
draw_string("[",-30+70*vitesse+10*(vitesse//3),90,'blue')
draw_string("]",30+80*vitesse+10*(vitesse//3),90,'blue')
dv=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
if (keydown(KEY_RIGHT) or keydown(KEY_LEFT)) and dv != 0:
draw_string(" ",-30+70*vitesse+10*(vitesse//3),90)
draw_string(" ",30+80*vitesse+10*(vitesse//3),90)
vitesse=(vitesse+dv-1)%3+1
while keydown(KEY_RIGHT) or keydown(KEY_LEFT):True
while keydown(KEY_OK) or keydown(KEY_EXE):True
draw_string("Déplacement des raquettes",55,120,'orange')
draw_string("lent moyen rapide",60,140,'orange')
Vraquette=2
while not (keydown(KEY_OK) or keydown(KEY_EXE)):
draw_string("[",-30+70*Vraquette+10*(Vraquette//3),140,'blue')
draw_string("]",30+80*Vraquette+10*(Vraquette//3),140,'blue')
dr=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
if (keydown(KEY_RIGHT) or keydown(KEY_LEFT)) and dr != 0:
draw_string(" ",-30+70*Vraquette+10*(Vraquette//3),140)
draw_string(" ",30+80*Vraquette+10*(Vraquette//3),140)
Vraquette=(Vraquette+dr-1)%3+1
while keydown(KEY_RIGHT) or keydown(KEY_LEFT):True
# ajouter mode de rebond : angle relatif ou absolu
return mode
mode=menu()
fill_rect(0,18,320,1,'purple')
fill_rect(0,204,320,1,'purple')
#raquette 1
x1=5
y1=111
#raquette 2
x2=315
y2=111
#balle
x=160
y=111
def balle(x,y,couleur):
fill_rect(x-2,y-2,5,5,couleur)
def raquette(x,y,couleur):
fill_rect(x-1,y-10,3,21,couleur)
score=[0,0]
angle=(pi/6-pi/18)*random()+pi/18
horiz=2*randint(0,1)-1
verti=1
clr1,clr2=(96,44,120),(104,44,128)
while 1:
fill_rect(0,19,320,185,'white')
x,y=160,111
x1,y1=5,111
x2,y2=315,111
#vitesse=2
V=vitesse
while 1:
d1 = (keydown(KEY_DOWN)-keydown(KEY_UP))
y1 = max(min(193,y1+(vitesse+Vraquette-1)*d1),29) # y1 +=3*d1
if mode:
d2 = (keydown(KEY_RIGHT)-keydown(KEY_LEFT))
else:
d2 = min(vitesse+Vraquette-1,abs(int(y2-y)))*(2*(y>y2)-1)
y2 = max(min(193,y2+d2*(1+(vitesse+Vraquette-1-1)*mode)),29)#y2 += d2 #3*(2*randint(0,3)-3
#draw_string("|",x1,y1,['green','red'][(y1-y)**2>100])
raquette(x1,y1,['green','red'][(y1-y)**2>169])
if d1 != 0:fill_rect(x1-1,y1-2-13*d1,5,5,'white')
raquette(x2,y2,['blue','red'][(y2-y)**2>169])
if d2 != 0:
if mode:
fill_rect(x2-1,y2-2-13*d2,5,5,'white')
else:
fill_rect(x2-1,y2-2-13*(2*(y>y2)-1),5,5,'white')
vx=x
vy=y
x+=V*cos(angle)*horiz
y+=V*sin(angle)*verti
clr1,clr2=clr2,clr1
balle(int(x),int(y),clr1)
for i in range(5):
for j in range(5):
if get_pixel(int(vx)-2+i,int(vy)-2+j)==clr2:
set_pixel(int(vx)-2+i,int(vy)-2+j,'white')
if x>311:
horiz = -1
if (y-y2)**2>169:
draw_string("Joueur1 a gagné",85,110)
score[0]+=1
break
else:
#angle=min(2*pi/5,angle+(y-y2)*pi/180)
angle=abs(6*(y-y2)*pi/180)
verti = 2*(y>y2)-1
V *= 1.1
draw_string("Vitesse : "+str(V)[:4]+" ",15,205,'blue')
draw_string("Angle : "+str(int(abs(angle*180/pi)))+"° ",180,205,'green')
if x<9:
horiz = 1
if (y-y1)**2>169:
draw_string("Joueur2 a gagné",85,110)
score[1]+=1
break
else:
#angle=min(2*pi/5,angle+(y-y1)*pi/180)
angle=abs(6*(y-y1)*pi/180)
verti = 2*(y>y1)-1
V *= 1.1
draw_string("Vitesse : "+str(V)[:4]+" ",15,205,'blue')
draw_string("Angle : "+str(int(abs(angle*180/pi)))+"° ",180,205,'green')
if y>200 or y<22:
verti *= -1
#angle *= -1
fill_rect(0,204,320,1,'purple')
fill_rect(0,18,320,1,'purple')
while not keydown(KEY_EXE):
draw_string("Score : "+str(score[0])+" - "+str(score[1]),60,0,'red')
draw_string("EXE",145,131,'purple','cyan')
from math import *
from math import *
from kandinsky import *
from ion import *
from time import *
from random import *
colors=[(25,151,0),
(0,0,255),
(0,0,128),
(192,192,192),
(128,128,0),
(255,127,80),
(233,150,122),
(218,165,32),
(128,0,0),
(85,107,47),
(100,149,237)
]
bgcolor=None
alive=True
posx=20
posy=170
points=0
energy=500
food_x=randint(80,200)
food_y=randint(70,150)
food_width=randint(15,50)
food_height=randint(15,50)
food_timer=randint(-3,0)
food_color=(0,100,0)
food_quantity=0
lava_x=randint(55,200)
lava_y=randint(55,150)
lava_width=3
lava_height=3
lava_color="red"
S=3/1000
fill_rect(0,0,322,222,"black")
while alive:
draw_string("Score:"+str(points),200,0,"white","black")
draw_string("Energy:"+str(energy),0,0,"white","black")
if food_timer>randint(2,5):
fill_rect(food_x,food_y,food_width,food_height,colors[randint(0,9)])
fill_rect(food_x,food_y,food_width,food_height,colors[randint(0,9)])
food_timer=0
food_x=randint(0,300)
food_y=randint(20,210)
food_width=randint(10,30)
food_height=randint(10,30)
#player
if energy<=150:
fill_rect(posx,posy,2,2,(245,245,245))
elif energy>=151 and energy<=250:
fill_rect(posx,posy,2,2,(128,128,128))
else:
fill_rect(posx,posy,2,2,(169,255,169))
fill_rect(food_x,food_y,food_width,food_height,food_color)
if keydown(KEY_LEFT):
sleep(S)
posx-=1
points+=1
food_timer+=0.01
energy-=1
fill_rect(0,0,150,18,"black")
if posx<1:
posx=319
if keydown(KEY_RIGHT):
sleep(S)
posx+=1
points+=1
food_timer+=0.01
energy-=1
fill_rect(0,0,322,18,"black")
if posx>319:
posx=1
if keydown(KEY_UP):
sleep(S)
posy-=1
points+=1
food_timer+=0.01
energy-=1
fill_rect(0,0,150,18,"black")
if posy<1:
posy=222
if keydown(KEY_DOWN):
sleep(S)
posy+=1
points+=1
energy-=1
fill_rect(0,0,150,18,"black")
if posy>222:
posy=1
if get_pixel(posx,posy)==get_pixel(food_x,food_y):
energy+=100
food_quantity+=1
points+=randint(20,100)
food_timer=0
food_x=randint(0,300)
food_y=randint(20,210)
food_width=randint(10,30)
food_height=randint(10,30)
fill_rect(food_x,food_y,food_width,food_height,"black")
if food_quantity>3:
fill_rect(0,0,322,222,(randint(0,255),randint(0,255),randint(0,255)))
food_quantity=0
if energy<1:
alive=False
fill_rect(0,0,320,222,"black")
draw_string("GAME OVER",100,100,"white","black")
draw_string("SCORE: "+str(points),100,130,"yellow","black")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment