ninja_v_4.py

Created by wperez274

Created on August 20, 2022

10.2 KB


from math import *
from cmath import *
from kandinsky import *
from kandinsky import fill_rect as F
from kandinsky import draw_string as STR
from random import *
from random import randint as R
from ion import *
from time import *



enemy_defeated=False
edir_timer=0
echase=False
eshooting=False
eshoot_timer=0
edir=0
ex=R(200,322)
ey=R(0,50)
ew=R(10,18)
eh=R(12,30)
ec=(R(0,150),R(0,150),R(0,150))
#enemy energy
e_energy=round(ew*eh/4)

def draw_enemy():
  global echase,edir,ex,ey,ew,eh,ec
  F(ex,ey,ew,eh,ec)
  F(ex+round(ew/5),ey+round(eh/6),round(ew/3),round(ew/5),"red")
  F(ex,ey,1,eh,"gray")
  F(ex+ew-1,ey,1,eh,"gray")
  F(ex,ey,ew,1,"gray")
  F(ex,ey+eh-1,ew,1,"gray")
  
def move_enemy():
  global echase,edir_timer,edir,ex,ey,ew,eh,ec,bg
#  edir=choice([0,0,0,1,2,3,4])
  edir_timer+=0.01
  
  if edir_timer>=R(1,3):
    edir_timer=0
    echase=choice([True,False])
  
  if echase:
    if px<ex:
      edir=1

    if px>ex:
      edir=2

   
    if py<ey:
      edir=3
    if py>ex:
      edir=4
    
  if edir==1:
    ex-=5
    F(ex+ew,ey,5,eh,bg)
    eshooting=True
  if edir==2:
    ex+=5
    F(ex-5,ey,5,eh,bg)
    eshooting=True
  if edir==3:
    ey-=3
    F(ex,ey+eh,ew,3,bg)
  if edir==4:
    ey+=3
    F(ex,ey-3,ew,3,bg)

def contain_enemy():
  global echase,edir_timer,edir,ex,ey,ew,eh,ec,bg,box_x,box_y,box_w,box_h

  if ex+ew<0:
    ex=0+ew
  if ex+ew>322:
    ex=322-ew
  if ey+eh<=0:
    ey=0
    edir=1
  if ey+eh>gy:
    ey=gy-eh
    edir=2

def spawn_new_enemy():
  global e_energy,echase,edir_timer,edir,ex,ey,ew,eh,ec,bg

  echase=False
  enemy_defeated=False
  e_energy=round(ew*eh/4)
  edir_timer=0
  edir=0
  ex=R(0,310)
  ey=R(0,190)
  ew=R(10,18)
  eh=R(12,30)
  ec=(R(0,155),R(0,155),R(0,155))

#////////////////////////
sleep(0.2)
bonus_points=R(50,200)
px=R(5,250)
py=185
pw=10
ph=16
pc=choice([
"blue","yellow","cyan",
"black","white",
(R(155,255),R(155,255),R(155,255))
])


pfaces_left=False
pfaces_right=True

bg=(0,0,0)
#start screen
F(0,0,322,222,bg)
F(0,0,3,222,"blue")
F(318,0,3,222,"blue")
F(0,0,322,4,"blue")
F(0,218,322,4,"blue")

F(3,3,318,3,"white")
F(3,30,318,3,"white")
F(3,35,318,2,"white")

F(R(260,280),0,R(8,45),222,(R(100,255),R(0,25),R(0,15)))

F(0,200,322,5,"cyan")

F(px,py,pw,ph,pc)
F(px,py,pw,ph,pc)
F(px,py-1,pw,1,bg)
F(px,py,1,ph,"white")
F(px+pw-1,py,1,ph,"white")
F(px,py,pw,1,"white")
F(px,py+ph,pw,1,"white")

while not keydown(KEY_OK) and not keydown(KEY_EXE):
  
  STR("RECTNINJA",100,10,"white",bg)
  STR("key [UP] = Float",100,60,(R(0,255),R(0,255),R(0,255)),bg)

  STR("key [Backspace] = Pause",15,80,"white",bg)
  STR("Key [Arrows] = Move",15,110,"cyan",bg)
  STR("Press [OK/EXE] key to Start",10,140,"orange",bg)
  
  F(0,200,322,3,"gray")



