chute.py

Created by wperez274

Created on April 22, 2023

8 KB

Not my game.. Which is why the code is good… :-) My code is simple. This code is complex.

There are benefits to both simple and complex code.


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

backColor=(0,0,0)
plateforms_color=(0,255,0)
death_color=(0,0,0)
shoot_color=(255,255,255)
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 move_all_shoot():
#  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]


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],[10,100]],
    [],
    [
      [10,200,100,10],
      [140,200,100,10],
      [170,150,100,10],
      [10,100,100,10],
    ],"utilisez les fleches\n pour vous deplacer, celle du\n haut pour sauter"
  ],[
    [[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],
    ],"appuyez sur OK pour tirer,\n restez appuye pour viser"
  ],
[[[20, 170], [280, 60]], [[60, 30, 20, 30, 5], [20, -10, 20, 30, 5]], [[10, 170, 40, 10], [60, 130, 20, 10], [20, 100, 20, 10], [60, 60, 20, 10], [20, 20, 20, 10], [260, 160, 20, 10], [290, 120, 20, 10], [230, 80, 20, 10], [280, 60, 20, 10]]],
[[[30, 130], [290, 60]], [[100, 100, 20, 30, 5], [30, -50, 20, 30, 5], [100, -50, 20, 30, 5], [180, -140, 20, 30, 5]], [[20, 130, 40, 10], [110, 130, 10, 10], [190, 180, 10, 10], [310, 140, 10, 10], [260, 100, 10, 10], [290, 60, 20, 10]]],
[[[20, 200], [260, 120]], [[160, 170, 20, 30, 5], [160, 170, 20, 30, 5], [160, 170, 20, 30, 5], [20, 0, 20, 30, 5], [100, -50, 20, 30, 5], [100, -90, 20, 30, 5], [240, -270, 20, 30, 5]], [[10, 200, 40, 10], [110, 170, 10, 10], [170, 200, 10, 10], [250, 200, 10, 10], [300, 160, 10, 10], [260, 120, 20, 10]]],
[[[10, 180], [290, 100]], [[130, 180, 20, 30, 5], [130, 180, 20, 30, 5], [130, 180, 20, 30, 5], [150, 180, 20, 30, 5], [170, 180, 20, 30, 5], [10, 0, 20, 30, 5], [210, 0, 20, 30, 5], [260, 0, 20, 30, 5]], [[0, 180, 40, 10], [120, 210, 70, 10], [220, 210, 10, 10], [270, 170, 10, 10], [240, 130, 10, 10], [290, 100, 20, 10]]],

  [
    [[10,200],[290,200]],
    [],
    [
      [10,200,300,10],
    ],"Vous avez gagne !"
  ]
]

def spawn(level):
  global avatar,entities,plateforms,shoot,points,txt

  fill_rect(0,0,320,222,backColor)
  shoot=[]
  if level==len(maps):
    print("Gagne !")
    print("Temps:")
    print(round(monotonic()-time_start,3)," s")
    raise BaseException('Le jeu est fini...')
  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()
while True:
  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.025)

#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.