cube_game.py

Created by wperez274

Created on February 03, 2022

6.81 KB

“cubegame.py”

key [left/right] = move key [OK] = Pause/Resume key [Backspace] = Jump key [Toolbox] = Shoot

Good luck!

Sincerely,

Wilson


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

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=(R(0,H),R(0,H),R(0,H))

dopen=False

edash=False
et=0
echase=False
edir=0
#enemys energy
ee=R(20,30)
ex=R(300,400)
ey=R(40,100)
ew=R(15,30)
eh=R(20,40)
ec=(R(0,H),R(0,H),R(0,H))

#door
dx=-10
dy=-10
dw=9
dh=20
dc=(35,20,0)


def pause():
  STR("(PAUSED)",110,100,(R(0,255),R(0,255),R(0,255)),bg)
  while K(KEY_OK):
    pass
  while not K(KEY_OK):
    pass
  while K(KEY_OK):
    STR("        ",110,100,bg,bg)
    pass


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

#side is the size or width
side=17
#player x 
x=15
#grd height
y_floor=R(140,200)

#player height i guess..
#player y = height - side i guess?
y=y_floor-side
# just a counter for the rgb effect on the cube


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

F(0,0,322,222,bg)

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,1,1,0,1,0,0,0,2,1,1,0,0,0],
[0,2,0,0,2,1,1,1,1,0,2,1,1,1,1,0,0],
[2,1,1,1,1,1,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)

#grd
y_grd1=y_floor+(222-y_floor)//3
#grass on dirt
y_grd2=215
#3 different colors for 3 different things
cherbe=GREEN
cgrd1=DARKGREEN
cgrd2=BLACK

#ground
def show_grd():
  
  F(0,y_floor,320,222-y_floor,cherbe)
  F(0,y_grd1,320,222-y_grd1,cgrd1)
  
  for i in range(80):#320
    random = R(0,20)
    h =random*(y_grd2-y_grd1)//20
    
    F(i*4,y_grd1,20,20,cherbe)
  
  for i in range(190):#190
    xgrd,ygrd=R(0,320),R(y_grd2-4,222)
    F(xgrd,ygrd,3,3,cgrd2)
  L_col=[RED,DARKRED,BLACK]
  
  for i in range(320):#320
    cgrd,xgrd,ygrd=L_col[R(0,2)],R(0,320),R(y_floor,y_grd1)
    F(xgrd,ygrd,2,2,cgrd)

#grd random pattern grass
show_grd()

ccarre=BLACK
#right
Direction=1
shoot=True

for i in range(0,322):
    i+=1
    lavay=R(200,220)
    F(i,lavay,4,40,(R(0,50),R(0,50),R(0,50)))

#main loop
while game:

  et+=0.1
  
  STR("Lives:"+str(round(pl)),4,0,CYAN,bg)
  STR("Score:"+str(psc),90,0,GREEN,bg)
  STR("Energy:"+str(round(pe)),230,0,DARKRED,bg)
  
  F(ex,ey,ew,eh,ec)    
  
  
  if et>R(8,12):
    et=0
    echase=choice([0,1,1,])
  
  #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 ee<10:
    F(ex,ey,ew,eh,RED)

  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 keydown(KEY_OK):
    pause()
  
  precedent_y=y

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

#player shoots
  if keydown(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(keydown(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]
              F(i[0],i[1],i[3], i[4], (0,0,0))
              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 echase:
    if y<ey:
      edir=3
    if y>ey:
      edir=4    
  
  if ex+ew<-R(10,20):
    world+=1
    ee=R(15,40)
    ew=R(15,60)
    eh=R(15,80)
    ex=R(250,400)
    ey=R(30,150)
    ec=(R(0,100),R(0,100),R(0,100))
    psc+=R(25,100)
    pe+=2
    bg=(R(0,H),R(0,H),R(0,H))
    echase=False
    F(0,0,322,222,bg)
    F(ex,ey,ew,eh,ec)
    show_grd()
    show_clouds()
    for i in range(0,322):
      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 keydown(KEY_TOOLBOX) and y+4>=ey and y+4<=ey+eh and x>ex and pdir==1:
    F(ex,ey,ew,eh,RED)
    psc+=1
    pe+=0.02
    F(0,0,322,20,bg)
  if keydown(KEY_TOOLBOX) and y+4>=ey and y+4<=ey+eh and x<ex and pdir==2:
    F(ex,ey,ew,eh,RED)
    psc+=1
    pe+=0.02
    F(0,0,322,20,bg)
   
    
    
for i in range(50):
    for j in range(50):
      i=R(x,x+side)
      j=R(y,y+side)
      F(i,j,R(2,5),R(2,5),choice([BLACK,DARKRED,RED]))
  
F(0,0,322,222,BLACK)
STR("GAME OVER",100,100,WHITE,BLACK)      
STR("SCORE:"+str(psc),100,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.