chute_v2_2.py

Created by wperez274

Created on August 17, 2022

16.7 KB


# Edited by: Wilson.
from kandinsky import *
from kandinsky import fill_rect as F
from kandinsky import draw_string as STR
from ion import *
from time import *
from random import *
from random import randint as R

laser_force_x=250

key_x=R(0,300)
key_y=R(-20,0)
key_w=4
key_h=18
key_c="cyan"

def draw_ground():
  bg=(0,0,0)
  gx=300
  gy=5
  gw=5
  gh=5
  for i in range(0,322,5):
    for j in range(210,222):
      gc=choice([(150,0,0),(100,0,0),(250,0,0)])
      i+=randint(0,1)
      j+=randint(-4,2)
      F(i,j,gw,gh,gc)
      F(i+gw,j,2,gh,bg)
      F(i,j-2,gw,2,bg)

sleep(0.2)
def draw_home_screen():
  clone_x=R(10,35)
  clone_y=170
  bg=(45,5,79)
  F(0,0,322,222,bg)
  while not keydown(KEY_OK):
    F(0,25,322,2,"cyan")
    F(0,55,322,2,"cyan")
    F(10,200,300,10,"gray")
    F(280,192,22,8,"cyan")
    F(0,210,322,25,"red")
    F(clone_x,clone_y,14,26,(0,120,0))
    F(clone_x+5,clone_y+5,12,8,"cyan")
    STR("Chute 2.0",100,34,"black","cyan")
    STR("keys [left]/[Right] = Move",25,70,"white",bg)
    STR("key [Backspace] = Jump",30,90,"yellow",bg)
    STR("key [Toolbox] = Shoot",40,110,"gray",bg)
    STR("key [VAR] = Pause",60,130,"cyan",bg)
    STR("(Press [OK] to Start)",90,160,(R(200,255),0,0),bg)
def print_intro():
  text=[
  "In a peaceful planet, chaos",
  "erupted! Visitors from unknown",
  "origin and unknown species",
  "appeared on a quiet morning.",
  "They took over all resources.",
  "HELP!! Who will save us??!"
  ]
  
  F(0,0,322,222,"white")
  sleep(0.7)
  STR(text[0],10,30,"blue")
  sleep(0.7)
  STR(text[1],10,50,"blue")
  sleep(0.7)
  STR(text[2],10,70,"blue")
  sleep(0.7)
  STR(text[3],10,90,"blue")
  sleep(0.7)
  STR(text[4],10,110,"blue")
  sleep(0.7)
  STR(text[5],10,130,"blue")
  sleep(0.5)
  
  while not keydown(KEY_OK):
    STR("Press [OK] to Start",50,180)
    
draw_home_screen()
sleep(0.2)
print_intro()
time=9
time_2=1
lives=3
score=0
energy=30
shield_on=False
shield_time=5
shield_x=R(2,310)
shield_y=R(-20,0)
shield_w=16
shield_h=16
shield_c=(R(0,255),R(0,255),R(0,255))

food_x=R(2,315)
food_y=R(-100,-50)
food_w=11
food_h=18
food_c=(R(0,255),R(0,255),R(0,255))

lava_y=210
#dark red
lava_c=(150,0,0)

rect_x=300
rect_y=R(20,60)
rect_w=R(20,50)
rect_h=R(10,30)

rect_c=(R(0,255),R(0,255),R(0,255))

#background
backColor=choice([
"black","pink",
(randint(0,105),R(0,105),randint(0,255))
])

def game_over_draw():
  F(0,0,322,222,"black")
  for x in range(0,322,14):
    for y in range(0,222,18):
      sleep(0.0025)
      F(x,y,13,17,choice(["grey","white",(150,140,135),(201,205,200)]))
  F(0,70,322,110,"black")
  while not keydown(KEY_OK):
    STR("GAME OVER",100,100,"white","black")
    STR("SCORE: "+str(round(score)),100,130,"cyan","black")
  F(0,0,322,222,"black")

#platforms to jump on
plateforms_color=(randint(0,255),randint(0,255),randint(0,255))
shoot_color=(randint(150,255),randint(150,255),randint(150,255))
trainee=False
level=0