gx=0
gy=R(160,210)
gw=322
gh=6
gc="cyan"

def draw_ground():
  global gx,gy,gw,gh,gc
  F(gx,gy,gw,gh,gc)

gravty=True
game=True
have_key=False
sh_on=False
level_complete=False
jump=False
#sky color
bg=(0,0,R(0,255))



fall_speed=1
jump_max=1
jump_time=0

level=1
plife=3
px=20
py=20
pw=12
ph=18
pc=(R(0,90),R(0,90),R(0,90))
psc=0

sh_time=R(3,6)
sh_x=R(2,310)
sh_y=R(-200,-50)
sh_w=15
sh_h=15
sh_c=(R(0,255),R(0,255),R(0,255))

b_x=25
b_y=R(50,150)
b_w=R(10,70)
b_h=R(6,40)
b_c=(R(0,255),R(0,255),R(0,255))


b_2_x=0
b_2_y=R(30,160)
b_2_w=R(10,70)
b_2_h=R(6,45)
b_2_c=(R(0,255),R(0,255),R(0,255))

b_3_x=320-105
b_3_y=R(30,160)
b_3_w=R(10,70)
b_3_h=R(6,30)
b_3_c=(R(0,255),R(0,255),R(0,255))



box_x=R(2,275)
box_y=R(5,170)
box_w=R(14,50)
box_h=R(14,30)
box_c=(R(0,255),R(0,255),R(0,255))

def draw_boxes():

  global b_x,b_y,b_w,b_h,b_c,b_2_x,b_2_y,b_2_w,b_2_h,b_2_c,b_3_x,b_3_y,b_3_w,b_3_h,b_3_c,box_x,box_y,box_w,box_h,box_c

  
  F(b_x,b_y,b_w,b_h,b_c)
  
  F(b_x,b_y,2,b_h,"white")
  F(b_x+b_w-2,b_y,2,b_h,"white")
  F(b_x,b_y,b_w,2,"white")
  F(b_x,b_y+b_h-2,b_w,2,"white")
  F(b_x+b_w,b_y,1,b_h,bg)
  
  
  F(b_2_x,b_2_y,b_2_w,b_2_h,b_2_c)
  F(b_2_x,b_2_y,2,b_2_h,"white")
  F(b_2_x+b_2_w-2,b_2_y,2,b_2_h,"white")
  F(b_2_x,b_2_y,b_2_w,2,"white")
  F(b_2_x,b_2_y+b_2_h-2,b_2_w,2,"white")
  
 
  F(b_3_x,b_3_y,b_3_w,b_3_h,b_3_c)
  F(b_3_x,b_3_y,2,b_3_h,"white")
  F(b_3_x+b_3_w-2,b_3_y,2,b_3_h,"white")
  F(b_3_x,b_3_y,b_3_w,2,"white")
  F(b_3_x,b_3_y+b_3_h-2,b_3_w,2,"white")
  
  F(box_x,box_y,3,box_h,box_c)
  F(box_x+box_w-3,box_y,3,box_h,box_c)
  F(box_x,box_y,box_w,3,box_c)
  F(box_x,box_y+box_h-3,box_w,3,box_c)





door_x=R(5,300)
door_y=R(5,180)
door_w=16
door_h=24

door_c="black"

