cube_game_7.py

Created by wperez274

Created on February 01, 2023

7.13 KB

simple game. laser shoot, clouds, level increase, pause.


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


#just plug in the RGB Tuples to save space
H=255

r=R(0,H)
g=R(0,H)
b=R(0,H)


#random tuple
RAND=(R(0,H),R(0,H),R(0,H))

CYAN=(0,H,H)
RED=(H,0,0)
DARKRED=(135,0,0)
DARKGREEN=(0,135,0)
WHITE=(H,H,H)
BLACK=(0,0,0)
GREEN=(0,H,0)
BLUE=(0,0,H)

SCRW=322
SCRH=222

world_count=0
world=1
game=True
bg=(R(0,H),R(0,H),R(0,H))

#player
pdir=0
psc=0
pe=9
pl=3

pc=BLUE

#enemy
ee=R(6,15)

edash=False
et=0
echase=False
edir=0

jump,count,L_jump=False,0,[int(-0.4*(x/4)**2+70) for x in range(-52,52)]+[0]

#player the size or width
side=17
#player x 
x=15
#grd height
y_floor=R(120,180)

eh=R(20,40)
ec=(R(0,H),R(0,H),R(0,H))
ex=R(300,400)
ey=y_floor-eh
ew=R(15,30)

y=y_floor-side

#clouds
ccloud=(R(220,H),R(220,H),R(220,H))

dr=False
drx=280
dry=y_floor-28
drw=22
drh=28

score=0
#variable for the square: length of each part
c1=side//7
c2=side//12
c3=side-c1*2-c2*2
#player colors 
ccol_1=(R(0,H),R(0,H),R(0,H))
ccol_3=(R(0,H),R(0,H),R(0,H))

def show_square():
  c1=side//7
  c2=side//8
  c3=side-c1*2-c2*2

  F(x,y,side,side,pc)

  
def wipe_out_previous_square(b):
  F(x-b,y-b,2*b+side,b,bg)
  F(x-b,y,b,side,bg)
  F(x+side,y,b,side,bg)
  F(x-b,y+side,2*b+side,y_floor-y-side,bg)
    
L_bullets=[]
#width and height of bullets
w_bullet=8
h_bullet=6
#velocity of bullets
v_bullet=4

offset=0

L_clouds=[[0]*9+[2,1,1]+[0]*9,
[0,0,2,2,1,1,0,0,0],
[0,2,0,0,2,1,1,0,2,1,1,1,1,0,0],
[2,1,1,1,1,1,1,0]]

h=R(5,10)
l=R(15,25)
  
def show_clouds():
  global L_clouds,offset
  
  if offset==16:
    offset=0
    L_clouds=[[i[-1]]+i[:-1]for i in L_clouds]
  else:offset+=1
  
  for i,p in enumerate(L_clouds):
    for j,q in enumerate(p):
      if q==1:
        x_nuage,y_nuage=-1*l+offset+j*l,20+i*h
        F(x_nuage,y_nuage,l,h,ccloud)
      if q==2:
        x_nuage,y_nuage=-1*l+offset+j*l+15,20+i*h
        F(x_nuage,y_nuage,2,h,bg)

cherbe=(R(0,H),R(0,H),R(0,H))
cgrd1=(R(50,H),R(50,H),R(50,H))
cgrd2=(R(0,H),R(0,H),R(0,H))
#ground

ccarre=BLUE

#pause game
def pause():
  STR("(PAUSED)",110,40,(R(0,H),R(0,H),R(0,H)),bg)
  while K(KEY_OK):
    pass
  while not K(KEY_OK):
    pass
  while K(KEY_OK):
    STR("        ",110,40,bg,bg)
    pass
    
F(0,0,322,222,bg)

#grd random pattern grass

ccarre=BLACK
#right
Direction=1
shoot=True

#main loop
F(0,y_floor,322,80,BLACK)
while game:

  et+=0.1
  
  STR("Lives:"+str(round(pl)),4,0,CYAN,bg)
  STR("World:"+str(world),105,0,BLACK,bg)
  
  STR("Score:"+str(psc),210,0,WHITE,bg)
  
  F(ex,ey,ew,eh,ec)    
  #enemy moves      
  if edir==1:
    ex-=1
    F(ex+ew,ey,1,eh,bg)    
  if edir==2:
    ex+=1
    F(ex-1,ey,1,eh,bg)
  if edir==3:
    ey-=1
    F(ex,ey+eh,ew,1,bg)    
  if edir==4:
    ey+=1
    F(ex,ey-1,ew,1,bg)

  if ey+eh>y_floor:
    ey=y_floor-eh 
#player energy loss  
  if x+side>=ex and x<=ex+ew and y+side>=ey and y<=ey+eh:
    F(x,y,side,side,RED)
    F(0,0,322,20,bg)
    
    sleep(0.001)
    pe-=0.3
    
    
  if pl<2:
    F(x,y,side,side,bg)
  
  if pe<1:
    pe=9
    pl-=1
    F(0,0,322,20,bg)
    
  if pe>9:
    pe=1
    pl+=1
    F(0,0,322,20,bg)
  

#game is over when plife < 1
  if pl<1:
    game=False
    for i in range(100):
      for j in range(100):
        i=R(x,x+side)
        j=R(y,y+side)
        F(i,j,R(2,5),R(2,5),choice([BLACK,DARKRED,RED]))
    

  if K(KEY_OK):
    pause()
  
  precedent_y=y

  if K(KEY_LEFT):
    pdir=1
    Direction=-1
    if x>0:
      F(x+side-2,y,2,side,bg)
      x-=2
  
  elif K(KEY_RIGHT):
    pdir=2
    Direction=1
    if x+side<=320:
      F(x,y,2,side,bg)
      x+=2
