platformer3.py

Created by wperez274

Created on May 02, 2025

5.85 KB


from math import *
from kandinsky import *
from ion import *
from random import *

booljump,compteur_saut,L_saut=False,0,[int(-0.4*(x/4)**2+70) for x in range(-52,52)]+[0]
#L_saut=[int(-1*(x/6)**2+70) for x in range(-50,50)]

#length side + 
side=15
y_floor=200

x=12
y=y_floor-side
# just a counter for the rgb effect on the cube
c=0
#usually colors variable name begins with "c_" and then the object it relates to
c_sky=(randint(200,255),randint(200,255),randint(200,255))
c_cloud=(randint(0,255),randint(0,255),randint(0,255))

fill_rect(0,0,320,222,c_sky)

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=(randint(0,255),randint(0,255),randint(0,255))
c_col_3=(255,255,255)

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

  fill_rect(x,y,side,c_1,c_col_1)
  fill_rect(x,y+side-c_1,side,c_1,c_col_1)
  fill_rect(x,y+c_1,c_1,c_3+c_2*2,c_col_1)
  fill_rect(x+side-c_1,y+c_1,c_1,c_3+c_2*2,c_col_1)
  
  fill_rect(x+c_1,y+c_1,side-2*c_1,c_2,c_carre)
  fill_rect(x+c_1,y+c_1+c_2+c_3,side-2*c_1,c_2,c_carre)
  fill_rect(x+c_1,y+c_1+c_2,c_2,c_3,c_carre)
  fill_rect(x+c_1+c_2+c_3,y+c_1+c_2,c_2,c_3,c_carre)
  
  fill_rect(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):
  fill_rect(x-b,y-b,2*b+side,b,c_sky)
  fill_rect(x-b,y,b,side,c_sky)
  fill_rect(x+side,y,b,side,c_sky)
  fill_rect(x-b,y+side,2*b+side,y_floor-y-side,c_sky)
#
###laser
#length or amount i guess
L_projectiles=[]
#width and height of bullets
w_projectile=8
h_projectile=6
#velocity of bullets
v_projectile=4

##### SKY PART
offset=0#counter for clouds

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

def display_clouds():
  global L_clouds,offset
  if offset ==16:
    offset=0
    L_clouds=[[i[-1]]+i[:-1]for i in L_clouds]
  else:offset+=1
  h,l=14,16
  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
        fill_rect(x_nuage,y_nuage,l,h,c_cloud)
      if q==2:
        x_nuage,y_nuage=-1*l+offset+j*l+15,20+i*h
        fill_rect(x_nuage,y_nuage,2,h,c_sky)

#ground
y_ground1=y_floor+(222-y_floor)//3
#grass on dirt
y_ground2=215
#3 different colors for 3 different things
c_herbe=(25,111,61)
c_ground1=(160,64,0)
c_ground2=(96,48,0)

def display_ground():
  
  fill_rect(0,y_floor,320,222-y_floor,c_herbe)
  fill_rect(0,y_ground1,320,222-y_ground1,c_ground1)
  
  for i in range(80):#320
    random = randint(0,20)
    h =random*(y_ground2-y_ground1)//20
    
    fill_rect(i*4,y_ground1,4,h,c_herbe)
  
  for i in range(190):#190
    xground,yground=randint(0,320),randint(y_ground2-4,222)
    fill_rect(xground,yground,3,3,c_ground2)
  L_col=[(88,140,0),(0,88,24),(0,168,0)]
  
  for i in range(320):#320
    cground,xground,yground=L_col[randint(0,2)],randint(0,320),randint(y_floor,y_ground1)
    fill_rect(xground,yground,2,2,cground)

display_ground()
#ground

c_carre=(148, 49, 38)

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

#main loop
while True:
  
  precedent_y=y

  if keydown(KEY_LEFT):
    Direction=-1
    if x>0:
      fill_rect(x+side-2,y,2,side,c_sky)
      x-=2
  
  elif keydown(KEY_RIGHT):
    Direction=1
    if x+side<=320:
      fill_rect(x,y,2,side,c_sky)
      x+=2
  
  if keydown(KEY_BACKSPACE):
    if y==y_floor-side: 
      booljump=True
#increase size
  if keydown(KEY_PLUS):
    if side<230:
        side+=1
        y-=1
        x-=side%2
#decrease size
  elif keydown(KEY_MINUS):
    if side>8:
        fill_rect(x,y-1,side,3,c_sky)
        fill_rect(x+side-1,y+1,1,side-1,c_sky)
        if booljump:
          fill_rect(x-1,y+side-1,side+3,1,c_sky)
        
        fill_rect(x,y,2,side,c_sky)
        x+=side%2
        side-=1
        y+=1
#player shoots
  if keydown(KEY_TOOLBOX):
    if bool_shoot:
      bool_shoot=False

      if len(L_projectiles)<15:
        if Direction==1:
          L_projectiles.append([x+side+3, y+4, 1,side//3,side//4])
        else:
          L_projectiles.append([x-3-w_projectile, y+4, -1,side//3,side//4])
  elif not(keydown(KEY_TOOLBOX)):
    bool_shoot=True  
    
  if len(L_projectiles)!=0:
      for i in L_projectiles:
          if i[0]>320 or i[0]<0-i[3]:
              L_projectiles.remove(i)
          else:
              if i[2]==1:
                x_eff_projectile=i[0]
              else:
                x_eff_projectile=i[0]+i[3]-v_projectile
  
              fill_rect(x_eff_projectile,i[1],v_projectile, i[4], c_sky)
              i[0]+=v_projectile*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_saut[compteur_saut]
      compteur_saut+=1
      
      if compteur_saut== len(L_saut):
        compteur_saut=0
        booljump=False
        #y=y_sol-cote
      if precedent_y<y:
        fill_rect(x-2,y-4,side+4,4,c_sky)
      else:
        fill_rect(x-2,y+side,side+4,precedent_y-y,c_sky)
        
      display_square()

  display_square()
  display_clouds()
  c+=1
  c_carre=hsv_to_rgb(c)
  

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.