class Entity():
  def __init__(self,x,y,width,height,speed,ma=True):
    self.x=x
    self.y=y
    self.width=width
    self.height=height
    self.speed=speed
    self.move_alone=ma
    self.chute=0
    self.gx=self.x+int(self.width/2)
    self.gy=self.y+self.height
    self.dead=False
    self.drawing=[]
    self.shooting=None

  def set_drawing_inverse(self):
    self.drawing_inverse=[None,]*len(self.drawing)
    for i in range(len(self.drawing)):
      j=self.drawing[i]
      self.drawing_inverse[i]=[self.width-j[0]-j[2],j[1],j[2],j[3],j[4]]

  def contact(self,x1,y1,x2,y2):
    if self.x<=x1<=self.x+self.width or self.x<=x2<=self.x+self.width:
      if self.y<=y1<=self.y+self.height or self.y<=y2<=self.y+self.height:
        return True
    return False

  def step(self):
    x=self.x
    y=self.y
    self.move()
    if self.x!=x or self.y!=y or len(shoot):
      F(x,y,20,30,backColor)
      self.draw()
    if is_shooting:
      draw_shoot((0,)*3)
#assign global variables..
  def move(self):
    global level,energy,lives
    if is_shooting: draw_shoot(backColor)
    if self.chute:
      if self.chute>0:
        if self.tomber():
          self.y+=self.chute
          self.chute+=1
        else:
          self.y=plateforms[self.get_plateform()][1]-self.height
#player dies
        if self.y+self.height>lava_y+4 or time_2<=0:
          self.dead=True
          lives-=1
#6 spaces for this else          
      else:
        self.y+=self.chute
        self.chute+=1

    if self.move_alone:
      self.x+=self.speed
      if self.tomber():
        self.speed*=-1
        self.x+=self.speed
    else:
      x=(keydown(3)-keydown(0))

      if x:
        self.speed=abs(self.speed)*x
        self.x+=self.speed
# Jump
      if keydown(KEY_BACKSPACE) and not self.tomber():
        self.y=plateforms[self.get_plateform()][1]-self.height
# and height of jump..
        self.chute=-10
      if (not self.tomber()):
        if self.contact(points[1][0],points[1][1],points[1][0]+20,points[0][1]+5):
          level+=1
          self.dead=True
          
    if self.tomber() and self.chute==0:
      self.chute=1
  def tomber(self):
    self.gx=self.x+int(self.width/2)
    self.gy=self.y+self.height
    for i in plateforms:
      if self.gy<=i[1]<=self.gy+self.chute:
        if i[0]<=self.gx<=i[0]+i[2]:
          return False
    return True

  def get_plateform(self):
    if not self.tomber():
      for i in plateforms:
        if self.gy<=i[1]<=self.gy+self.chute:
          if i[0]<=self.gx<=i[0]+i[2]:
            return plateforms.index(i)

  def draw(self):
    if self.speed<=0:
      for i in self.drawing:
        F(self.x+i[0],self.y+i[1],i[2],i[3],i[4])
    else:
      for i in self.drawing_inverse:
        F(self.x+i[0],self.y+i[1],i[2],i[3],i[4])
#Plateforms    
def draw_all_plateforms(color):
  for i in plateforms:
    F(i[0],i[1],i[2],i[3],color)
#Entites
def draw_all_entities():
  for i in entities:
    i.move()
    i.draw()
#Shoot
is_shooting=None
shooting_direction=1
shoot=[]
shooting_list=[]

for i in range(20):
  shooting_list+=[[int((15**2-(i-10)**2)**0.5),i-10],]

def get_shoot(e,shooting):
    return [e.gx,e.y+8+10,
    shooting_list[shooting][0]*((e.speed>0)*2-1),
    shooting_list[shooting][1],0]
#shoot aim moves in curve..
def draw_shoot(color):
  shoot_color="cyan"
  c=get_shoot(avatar,is_shooting)
  F(c[0]+c[2]*2-5,c[1]+c[3]*2-2,9,9,color)
#Screen when died or when complete level
def new_area_screen():
  F(0,0,322,222,"black")
  while not keydown(KEY_OK):
    STR("Life: "+str(round(lives)),60,60,"gray","black")
    STR("Press [OK] to conitue",70,120,"white","black")
#player loss 1 chance (life)  
def death_animation():
  F(0,0,322,222,"black")
  for x in range(0,322,16):
    for y in range(0,222,5):
      sleep(0.0006)
      F(x,y,15,4,choice(["white","black"]))
  
  F(0,70,322,110,"black")
  while not keydown(KEY_OK):
    STR("Life: "+str(round(lives)),100,90,"cyan","black")
    STR("Press [OK] to conitue",70,120,"black","cyan")
# portals on left and right
# 2 teleport stations
points=[]
def draw_all_points():
#start
  F(points[0][0],points[0][1]-5,20,5,"cyan")
#goal
  F(points[1][0],points[1][1]-5,20,5,(R(0,50),R(200,250),R(200,250)))
