chute3_0.py

Created by rubimath40

Created on May 13, 2025

8.84 KB


from kandinsky import *
from ion import *
from time import *

backColor=(255,)*3
plateforms_color=(0,0,200)
death_color=(0,0,0)
shoot_color=(255,100,0)
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):
      fill_rect(x,y,20,30,backColor)
      self.draw()
    if is_shooting:
      draw_shoot((0,)*3)
  def move(self):
    global level
    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
        if self.y>222:
          self.dead=True
      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
      if keydown(KEY_UP) and not self.tomber():
        self.y=plateforms[self.get_plateform()][1]-self.height
        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:
        fill_rect(self.x+i[0],self.y+i[1],i[2],i[3],i[4])
    else:
      for i in self.drawing_inverse:
        fill_rect(self.x+i[0],self.y+i[1],i[2],i[3],i[4])
#Plateforme    
def draw_all_plateforms(color):
  for i in plateforms:
    fill_rect(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]
def draw_shoot(color):
  c=get_shoot(avatar,is_shooting)
  fill_rect(c[0]+c[2]*2-5,c[1]+c[3]*2-2,10,10,color)
#Dead animation
def death_animation():
  for y in range(11):
    for x in range(16):
      fill_rect(x*20,y*20,20,24,death_color)
      sleep(0.003)
#points
points=[]
def draw_all_points():
  fill_rect(points[0][0],points[0][1]-5,20,5,(255,150,100))
  fill_rect(points[1][0],points[1][1]-5,20,5,(100,100,255))
#Spawn
maps=[
#[spaw/end,monsters,plateforms(,txt)]
[[[10,200],[290,200]],[],[[10,200,300,10],],"utilisez les fleches\n pour vous deplacer"],
[[[10,200],[10,100]],[],[[10,200,100,10],[140,200,100,10],[170,150,100,10],[10,100,100,10],],"utilisez la fleche\n du haut pour sauter"],
[[[10,160],[280,80]],[[100,100,20,30,5]],[[10,160,50,10],[100,130,100,10],[200,80,100,10]],"appuyez sur OK pour tirer"],
[[[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]],"restez appuye pour viser"],
[[[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]]],
[[[80, 190], [280, 190]], [[220, 160, 20, 30, 5]], [[80, 190, 220, 10]]],
[[[10, 30], [210, 130]], [[380, 100, 20, 30, 5], [60, 100, 20, 30, 5], [200, 100, 20, 30, 5], [180, 100, 20, 30, 5], [160, 100, 20, 30, 5], [80, 80, 20, 30, 5], [140, 90, 20, 30, 5], [120, 100, 20, 30, 5], [100, 100, 20, 30, 5], [60, 50, 20, 30, 5], [40, 60, 20, 30, 5], [20, 80, 20, 30, 5]], [[-10, 130, 220, 10], [200, 130, 20, 10], [210, 130, 20, 10], [10, 30, 20, 10]]],
[[[20, 40], [270, 210]], [[270, 180, 20, 30, 5]], [[0, 120, 320, 10], [20, 40, 20, 10], [-20, 210, 340, 10]]],
[[[40, 130], [60, 130]], [[120, 100, 20, 30, 5]], [[40, 130, 40, 10]]],
[[[40, 60], [40, 180]], [[120, 150, 20, 30, 5]], [[40, 180, 220, 10]]],
[[[10,200],[290,200]],[],[[10,200,300,10],],"Vous avez gagne !"]
]
def spawn(level):
  global avatar,entities,plateforms,shoot,points,txt,run
  fill_rect(0,0,320,222,backColor)
  shoot=[]
  if level==len(maps):
    run=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=[
  (3,0,17,30,(255,0,0)),
  (0,3,12,10,(200,200,200)),
  ]
  avatar.set_drawing_inverse()
  entities=[avatar,]
  for i in map[1]:
    entities+=[Entity(i[0],i[1],i[2],i[3],i[4]),]
    entities[-1].drawing=[
    (3,0,17,30,(0,255,0)),
    (0,3,12,10,(200,200,200)),
    ]
    entities[-1].set_drawing_inverse()
  if len(map)==4:
    draw_string(map[3],20,20,(150,)*3,backColor)
  avatar.draw()
spawn(level)
time_start=monotonic()
run=True
while run:
  if avatar.dead:
    death_animation()
    spawn(level)
  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):
        fill_rect(entities[i].x,entities[i].y,20,30,backColor)
        fill_rect(shoot[j][0]-5,shoot[j][1]-5,10,10,backColor)
        del entities[i]
        del shoot[j]
        break
  if keydown(KEY_OK) or is_shooting!=None:
    if keydown(KEY_OK):
      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:
      fill_rect(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
    fill_rect(shoot[i][0]-5,shoot[i][1]-5,10,10,shoot_color)
  for i in entities[1:]:
    if i.contact(avatar.x,avatar.y,avatar.x+avatar.width,avatar.y+avatar.height):
      avatar.dead=True
  draw_all_points()
  draw_all_plateforms(plateforms_color)
  sleep(0.05)
print("Gagne !")
print("Temps:")
print(round(monotonic()-time_start,3)," s")
#Everyone !
#You can change all features (go to top) 
#You can also create levels with
#level_maker.py (the same creator) and put
#it at line 225

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.