#jump  
  if K(KEY_BACKSPACE):
    if y==y_floor-side: 
      jump=True

#player shoots
  if K(KEY_TOOLBOX):
    if shoot:
      shoot=False
# 1 = right
      if len(L_bullets)<15:
        if Direction==1:
          L_bullets.append([x+side+3, y+4, 1,side//3,side//4])
        else:
          L_bullets.append([x-3-w_bullet, y+4, -1,side//3,side//4])
  elif not(K(KEY_TOOLBOX)):
    shoot=True  

#bullets fired (L_bullets)    
  if len(L_bullets)!=0:
      for i in L_bullets:
          #bullets reach left or right
          if i[0]>320 or i[0]<0-i[3]:
              L_bullets.remove(i)
          else:
              if i[2]==1:
                x_eff_bullet=i[0]
              else:
                x_eff_bullet=i[0]+i[3]-v_bullet
  
              F(x_eff_bullet,i[1],v_bullet,i[4],bg)
              i[0]+=v_bullet*i[2]
          #player bullets color
              F(i[0],i[1],i[3], i[4], (R(0,255),R(0,255),R(0,255)))
              F(i[0]+(i[3]//6)+1,i[1]+(i[4]//6)+1,i[3]-(i[3]//6)*2-2,i[4]-(i[4]//6)*2-2, ccarre)
          
  if jump:
      y=y_floor-side-L_jump[count]
      count+=1
      
      if count==len(L_jump):
        count=0
        jump=False

      if precedent_y<y:
        F(x-2,y-4,side+4,4,bg)
      else:
        F(x-2,y+side,side+4,precedent_y-y,bg)
        
      show_square()

  show_square()
  show_clouds()
  

  if ey<22:
    ey=22
  if ey+eh>200:
    ey=200-eh

  
  if y<ey:
    edir=choice([3,4])
  if y>ey:
    edir=choice([3,4])    
  
  if ex+ew<-R(10,20):
    ee+=R(1,3)
    ew=R(15,60)
    eh=R(15,60)
    ex=R(250,400)
    ey=R(20,y_floor-eh)
    ec=(R(0,100),R(0,100),R(0,100))
    pl-=1
    echase=False
    F(ex,ey,ew,eh,ec)
    F(x,y,side,side,RED)


  
#enemy defeated
  if ee<1 and x+side>=300 and y+side>=y_floor-30:
    ee=R(12,35)
    F(0,y_floor,322,120,(R(0,255),R(0,255),R(0,255)))
    psc+=R(25,100)
    pe+=2
    ew=R(15,60)
    eh=R(15,60)
    ex=R(220,400)
    ey=R(20,y_floor-ey+eh)
    ec=(R(0,H),R(0,H),R(0,H))
    pe+=2
    echase=False
    world+=1
    bg=(R(0,H),R(0,H),R(0,H))
    y_floor=R(120,180)
 
    x=10
    y=y_floor-side
    F(0,0,322,222,bg)
    r=R(0,H)
    g=R(0,H)
    b=R(0,H)

    F(0,y_floor,322,120,(r,g,b))
    
#enemy destroyed    
  if ee<1:
    ex+=R(1,3)
    for i in range(10):
      for j in range(10):
        i=R(ex,ex+ew)
        j=R(ey,ey+eh)
        F(i,j,R(2,5),R(2,5),choice([BLACK,bg]))
    
#door pops up    
    F(300,y_floor-30,16,30,(R(0,H),R(0,H),R(0,H)))
        
    
    for i in range(0,10):
      i+=1
      lavay=R(200,220)
      F(i,lavay,4,40,(R(0,50),R(0,50),R(0,50)))
    
  ex-=1
  F(ex+ew,ey,1,eh,bg)  


  if K(KEY_TOOLBOX) and y+4>=ey and y+4<=ey+eh and x>ex and pdir==1 and ee>=1:
    F(ex,ey,ew,eh,RED)
    F(0,0,322,20,bg)
    psc+=1
    pe+=0.04
    ee-=0.1
    
  if K(KEY_TOOLBOX) and y+4>=ey and y+4<=ey+eh and x<ex and pdir==2 and ee>=1:
    F(ex,ey,ew,eh,RED)
    F(0,0,322,20,bg)
    psc+=1
    pe+=0.04
    ee-=0.1
  
  if K(KEY_UP):
    F(x+7,0,3,y,RED)
    sleep(0.01)
    F(x+7,0,3,y,bg)
    
  if K(KEY_UP) and x+9>=ex and x+7<=ex+ew and ee>=1:
    F(ex,ey,ew,eh,RED)
    F(0,0,322,20,bg)
    psc+=1
    pe+=0.04
    ee-=0.2
  
  
  dash=R(0,2)
  if dash:
    for i in range(2):
      ex-=R(0,1)
      F(ex+ew-1,ey,2,eh,bg)

  
#player
  pc=(0,R(150,H),R(150,H))

#grass and flowers
  if y+side>=y_floor:
    y=y_floor-side
    for i in range(0.0001):
      i=R(0,322)
      F(i,y_floor+R(0,4),R(1,2),R(1,2),choice([GREEN,BLACK,RED]))

 


STR("GAME OVER",100,90,BLACK,bg)      
STR("SCORE:"+str(psc),100,120,CYAN,bg)

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.