maps=[
#[spawn/end,monsters,plateforms(,txt)]
  [  
#[x,y] coordinate of: 
#platform and portal.  
    [[10,200],[280,200]],
    [],
    [
#[x,y,w,h] values of: plateforms
      [0,200,322,10],
    ],
  ],[
    [[10,200],[10,100]],
    [],
    [
      [10,200,100,10],
      [140,200,100,10],
      [170,150,100,10],
      [10,100,100,10],
    ],
  ],
  [
    [[10,160],[280,80]],
    [[100,100,20,30,5],],
    [
      [10,160,50,10],
      [100,130,100,10],
      [200,80,100,10],
    ],
  
  ],
  [
    [[0,120],[280,80]],
    [
      [130,100,20,30,5],
      [160,100,20,30,5],
      [190,100,20,30,5],
    ],[
      [0,120,50,10],
      [100,130,140,10],
      [200,80,100,10],
    ],
  ],
[[[10, 190], [270, 90]], [[180, 100, 20, 30, 5]], [[10, 190, 130, 10], [140, 140, 100, 10], [240, 90, 50, 10]]],
[[[10, 170], [270, 170]], [[90, 140, 20, 30, 5]], [[10, 170, 100, 10], [170, 170, 120, 10]]],
[[[20, 40], [270, 200]], [[270, 100, 20, 30, 5], [100, 80, 20, 30, 5], [180, 130, 20, 30, 5], [160, 10, 20, 30, 5], [50, 50, 20, 30, 5], [230, 170, 20, 30, 5]], [[220, 200, 70, 10], [220, 130, 70, 10], [160, 160, 60, 10], [90, 110, 40, 10], [140, 40, 60, 10], [40, 80, 40, 10], [20, 40, 50, 10]]],
[[[120, 200], [290, 50]], [[190, 120, 20, 30, 5], [110, 120, 20, 30, 5], [100, 70, 20, 30, 5], [200, 70, 20, 30, 5], [150, 70, 20, 30, 5], [80, 20, 20, 30, 5], [120, 20, 20, 30, 5], [180, 20, 20, 30, 5], [220, 20, 20, 30, 5]], [[100, 150, 120, 10], [90, 100, 140, 10], [70, 50, 180, 10], [120, 200, 80, 10], [280, 50, 30, 10]]],
[[[40, 130], [240, 130]], [[70, 0, 20, 30, 5], [90, 10, 20, 30, 5], [110, 20, 20, 30, 5], [130, 30, 20, 30, 5], [150, 40, 20, 30, 5], [170, 50, 20, 30, 5], [190, 60, 20, 30, 5], [210, 70, 20, 30, 5], [230, 80, 20, 30, 5]], [[40, 130, 220, 10],]],
[[[10, 200], [280, 40]], [[250, 10, 20, 30, 5], [190, 150, 20, 30, 5], [260, 100, 20, 30, 5], [120, 70, 20, 30, 5], [30, 20, 20, 30, 5], [140, -10, 20, 30, 5]], [[240, 40, 60, 10], [10, 200, 80, 10], [180, 180, 50, 10], [240, 130, 70, 10], [100, 100, 70, 10], [2990, 210, 20, 10], [10, 50, 60, 10], [110, 20, 90, 10]]],
[[[20, 190], [280, 70]], [[160, 110, 20, 30, 5], [70, 70, 20, 30, 5], [150, 20, 20, 30, 5], [110, 160, 20, 30, 5]], [[20, 190, 270, 10], [140, 140, 70, 10], [50, 100, 60, 10], [130, 50, 60, 10], [230, 70, 70, 10]]],
  [
    [[10,200],[290,200]],
    [],
    [
      [10,200,300,10],
    ],"**[ You completed this game! ]**"
  ]
]

def spawn(level):
  global avatar,entities,plateforms,shoot,points,txt,game
  F(0,0,320,222,backColor)
  shoot=[]
  if level==len(maps):
    game=False
    return
  map=maps[level]
  points=map[0]
  plateforms=map[2]
  avatar=Entity(points[0][0],points[0][1]-30,20,30,10,False)
  avatar.drawing=[
#player
  (3,0,17,30,(R(0,100),R(0,100),R(0,100))),
  (0,3,12,10,"cyan"),
  ]
  avatar.set_drawing_inverse()
  entities=[avatar,]
  for i in map[1]:
    enemy_c=choice([
    "black",(135,0,0),(0,135,0),
    (0,0,135),(135,135,135),
    (65,25,95),(95,25,65),
    (R(0,255),R(0,255),R(0,255))
    ])
#enemy width and height    
    enemy_w=R(11,17)
