plat.py

Created by yannis300307

Created on September 03, 2025

3.27 KB


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

ppa="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-*/():;%=<>?![]{}_"

# Tiles
tiles=[
'onmmmnnnoonn 006mnmmmn 007mnmmmmnmmmn 007mnmnommmnmmmmnmmmonommmnmmmmmnmmmon 006mommmn 006mommmomn 008mommmmmomnmmn 007mnmomommnmnmnmmmmmnnnmmnmmmmnnmmmnmmmnnmmoooonmmmmnmmo 007mnmmmmmnon 008mommnmmmmnmommmmommmmn'
'mmmnoommommmnnnmnmmoonnmmo',
"ee 012deee 014de 016d 011eddeee 048deeedd 011e 048d 011eddeee 032deeedd 011e 016d",
" 256J",
' 086Jeee 012JeeeeeJe 008J 009e 006J 011eJJJJ 012eJJJJ 011e 006J 010e 007JeeJ 006e 010Jeee 038J',
]
# Sprites
img={
"player":' 021awwwww 010awRRRR 010awRRRgR 011aRRRRR 012aRRRR 013aR 013axxxxx 011axxwxx 011axxwxx 011axxwxx 011attttt 011attttt 011ataaat 011ataaat 011attaatt 006a',
}

# Maps
lvl=[
' 040de 014de 022deddde 021de 036dedddde 020dccc 025dccccdddcccdddddc 024deddddbbbbbdddedddddbb',
]

def decompress(d):
  r,c="",0
  while c<len(d):
    if d[c]==" ":
      r+=d[c+4]*int(d[c+1:c+4])
      c+=5
    else:
      r+=d[c]
      c+=1
  return r

class Renderer:
  def __init__(s,en):
    s.pa=[]
    s.en=en
  def update(s,f):
    if f==1:
      s.redraw_part(s.en.old_p_x//16,s.en.old_p_y//16+1,2,4)
    elif f==2:
      s.render_map(s.en.map)
      
    s.render_player()
  def render_player(s):
    s.draw_img(s.en.player_x+32,s.en.player_y,decompress(img["player"]),2)
  def redraw_part(s,xx,yy,w,h):
    ys=-32
    xs=32
    for x in range(xx,xx+w):
      for y in range(yy,yy+h):
        if x>=0 and x<16 and y>=0 and y<16:
          t=ppa.index(s.en.map[x+y*16])
          if t:s.draw_img(x*16+xs,y*16+ys,tiles[t-1],1)
  def render_map(s,m):
    ys=-32
    xs=32
    for x in range(16):
      for y in range(16):
        t=ppa.index(m[x+y*16])
        if t:s.draw_img(x*16+xs,y*16+ys,tiles[t-1],1)
  def draw_img(s,xx,yy,d,z):
    si=int(sqrt(len(d)))
    for y in range(si):
      yi=si*y
      for x in range(si):
        i=ppa.index(d[x+yi])
        if i:fill_rect(xx+x*z,yy+y*z,z,z,s.pa[i])
  def gen_c_pa(s,r,v,b,n,br=0,bv=0,bb=0):
    sb=255//n
    for i in range(1,n+1):
      s.pa.append(color(br+r*sb*i,bv+v*sb*i,bb+b*sb*i))
  def gen_pa(s):
    for i in [(0,0,0),(0,0,0),(85,85,85),(170,170,170),(255,255,255)]:
      s.pa.append(i) 
    s.gen_c_pa(1,0,0,5)
    s.gen_c_pa(0,1,0,5)
    s.gen_c_pa(0,0,1,5)
    s.gen_c_pa(1,1,0,5)
    s.gen_c_pa(1,0,1,5)
    s.gen_c_pa(1,1,1,5)
    s.gen_c_pa(1,0,0,5,0,255,255)
    s.gen_c_pa(0,1,0,5,255,0,255)
    s.gen_c_pa(0,0,1,5,255,255,0)
    s.gen_c_pa(1,1,0,5,0,0,255)
    s.gen_c_pa(1,0,1,5,0,255,0)

class Engine:
  def __init__(s):
    s.renderer=Renderer(s)
    s.map=""
    s.player_x=0
    s.player_y=16*(16-5)
    s.old_p_x=0
    s.old_p_y=0
  def load_map(s,nbr):
    s.map=decompress(lvl[nbr])
  def update(s,st=False):
    if st:
      m=2
    else:
      m=s.controls()
    s.renderer.update(m)
  def controls(s):
    speed=5
    x=keydown(KEY_RIGHT)-keydown(KEY_LEFT)
    y=keydown(KEY_DOWN)-keydown(KEY_UP)
    s.old_p_x=s.player_x
    s.old_p_y=s.player_y
    s.player_x+=x*5
    s.player_y+=y*5
    
    return x!=0 or y!=0

  def loop(s):
    s.update(True)
    while True:
      s.update()

for t in range(len(tiles)):
  tiles[t]=decompress(tiles[t])

en=Engine()
en.renderer.gen_pa()
en.load_map(0)
en.loop()
#en.renderer.draw_img(decompress(),3)

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.