fill_rect(0,0,322,222,bg)
while game:
  gravty=True
  psc+=0.01
  sh_c=(R(0,255),R(0,255),R(0,255))


  if sh_on==False and have_key==False:
    sh_y+=1
  
  F(px,py,pw,ph,pc)
  F(px,py-2,pw,2,bg)
  F(px,py,1,ph,"gray")
  F(px+pw-1,py,1,ph,"gray")
  F(px,py,pw,1,"gray")
  F(px,py+ph-1,pw,1,"gray")

  F(sh_x,sh_y,sh_w,sh_h,sh_c)
  F(sh_x,sh_y-1,sh_w,1,bg)
  
  F(sh_x,sh_y,2,sh_h,"white")
  F(sh_x+sh_w-2,sh_y,2,sh_h,"white")
  F(sh_x,sh_y,sh_w,2,"white")
  F(sh_x,sh_y+sh_h-2,sh_w,2,"white")

  if keydown(KEY_LEFT):
    pfaces_left=True
    pfaces_right=False
    px-=2
    F(px+pw,py,2,ph,bg)
  
  if keydown(KEY_RIGHT):
    pfaces_right=True
    pfaces_left=False
    px+=2
    F(px-2,py,2,ph,bg)
  

  if px+pw<0:px=322  

  if px>322:px=0  

  
  if gravty:  
    py+=choice([0,1])
    
    
  if py<0 and enemy_defeated:
    F(0,0,322,222,"black")
    level+=1
    plife+=1
    psc+=bonus_points
    
    while not keydown(KEY_OK):
      STR("Level complete.!",20,10,"cyan","black")
      STR("Bonus Points: "+str(bonus_points),20,55,"white","black")
      STR("Score:"+str(round(psc)),20,85,"yellow","black")
      STR("life: "+str(round(plife)),20,120,"purple","black")
      STR("Press [OK] to continue to",40,160,"magenta","black")    
      STR("the next level.",40,190,"blue","black")
      
#reset and spawn new random world.                
    bonus_points=R(100,500)
    bg=(R(0,255),R(0,255),R(0,255))
    F(0,0,322,222,bg)
    draw_ground()
    door_x=R(5,300)      
    door_y=R(5,200)
    door_c=(R(0,255),R(0,255),R(0,255))
    key_1_x=R(5,300)
    key_1_y=R(5,200)
    
    
    
    b_x=R(300,400)
    b_y=R(60,150)
    b_w=R(70,120)
    b_h=R(6,10)
    b_c=(R(0,255),R(0,255),R(0,255))
    
    
    b_2_x=R(0,50)
    b_2_y=R(60,150)
    b_2_w=R(50,85)
    b_2_h=R(6,10)
    b_2_c=(R(0,255),R(0,255),R(0,255))

    b_3_x=R(225,280)
    b_3_y=R(60,150)
    b_3_w=R(40,70)
    b_3_h=R(6,10)
    b_3_c=(R(0,255),R(0,255),R(0,255))
    
    
    box_x=R(2,275)
    box_y=R(5,170)
    box_w=R(14,50)
    box_h=R(14,30)
    box_c=(R(0,255),R(0,255),R(0,255))

    
    py=0

    sh_time=0
    sh_on=False
    have_key=False
    enemy_defeated=False
    e_energy=round(ew*eh/4)
    spawn_new_enemy()
    

  if sh_on:
    sh_time-=0.0025
    F(px,py,pw,ph,(R(0,255),R(0,255),R(0,255)))
    STR("Shield: "+str(round(sh_time)),2,2,"cyan","black")      

  if sh_time<0:
    sh_on=False
    sh_time=5
    STR("          ",2,2,bg,bg)

#game over when plife<1
  if plife<1:
    game=False    
                  
# simple Pause/Resume code
  if keydown(KEY_BACKSPACE):
    while not keydown(KEY_TOOLBOX):
      STR("( PAUSED )",110,60,"blue","cyan")  
      STR("(PRESS [TOOLBOX] KEY TO RESUME)",5,140)  
    STR("          ",110,60,bg,bg)  
    STR("(PRESS [TOOLBOX] KEY TO RESUME)",5,140,bg,bg)  
         
  F(px+2,py+3,9,7,"white")

  if sh_y+sh_h>=gy:
    sh_y=gy-sh_h

  if px+pw>=sh_x and px<=sh_x+sh_w and py+ph>=sh_y and py<sh_y+sh_h:
    F(sh_x,sh_y,sh_w,sh_h,bg)
    sh_on=True
    sh_x=R(2,310)
    sh_y=R(-400,-250)
    
  
  
  if b_x+b_w<-10:
    b_x=R(600,900)
    b_y=R(50,100)
  
#player lands on platforms

#middle bottom board
  b_x-=choice([0,1])
  
  if px+pw>=b_x and px<=b_x+b_w and py+ph>=b_y and py<=b_y:
    gravty=False
    py=b_y-ph
    px-=choice([0,1])
    F(px+pw,py,1,ph,bg)
    
    
#left board  
  if px+pw>=b_2_x and px<=b_2_x+b_2_w and py+ph>=b_2_y and py<=b_2_y:
    gravty=False
    py=b_2_y-ph
