platformer_sprite.py

Created by aidanplews2013

Created on September 14, 2020

3.19 KB


from kandinsky import *

from ion import *

from time import *

from math import *
fr=monotonic()
c1=[255,0,0]
c2=[255,153,51]
c3=[0,0,255]
map=[126,255,254,255,126,124,254,108]
c=[c1,c1,c2,c2,c2,c1,c3,c3]
dd=[0,0,0,126,255,124,254,108]
ddc=[c1,c1,c1,c1,c1,c1,c3,c3]

jj=[126,255,254,255,126,254,124,108]
jjc=[c1,c1,c2,c2,c2,c3,c3,c3]

def cubecol(x,y,xvel,yvel,onground,x1,y1,x2,y2):



  if x+10>x1 and x-10<x2 and y+10-yvel>y1 and y-10-yvel<y2:

    if yvel<0:

      y=y1-10

      onground=1

    else:

      y=y2+10

    yvel=0

  if y+10>y1 and y-10<y2 and x+10+xvel>x1 and x-10+xvel<x2:

    onground=1

    x=x-xvel

  return (x,y,xvel,yvel,onground)

    



def player(x,y,state):

  if state==0:

    #fill_rect(x-10,y-10,20,20,color(255,100,0))
    for i in range(8):
      nut=('{0:08b}'.format(map[i]))  
      for o in range(8):                        
        if int(nut[o])==1:
          fill_rect(x-10+((o)*2),y-10+((i)*2),2,2,(c[i]))
 
    #fill_rect(x-10,y-10,20,20,color(0,100,0))



    

  elif state==1:
    #fill_rect(x-10,y-10,20,20,color(255,255,255))  

    
    for i in range(8):
      nut=('{0:08b}'.format(jj[i]))  
      for o in range(8):                        
        if int(nut[o])==1:
          fill_rect(x-10+((o)*2),y-10+((i)*2),2,2,(jjc[i]))
  

  elif state==-1:
    fill_rect(x-10,y-10,20,20,color(255,255,255))  
    for i in range(8):
      nut=('{0:08b}'.format(dd[i]))
  
      for o in range(8):
        #if int(nut[o])==0:
          #fill_rect(x-10+((o)*2),y-10+((i)*2),2,2,(255,255,255))
      
        if int(nut[o])==1:
        
          fill_rect(x-10+((o)*2),y-10+((i)*2),2,2,(ddc[i]))
         
    



def noplayer(x,y,state):

  if state==-1:

   

      

    fill_rect(x-20,y-5,40,10,color(255,255,255))

  else:

   if yvel!=0 or xvel!=0 or onground==0: 

    fill_rect(x-10,y-10,20,20,color(255,255,255))  



def zero(n):

  if n>0:

    return 1

  if n<0:

    return -1

  if n==0:

    return 0





x=100

xvel=0

onground=1

y=50

yvel=10

time=0

maxvel=5

state=0



fill_rect(100,70,100,60,color(0,0,0))



while (1):

  if keydown(KEY_LEFT)and xvel>-maxvel:

    xvel=xvel-1

  elif keydown(KEY_RIGHT)and xvel<maxvel:

    xvel=xvel+1

  else:

    xvel=xvel-zero(xvel)

    

  noplayer(x,y,state)

    

  if keydown(KEY_UP):
    #fill_rect(x-10,y-10,20,20,color(255,255,255))  
    if state!=1:

      if onground==1:
        fill_rect(x-10,y-10,20,20,color(255,255,255))  

        state=1

        maxvel=0

        onground=0

        yvel=10
        fill_rect(x-10,y-10,20,20,color(255,255,255))  

    else:

      onground=0

      state=1

  elif keydown(KEY_DOWN):

    state=-1

    maxvel=10

    onground=0

  else:

    maxvel=5

    state=0

  if keydown(KEY_OK) and onground==1:
    fill_rect(x-10,y-10,20,20,color(255,255,255))  
    yvel=10

  else:

    if state!=1:

      yvel=yvel-1

    elif yvel>0:

      yvel=yvel-0.5

  onground=0

  x,y,xvel,yvel,onground=cubecol(x,y,xvel,yvel,onground,0,200,320,300)

  x,y,xvel,yvel,onground=cubecol(x,y,xvel,yvel,onground,100,70,200,130)

  x=x+xvel

  y=y-int(yvel)

  player(x,y,state)

  

  if monotonic()<(fr+(1/30)):
    sleep((fr+(1/30))-monotonic())
    fr=monotonic()
  fr=monotonic()