2013/02/15

Cascade Demo

This is a demo.
Drops is falling from above until it is full.
It repeat from the beginning.
If you want to stop, tap "[X]".



/******************************************************************/
/* Cascade Demo
/******************************************************************/

/* These are variables. These are not constant. So they can not use for dim size or case value.
int WX ://=(80/2)
int WY ://=24
int MAX =75  :/* number of shelves
int LNG =5  :/* max length of cascade

/* internal character
int WALL='#' :/* wall
int BALL='o' :/* drop
int TANA='=' :/* shelf
int SPC =' '    :/* space

int dx,dy :/* move direction
int clng :/* move pointer
int retY :/* second return value is returned via the global variable

dim int drpx(5)   :/* move x position buffer (LNG)
dim int drpy(5)   :/* move y position buffer (LNG)
dim char tvram(96,96) :/* virtual video RAM
      :/* Size is variable depending on the model, It set aside the maximum value.

int wtt  :/* wait

/******************************************************************/
/* main routine
/******************************************************************/

getWidth(WX,WY):// get text screen size(system set)
setUpScroll(NO):// disable to up scroll (V2.0)
WX=WX/2
MAX=(WX*WY/13)
int ts,t
/*int c
 srand2() /* random initialize
 wtt=CalcWait()/100*8:/* calc wait time (8ms)
 repeat
  InitScreen() :/* screen initialize
  if Cascades() then break:/* move cascade
  beep()
  locate(8,7)
  if isLocalizeJapan() then {
   print "もう一度見ますか? ";
  } else {
   print "Watch again ?";
  }
 /* c='Y';
  ts =time()
  repeat:/* automatic repeat after 15 seconds
   t=time()
  until t-ts>=15
 until NO :/*(c<>'Y' && c<>CR)
end

/******************************************************************/
/* below are internal functions
/******************************************************************/

func InitScreen()
/* make initial screen
int i,j,x,y
 cls()
 print "  CASCADES"
 init_tvram()

 /* make outside wall
 for y=1 to WY-2:/* vertical
  storexy(   0,y,WALL)
  storexy(WX-1,y,WALL)
 next
 for x=0 to WX-1:/* horizontal
  storexy(x,WY-1,WALL)
 next

 /* make shelf
 for i=0 to MAX-1
  x=1+rand2(WX-2) :/* Except both ends
  y=2+rand2(WY-3) :/* Except for the bottom line and the top and the next
  for j=0 to 3
   if (x+j>=WX-1) then break /* x-over
   if (scrn(x+j,y)=SPC) then storexy(x+j,y,TANA)
  next
 next
 // full screen display
 printScreenAll(1,WY-1)
endfunc

/******************************************************************/

func int Cascades()
/* cascade move
int x,y
//int ts=time():/* start time
//int t,dt=0
 while CheckDropPoint()=0
  /* if there is open
  /* set initial position
  x=SetFirstPoint()
  y=1
  repeat
   //t=time()
   //if t-ts>dt then { :/* change second
   // dt=dt+1
   // locate(30,0):print dt;
   //}
   //if inkey()=ESC then return(1)

   /* move and display
   Drop(x,y)
   if clng=0 then break
   clng=clng-1
   x=drpx(clng)
   y=drpy(clng)
  until x=0 and y=0
 endwhile
 /* if top line is full then terminate
 return(0)
endfunc

/******************************************************************/

func int CheckDropPoint()
/* check top line open
int x
 for x=1 to WX-2
  if scrn(x,1)=SPC then return(0) :/* open
 next
 return(1) :/* close
endfunc

/******************************************************************/

func int SetFirstPoint()
/* start point setting (top line)
int i,x
 x=1+rand2(WX-2):/* set start point
 while scrn(x,1)<>SPC
  x=x+1
  if (x=WX-1) then x=1
 endwhile
 clng=LNG-1
 for i=0 to clng-1
  drpx(i)=0
  drpy(i)=0:/* 0=non record symbol
 next
 drpx(clng)=x
 drpy(clng)=1
 dx=0
 dy=1: /* to down
 return(x)
endfunc

/******************************************************************/