#right board  
  if px+pw>=b_3_x and px<=b_3_x+b_3_w and py+ph>=b_3_y and py<=b_3_y:
    gravty=False
    py=b_3_y-ph

#gy is ground
  if py+ph>=gy:
    py=gy-ph
    
    
  b_2_y+=choice([0,0,0,1])


  if b_x+b_w<-5:
    b_x=322
    b_w=R(10,70)
    b_h=R(6,45)
    b_c=(R(0,255),R(0,255),R(0,255))


  if b_2_y>200:
    F(b_2_x,b_2_y-1,b_2_w,b_2_h,bg)
    b_2_y=0
    b_2_w=R(10,70)
    b_2_h=R(6,40)
    b_2_c=(R(0,255),R(0,255),R(0,255))

    
  if b_3_y+b_3_h<0:
    b_3_y=200
    b_3_w=R(10,75)
    b_3_h=R(6,30)
    b_3_c=(R(0,255),R(0,255),R(0,255))


  F(b_2_x,b_2_y-1,b_2_w,1,bg)
  F(b_3_x,b_3_y+b_3_h,b_3_w,1,bg)

  if jump:
    py-=R(1,2)
    F(px,py+ph,pw,2,bg)
    jump_max-=0.1
  
  if jump_max<0:
    jump=False
    jump_max=1
    
  if py<=1:py=1
    
  if keydown(KEY_UP):
    py-=1
    F(px,py+ph,pw,2,bg)

  draw_ground()
  draw_enemy()
  draw_boxes()
  move_enemy()
  contain_enemy()
  

  if px+pw>=box_x and px<=box_x+box_w and py<=box_y+2 and py>=box_y+2:
    py=box_y+2
    gravty=False

  if px+pw>=box_x and px<=box_x+box_w and py+ph>=box_y and py<=box_y:
    gravty=False
    py=box_y-ph

  if keydown(KEY_OK) and pfaces_right:
    F(px+pw,py+4,322-px,2,(R(0,255),R(0,255),R(0,255)))
    F(px+4,py+3,8,6,"red")
    sleep(0.02)
    F(px+pw,py+4,322-px,2,bg)

  if keydown(KEY_OK) and pfaces_left:
    F(0,py+4,px,2,(R(0,255),R(0,255),R(0,255)))
    F(px,py+3,8,6,"red")
    sleep(0.02)
    F(0,py+4,px,2,bg)

#player shoots enemy
  if keydown(KEY_OK) and pfaces_left and ex+ew<px and ey+eh>=py+4 and ey<=py+6:
    e_energy-=1
    edir=R(3,4)
    ey+R(-5,5)
    psc+=R(2,5)
    STR(str(round(e_energy)),180,0)
    F(ex+R(-3,3),ey+R(-3,3),ew,eh,"red")

  if keydown(KEY_OK) and pfaces_right and ex>=px+pw and ey+eh>=py+4 and ey<=py+6:
    e_energy-=1
    ey+R(-5,5)
    edir=R(3,4)
    psc+=R(2,5)
    STR(str(round(e_energy)),180,0)
    F(ex+R(-3,3),ey+R(-3,3),ew,eh,"red")


#player gets caught or hit :-(
  if px+pw>=ex and px<=ex+ew and py+ph>=ey and py<=ey+eh and sh_on==False:
    plife-=0.02
    F(px+R(-5,5),py+R(-5,5),pw,ph,"red")

  if e_energy<0:
    F(ex,ey,ew,eh,(R(0,120),0,0))
    edir_timer=0
    edir=0
    enemy_defeated=True
    door_y=200
    
    F(door_x,0,20,222,(R(0,255),R(0,255),R(0,255)))
    
    F(door_x,0,2,222,"cyan")
    F(door_x+18,0,2,222,"cyan")
  
  if px>=door_x and px<=door_x+20 and enemy_defeated:
    py-=5
    door_y-=5



#game over
sleep(1)
F(0,0,322,222,"black")
STR("GAME OVER",100,90,"red","black")      
sleep(1)
STR("SCORE:",100,140,"white","black")
sleep(1.1)
STR(str(round(psc)),170,140,"cyan","black")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

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.