#original height=30
    enemy_h=R(25,34)
    entities+=[Entity(i[0],i[1],i[2],i[3],i[4]),]
    entities[-1].drawing=[
#Original width,height=17,30
    (3,0,enemy_w,enemy_h,enemy_c),
#enemy mask
    (0,3,12,10,choice(["white",
    "cyan","black","green",
    (120,0,0),(36,25,32),
    (R(0,255),R(0,255),R(0,255))
    ])),
    ]
    entities[-1].set_drawing_inverse()

  if len(map)==4:
    STR(map[3],20,20,(150,)*3,backColor)

  avatar.draw()
spawn(level)
time_start=monotonic()
game=True

while game:
  
  if avatar.dead:
    death_animation()
    spawn(level)
    draw_ground()

  for i in range(len(entities)-1,0,-1):
    for j in range(len(shoot)-1,-1,-1):
      if entities[i].contact(shoot[j][0]-5,shoot[j][1]-5,shoot[j][0]+5,shoot[j][1]+5):
        F(entities[i].x,entities[i].y,20,30,backColor)
        F(shoot[j][0]-5,shoot[j][1]-5,10,10,backColor)
        del entities[i]
        del shoot[j]
        break
  if keydown(KEY_TOOLBOX) or is_shooting!=None:
    if keydown(KEY_TOOLBOX):
      if is_shooting==None:
        is_shooting=1
      draw_shoot(backColor)
      is_shooting+=shooting_direction

      if is_shooting==len(shooting_list)-1 or is_shooting==0:
        shooting_direction*=-1
    elif is_shooting!=None:
      shoot+=[get_shoot(avatar,is_shooting),]
      shoot[-1][0]+=shoot[-1][2]
      shoot[-1][1]+=shoot[-1][3]
      draw_shoot(backColor)
      is_shooting=None
  for i in entities:
    i.step()
  if not trainee:
    for i in shoot:
      F(i[0]-5,i[1]-5,10,10,backColor)
  for i in range(len(shoot)-1,-1,-1):
    shoot[i][0]+=shoot[i][2]
    shoot[i][1]+=shoot[i][3]
    shoot[i][3]+=1
    if shoot[i][1]>222:
      del shoot[i]
      continue
    F(shoot[i][0]-5,shoot[i][1]-5,10,10,shoot_color)
#player collides into
  for i in entities[1:]:
    if i.contact(avatar.x,avatar.y,avatar.x+avatar.width,avatar.y+avatar.height) and shield_on==False:
      avatar.dead=True


  STR("Time: "+str(round(time)),0,0,"black","cyan")
  STR("Energy: "+str(round(energy)),110,0,"black","orange")      
  STR("Life: "+str(round(lives)),240,0,"black","yellow")      

#use the dot to access x,y,width,height
# Player losses energy fast
  if avatar.x+avatar.width>rect_x and avatar.x<rect_x+rect_w and avatar.y+avatar.height>rect_y and avatar.y<rect_y+rect_h and shield_on==False:
    F(avatar.x,avatar.y,avatar.width,avatar.height,"red")
    energy-=2
    avatar.x-=5
#for a few seconds,can jump higher!
  if avatar.x+avatar.width>food_x and avatar.x<food_x+food_w and avatar.y+avatar.height>food_y and avatar.y<food_y+food_h:
    F(avatar.x,avatar.y,avatar.width,avatar.height,"pink")
    F(food_x,food_y,food_w,food_h,backColor)
    food_y=R(-2000,-1500)
    food_x=R(2,310)
    lives+=0.5
    score+=R(50,100)      

  if avatar.x+avatar.width>shield_x and avatar.x<shield_x+shield_w and avatar.y+avatar.height>shield_y and avatar.y<shield_y+shield_h:
    F(avatar.x,avatar.y,avatar.width,avatar.height,"cyan")
    F(shield_x,shield_y,shield_w,shield_h,backColor)
    while not keydown(KEY_OK):
      STR("Press [UP]+[Backspace]",20,50,"black","cyan")
      STR("to Super Jump!!",20,70,"black","cyan")
      STR("(Press [OK] to continue)",70,100,"black","yellow")
    
    STR("                       ",20,50,backColor,backColor)
    STR("               ",20,70,backColor,backColor)
    STR("                          ",70,100,backColor,backColor)
    sleep(0.3)
    time+=1
    lives+=0.25
    score+=R(200,500)
    shield_on=True
    shield_y=R(-3000,-2000)
    shield_x=R(2,300)
    shield_c=(R(0,255),R(0,255),R(0,255))
  if shield_on:
    F(avatar.x,avatar.y,2,avatar.height,(R(0,255),R(0,255),R(0,255)))
    F(avatar.x+avatar.width-2,avatar.y,2,avatar.height,(R(0,255),R(0,255),R(0,255)))
    F(avatar.x,avatar.y,avatar.width,2,(R(0,255),R(0,255),R(0,255)))
    F(avatar.x,avatar.y+avatar.height-2,avatar.width,2,(R(0,255),R(0,255),R(0,255)))
    color=(R(0,255),R(0,255),R(0,255))
    STR("Shield:"+str(round(shield_time)),110,20,"black",(235,240,220))
    shield_time-=0.04
    
  if shield_time<=0:
    shield_time=5
    shield_on=False
    STR("          ",110,20,backColor,backColor)
  
  if energy<=0:
    lives-=1
    energy=25
