plateformer.py

Created by ziii

Created on August 31, 2022

4.04 KB

A basic 2d plateformer, there are some bugs but it globally works well.

You need to download the map too, it’s the program called “map_t”.

How to play :

Left and right arrows to move

Ok to jump, you can jump once in the air (shown by the red color of the “character”), you can also jump on walls.

You can grip walls but you have a stamina bar, its level is shown by the darkness of you character, the darker the lower your stamina is.

There is no end (didn’t bother program one) and no death (spikes are annoying to program).

It is posssible to create maps but the system is not perfect and a bit messy, i’ll maybe work on it if I have time.


from kandinsky import fill_rect as f
from ion import *
from time import *
import map_t as map
def draw_lvl():
  f(0,0,320,222,COLORS[0])
  for x in range(len(lvl)):
    for y in range(len(lvl[0])):
      if lvl[x][y]:
        f(x*10,y*10,10,10,COLORS[1])
def load_map(side,p,spos):
  global screen,lvl,pos,pos_st
  try:
    screen=map.sides[screen][side]
    lvl=map.screen(screen)
    draw_lvl()
    pos=p
    pos_st=[320,222]
  except KeyError:
    pos=spos
COLORS=((255,255,255),(0,0,0))
pos=map.fpos
screen=0
SIZE=(8,15)
SPEED=50
JUMP=-100
dash=1
GRAVITY=200
state=0
stamina=1
vel=[0,0]
pos_st=[0,0]
tick=[monotonic(),1]
player_color=(255,0,0)
pressed=0
lvl=map.screen(0)
a=[0,GRAVITY]
draw_lvl()
while 1:
  if monotonic()-tick[0]>=1/40:
    if state==0:
      a[0]=(-vel[0])*2
    if keydown(KEY_LEFT):
      if state==1:
        vel[0]=-SPEED
      elif state==0 or state>1:
        a[0]=(-SPEED-vel[0])*1.5
    if keydown(KEY_RIGHT):
      if state==1:
        vel[0]=SPEED
      elif state==0 or state>1:
        a[0]=(SPEED-vel[0])*1.5
    if keydown(KEY_OK):
      if pressed==0:
        if state==1:
          vel[1]=JUMP
        elif dash and state==0:
          dash=0
          vel=[-SPEED*keydown(KEY_LEFT)+SPEED*keydown(KEY_RIGHT),JUMP]
        elif state==2:
          vel=[SPEED,JUMP]
        elif state==3:
          vel[1]=JUMP            
          vel[0]=-SPEED
        pressed=1
    else:
      pressed=0
    state=0
    tick=[monotonic(),monotonic()-tick[0]]
    stamina_st=stamina
    vel[0]+=(a[0]*tick[1])
    vel[1]+=(a[1]*tick[1])
    pos_st=tuple(pos)
    pos[0]+=vel[0]*tick[1]
    a=[0,GRAVITY]
    try:
      if vel[0]<=0: 
        if pos[0]<0:
          load_map("left",[319,pos[1]],[0,pos[1]])
        if lvl[int(pos[0]/10)][int(pos[1]/10)] or lvl[int(pos[0]/10)][int((pos[1]+SIZE[1]/2)/10)]:
          pos[0]+=(10-pos[0]%10)
          vel[0]=-0.001
          state=2
          if keydown(KEY_UP) and stamina>0:
            vel[1]=-SPEED
            stamina-=tick[1]/5
          elif keydown(KEY_DOWN) and stamina>0:
            vel[1]=0
            stamina-=tick[1]/5
        elif lvl[int(pos[0]/10)][int((pos[1]+SIZE[1]-1)/10)]:
          pos[0]+=(10-pos[0]%10)
          state=2
          if keydown(KEY_UP) and stamina>0:
            vel[1]=0
            stamina-=tick[1]/5
      elif vel[0]>0:
        if pos[0]>319:
          load_map("right",[1-SIZE[0],pos[1]],[319,pos[1]])
        if lvl[int((pos[0]+SIZE[0])/10)][int(pos[1]/10)] or lvl[int((pos[0]+SIZE[0])/10)][int((pos[1]+SIZE[1]/2)/10)]:
          pos[0]-=((pos[0]+SIZE[0])%10)
          vel[0]=0.001
          state=3
          if keydown(KEY_UP) and stamina>0:
            vel[1]=-SPEED
            stamina-=tick[1]/5
          elif keydown(KEY_DOWN) and stamina>0:
            vel[1]=0
            stamina-=tick[1]/5
        elif lvl[int((pos[0]+SIZE[0])/10)][int((pos[1]+SIZE[1]-1)/10)]:
          pos[0]-=((pos[0]+SIZE[0])%10)
          state=3
          if keydown(KEY_UP) and stamina>0:
            vel[1]=0
            stamina-=tick[1]/5
    except IndexError:
      pass
    pos[1]+=vel[1]*tick[1]
    try:
      if vel[1]<0: 
        if pos[1]<0:
          load_map("top",[pos[0],219],[pos[0],0])
        if lvl[int((pos[0]+SIZE[0]-1)/10)][int(pos[1]/10)] or lvl[int(pos[0]/10)][int(pos[1]/10)]:
          pos[1]+=(10-pos[1]%10)
          vel[1]=0
      elif vel[1]>0: 
        if pos[1]>219:
          load_map("bottom",[pos[0],1-SIZE[1]],[pos[0],219])
        if lvl[int((pos[0]+SIZE[0]-1)/10)][int((pos[1]+SIZE[1])/10)] or lvl[int(pos[0]/10)][int((pos[1]+SIZE[1])/10)]:
          pos[1]-=(pos[1]+SIZE[1])%10
          vel[1]=0
          state=1
          dash=1
          stamina=1
    except IndexError:
      pass
    if dash:
      player_color=(int(100+(155*stamina)),0,0)
    else:
      player_color=(0,0,int(100+155*stamina))
    if int(pos[0])!=int(pos_st[0]) or int(pos_st[1])!=int(pos[1]) or int(stamina_st*155)!=int(stamina*155):
      f(int(pos_st[0]),int(pos_st[1]),SIZE[0],SIZE[1],(255,255,255))
      f(int(pos[0]),int(pos[1]),SIZE[0],SIZE[1],player_color)
    if state==1:
      vel[0]=0