square5.py

Created by wperez274

Created on July 06, 2022

7.75 KB

game

fun.


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=1
game=True
bg=(R(0,H),R(0,H),R(0,H))

#player
psc=0
pe=9
pl=3

pc=(R(0,H),R(0,H),R(0,H))

dopen=False

edash=False
etimer=0
echase=False
edir=0
#enemys energy
ee=R(100,300)
ex=R(100,180)
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 spawnNewWorld():
  pass
  
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


booljump,compteur_saut,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=15
#player x 
x=15
#grd height
y_floor=R(120,180)

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

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

F(0,0,320,222,bg)

score=0
#variable for the square: length of each part
c_1=side//7
c_2=side//12
c_3=side-c_1*2-c_2*2
#player colors 
c_col_1=(R(0,H),R(0,H),R(0,H))
c_col_3=(R(0,H),R(0,H),R(0,H))

def show_square():
  c_1=side//7
  c_2=side//8
  c_3=side-c_1*2-c_2*2

  F(x,y,side,c_1,c_col_1)
  F(x,y+side-c_1,side,c_1,c_col_1)
  F(x,y+c_1,c_1,c_3+c_2*2,c_col_1)
  F(x+side-c_1,y+c_1,c_1,c_3+c_2*2,c_col_1)
  F(x+c_1,y+c_1,side-2*c_1,c_2,c_carre)
  F(x+c_1,y+c_1+c_2+c_3,side-2*c_1,c_2,c_carre)
  F(x+c_1,y+c_1+c_2,c_2,c_3,c_carre)
  F(x+c_1+c_2+c_3,y+c_1+c_2,c_2,c_3,c_carre)
  F(x+c_1+c_2,y+c_1+c_2,side-(c_1+c_2)*2,side-(c_1+c_2)*2,c_col_3)

#carre  
def hsv_to_rgb(h, s=1.0, v=1.0):#rgb effect, feel free to use it in your own project
      h = (h%360)/360
      if s == 0.0: v*=255; return (v, v, v)
      i = int(h*6.) # XXX assume int() truncates!
      f = (h*6.)-i; p,q,t = int(255*(v*(1.-s))), int(255*(v*(1.-s*f))), int(255*(v*(1.-s*(1.-f)))); v*=255; i%=6
      if i == 0: return (v, t, p)
      if i == 1: return (q, v, p)
      if i == 2: return (p, v, t)
      if i == 3: return (p, q, v)
      if i == 4: return (t, p, v)
      if i == 5: return (v, p, q)
#  
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

##### SKY PART

offset=0

L_clouds=[[0]*9+[2,1,1]+[0]*9,#here you can easily change the aspect of clouds (2 is meant to be directly placed at the left of 1)   {1 = a square of a cloud and 2 is the part to erase}
[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,c_cloud)
      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
c_herbe=GREEN
c_grd1=DARKGREEN
c_grd2=BLACK

def show_grd():
  
  F(0,y_floor,320,222-y_floor,c_herbe)
  F(0,y_grd1,320,222-y_grd1,c_grd1)
  
  for i in range(80):#320
    random = R(0,20)
    h =random*(y_grd2-y_grd1)//20
    
    F(i*4,y_grd1,4,h,c_herbe)
  
  for i in range(190):#190
    xgrd,ygrd=R(0,320),R(y_grd2-4,222)
    F(xgrd,ygrd,3,3,c_grd2)
  L_col=[(88,140,0),(0,88,24),(0,168,0)]
  
  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()

c_carre=BLACK

F(x,y,side,side,c_carre)
Direction=1
bool_shoot=True

#main loop
while game:
  etimer+=0.1
  if etimer>R(8,12):
    etimer=0
    echase=choice([0,1,1,1])
    

  draw_string("World:"+str(world),100,2,BLACK,bg)
  draw_string(":["+str(round(pl)),16,2,WHITE,bg)
  draw_string(":[",240,2,GREEN,bg)
  F(259,6,round(ee/4),10,RED)
 
  F(2,3,7,13,pc)
  F(4,5,5,5,CYAN)

  F(226,3,round(ew/4),round(eh/3),ec)
  F(227,6,5,4,WHITE)

  #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 dopen:
    F(dx,dy,dw,dh,(R(0,255),R(0,255),R(0,255)))
    F(dx,dy+dh,dw,2,WHITE)

  
  F(ex,ey,ew,eh,ec)
  F(ex+2,ey+4,8,4,choice([BLACK,WHITE,DARKGREEN]))
  
  
  if ey+eh>y_floor:
    ey=y_floor-eh 
  
  if x+side>=ex and x<=ex+ew and y+side>=ey and y<=ey+eh:
    F(x,y,side,side,RED)
    sleep(0.001)
    pe-=0.3
    side-=round(0.2)
    ee+=0.1
    edir=R(1,4)
        
  if pl<2:
    F(x,y,side,side,bg)
  if ee<50:
    F(ex,ey,ew,eh,"gray")
  if pe<1:
    pe=9
    pl-=1
  if pe>9:
    pe=1
    pl+=1

#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):
    Direction=-1
    if x>0:
      fill_rect(x+side-2,y,2,side,bg)
      x-=2
  
  elif keydown(KEY_RIGHT):
    Direction=1
    if x+side<=320:
      fill_rect(x,y,2,side,bg)
      x+=2
#jump  
  if keydown(KEY_BACKSPACE):
    if y==y_floor-side: 
      booljump=True

#player shoots
  if keydown(KEY_TOOLBOX):
    if bool_shoot:
      bool_shoot=False

      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)):
    bool_shoot=True  
#i guess there is limit of  
#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
  
              fill_rect(x_eff_bullet,i[1],v_bullet,i[4],bg)
              i[0]+=v_bullet*i[2]
              fill_rect(i[0],i[1],i[3], i[4], (0,0,0))
              fill_rect(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, c_carre)
          
  if booljump:
      y=y_floor-side-L_jump[compteur_saut]
      compteur_saut+=1
      
      if compteur_saut==len(L_jump):
        compteur_saut=0
        booljump=False

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

  show_square()
  show_clouds()
  c+=1
  c_carre=hsv_to_rgb(c)


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

#enemy chases


  if echase:
    if y<ey:
      edir=3
    if y>ey:
      edir=4    
  
  
  if ex+ew<-R(10,20):
    ew=R(15,60)
    eh=R(15,80)
    ex=R(250,400)
    ey=R(20,120)
    ec=(R(0,50),R(0,50),R(0,50))
    psc+=R(5,20)
    pl+=0.2
    world+=1
    bg=(R(200,H),R(200,H),R(200,H))
    F(0,0,322,222,bg)
    show_grd()
    show_clouds()
    
  ex-=1
  F(ex+ew,ey,1,eh,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]))
  

STR("GAME OVER",100,100,RED,bg)      
STR("SCORE:"+str(psc),100,140,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.