#Pause/Resume code
  if keydown(KEY_VAR):
    while not keydown(KEY_OK):
      STR("( PAUSE )",120,60,"black","cyan")  
      STR("(PRESS [OK] KEY TO RESUME)",20,90,"black","yellow")  
    STR("          ",120,60,backColor,backColor)  
    STR("                               ",20,90,backColor,backColor)  
      
  F(rect_x,rect_y,rect_w,rect_h,rect_c)
  F(rect_x+rect_w,rect_y,7,rect_h,backColor)
#border on rect  
  F(rect_x,rect_y,2,rect_h,"blue")
  F(rect_x+rect_w-2,rect_y,2,rect_h,"blue")
  F(rect_x,rect_y,rect_w,2,"blue")
  F(rect_x,rect_y+rect_h-2,rect_w,2,"blue")
# temporary shield
  F(shield_x,shield_y,shield_w,shield_h,shield_c)
  F(shield_x,shield_y-4,shield_w,4,backColor)
  F(shield_x,shield_y,2,shield_h,"white")
  F(shield_x+shield_w-2,shield_y,2,shield_h,"white")
  F(shield_x,shield_y,shield_w,2,"white")
  F(shield_x,shield_y+shield_h-2,shield_w,2,"white")
#player outer color
  if shield_on==False:
    F(avatar.x,avatar.y,2,avatar.height,"black")
    F(avatar.x+avatar.width-2,avatar.y,2,avatar.height,"black")
    F(avatar.x,avatar.y,avatar.width,2,"black")
    F(avatar.x,avatar.y+avatar.height-2,avatar.width,2,"black")
  
  F(food_x,food_y,food_w,food_h,food_c)
  F(food_x,food_y-3,food_w,3,backColor)
  F(food_x,food_y,2,food_h,"pink")
  F(food_x+food_w-2,food_y,2,food_h,"pink")
  F(food_x,food_y,food_w,2,"pink")
  F(food_x,food_y+food_h-2,food_w,2,"pink")
  food_y+=3

  if food_y+food_h>=lava_y:
#    food_y=lava_y-food_h
    food_y-=2
    food_c=(R(100,255),0,0)
  if food_y>222:
    food_y=R(-1200,-1000)
    food_x=R(2,310)

  rect_x-=7
  shield_y+=4

  if shield_y+shield_h>=lava_y:
   # shield_y=lava_y-shield_h
    shield_y-=1
  if shield_y>222:
    shield_y=R(-1000,-800)
    shield_x=R(2,300)
    shield_c=(R(0,255),R(0,255),R(0,255))
#rectangle enemy reaches left side of screen        
  if rect_x+rect_w<0-R(5,10):
    rect_x=R(400,600)
    rect_y=R(10,198-rect_h)
    rect_w=R(10,30)
    rect_h=R(16,85)
    rect_c=choice([
    (R(0,125),R(0,125),R(0,125)),
    "red","white","yellow"
    ])

  time-=0.005  

  if time<=0 or lives<=0.9:
    game_over_draw()
    game=False
  if keydown(KEY_UP) and shield_on:
    avatar.y-=4
  food_x-=R(-4,4)

  F(food_x-4,food_y,4,food_h,backColor)
  F(food_x+food_w,food_y,4,food_h,backColor)
#player bullet colors are random
  shoot_color=(R(0,255),R(0,255),R(0,255))

  draw_all_points()
  draw_all_plateforms(plateforms_color)

  if avatar.y+avatar.height>=202:
    avatar.y=202-avatar.height
    draw_ground()
    avatar.y-=6
    lives-=0.02
    F(0,0,60,20,backColor)
#this is the speed of game loop.
  sleep(0.04)  
if time<=0:
  print("\nYou ran out of time.\n")
if lives<=0:
  print("\nYou ran out of chances.\n")


#********The End....or is it ?...******

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.