func Drop(x;int,y;int)
/* move and display
int r,l
 while (YES)
  /* display
  printxy(x,y,BALL)
  TWait(wtt):/* wait
  
  /* Is bottom open?
  if scrn(x,y+1)=SPC then {
   /* down fall
   dx=0
   dy=1
   x=Move(x,y)
   y=retY
   continue
  }
  
  /* check destination
  if scrn(x+dx,y+dy)=SPC then {
   /* enable to move
   x=Move(x,y)
   y=retY
   continue
  }
  
  /* can't move the destination
  if (dy=1) then { :/* fall down
   /* check left and right
   r=(scrn(x+1,y)=SPC):/* right
   l=(scrn(x-1,y)=SPC):/* left
   
   /* change left or right move if it can move
   dx=0
   if (r) then { :/* right side is open
    dx=1
   }
   if (l) then { :/* left side is open
    dx=-1
    if (r) then { :/* right side is open too
     /* select left or right
     if rand2(2)=1 then dx=1
    }
   }
   if dx<>0 then {:/* enable to  move
    dy=0
    x=Move(x,y)
    y=retY
    continue
   }
  }
  /* if can't move anywhere then loop out
  break
 endwhile
endfunc

/******************************************************************/

func int Move(x;int,y;int)
/* move and erase
int i
 if (drpy(0)<>0) then printxy(drpx(0),drpy(0),SPC):/* erase previous position
 x=x+dx
 y=y+dy
 if (clng>=1) then {
  for i=1 to clng
   drpx(i-1)=drpx(i)
   drpy(i-1)=drpy(i)
  next
 }
 drpx(clng)=x
 drpy(clng)=y
 retY=y:/* second return value is returned via the global variable
 return(x)
endfunc

/******************************************************************/
/* virtual text VRAM system
/******************************************************************/

func str printChar$(c;char)
/* get display characters
str c$
 switch (c)
  case ' ': c$= " ":break :/* erase
  case 'o': c$= "●":break :/* drops
  case '#': c$= "■":break :/* wall
  case '=': c$= "□":break :/* shelf
 endswitch
 return (c$)
endfunc

func printxy(x;int,y;int,c;char)
/* display characters
 /* output for virtual text VRAM
 tvram(x,y)=c

 /* position transrate
 locate(x*2,y)
 
 /* display with character transrate
 print printChar$(c);
endfunc

func storexy(x;int,y;int,c;char)
/* output for virtual text VRAM
 tvram(x,y)=c
endfunc

func char scrn(x;int,y;int)
/* read virtual text VRAM
 return(tvram(x,y))
endfunc

func printScreenAll(stY;int,edY;int)
/* full screen display
/* X-BASIC for iOS's text display is so slow. Therefore this function is measured.
/* Remnant of V1.0
int x,y
str linestr
 for y=stY to edY
  linestr=""
  for x=0 to WX-1
   linestr=linestr+printChar$(tvram(x,y))
  next
  locate(0,y):print linestr;
 next
endfunc

func init_tvram()
/* virtual text VRAM initialize
int x,y
 for y=0 to WY-1
  for x=0 to WX-1
   tvram(x,y)=SPC
  next
 next
endfunc

/******************************************************************/
/* misc function
/******************************************************************/

func srand2()
/* rand() initialize
int tm
 t=time()
 tm=tm and &h0fff
 tm=tm*16
 srand(tm)
endfunc

func int rand2(seed;int)
/* generate random number
 return (rand() mod seed)
endfunc

/******************************************************************/

func int CalcWait()
/* calc wait
int tim,tm
int count
 /* Zero clock check
 tm=time()
 repeat
  tim=time()
 until tim-tm<>0
 count=0
 repeat
  tm=time()
  count=count+1
 until tm-tim<>0
 return(count):/* count number per 1 second
endfunc

/******************************************************************/

func TWait(wt;int)
int i,tm
 for i=0 to wt-1
  tm=time()
 next
endfunc

/******************************************************************/

Zip archive file : XBetc.zip

No comments:

Post a Comment