maze walk

Game development specific discussions.
Post Reply
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

maze walk

Post by bluatigro »

opject of the game :
walk trou enless mazes

error :
if you solve the first maze the second is not created

Code: Select all

''bluatigro 5 jan 2017
''maze test

#include "color.bas"
#include "_maze2d.bas"
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB 
#endif


screen 20 , 32

create_new_maze 0 , 30 , 20
maze( mazemaxx - 1 , mazemaxy - 1 ) = 2

dim as integer x , y , tel = 1 , px = 1 , py = 1 , dx , dy

do
  screenlock
    cls
    for x = 0 to mazemaxx
      for y = 0 to mazemaxy
        if maze( x , y ) = 1 then
          line ( x * 32 , y * 32 ) - step ( 31 , 31 ) , blue , bf
        end if
        if maze( x , y ) = 2 then
          circle ( x * 32 + 16 , y * 32 + 16 ) , 7 , green ,,,, f
        end if
      next y
    next x
    circle ( px * 32 + 16 , py * 32 + 16 ) , 10 , yellow ,,,, f
  screenunlock
  dx = 0
  dy = 0
  if multikey( sc_up ) then dy = -1
  if multikey( sc_down ) then dy = 1
  if multikey( sc_left ) then dx = -1
  if multikey( sc_right ) then dx = 1
  if maze( px + dx , py + dy ) = 1 then
    dx = 0
    dy = 0
  end if
  px += dx
  py += dy
  if px = mazemaxx - 1 _
  and py = mazemaxy - 1 then
    px = 1
    py = 1
    create_new_maze tel , 30 , 20
    tel += 1
  end if
  sleep 250
loop until inkey = chr( 27 )
    

Code: Select all

''bluatigro 5 jan 2017
''game lib : 2d maze generation

type wall
  as integer wallx, wally, roomx, roomy
end type

dim shared as integer mazemaxx = 10
dim shared as integer mazemaxy = 10

dim shared as integer maze( mazemaxx , mazemaxy )
dim shared as wall walls( mazemaxx * mazemaxy )
dim shared as integer maxwall


sub create_new_maze( seed as integer , w as integer , h as integer )
  mazemaxx = w
  mazemaxy = h
  redim as integer maze( mazemaxx , mazemaxy )
  redim as wall walls( mazemaxx * mazemaxy )
  maxwall = 0

  dim as integer x , y

  for x = 0 to mazemaxx
    for y = 0 to mazemaxy
      maze( x , y ) = 1
    next y
  next x
  
  if seed >= 0 then
    randomize seed
  else
    randomize timer
  end if

  walls( 0 ).roomx = 1
  walls( 0 ).roomy = 1
  walls( 0 ).wallx = 1
  walls( 0 ).wally = 1

  do
    x = int(rnd * (1 + maxwall))  
    if walls( x ).roomx >= 0 and walls( x ).roomx <= mazemaxx _
    and walls( x ).roomy >= 0 and walls( x ).roomy <= mazemaxy then
      if maze( walls( x ).roomx , walls( x ).roomy ) then
        maze( walls( x ).roomx , walls( x ).roomy ) = 0
        maze( walls( x ).wallx , walls( x ).wally ) = 0
      
        walls( maxwall + 1 ).wallx = walls( x ).roomx - 1
        walls( maxwall + 1 ).wally = walls( x ).roomy
        walls( maxwall + 1 ).roomx = walls( x ).roomx - 2
        walls( maxwall + 1 ).roomy = walls( x ).roomy
      
        walls( maxwall + 2 ).wallx = walls( x ).roomx
        walls( maxwall + 2 ).wally = walls( x ).roomy + 1
        walls( maxwall + 2 ).roomx = walls( x ).roomx
        walls( maxwall + 2 ).roomy = walls( x ).roomy + 2
      
        walls( maxwall + 3 ).wallx = walls( x ).roomx
        walls( maxwall + 3 ).wally = walls( x ).roomy - 1
        walls( maxwall + 3 ).roomx = walls( x ).roomx
        walls( maxwall + 3 ).roomy = walls( x ).roomy - 2
      
        walls( maxwall + 4 ).wallx = walls( x ).roomx + 1
        walls( maxwall + 4 ).wally = walls( x ).roomy
        walls( maxwall + 4 ).roomx = walls( x ).roomx + 2
        walls( maxwall + 4 ).roomy = walls( x ).roomy
      
        maxwall += 4
      end if
    end if
  
    walls( x ).wallx = walls( maxwall ).wallx
    walls( x ).wally = walls( maxwall ).wally
    walls( x ).roomx = walls( maxwall ).roomx
    walls( x ).roomy = walls( maxwall ).roomy

    maxwall -= 1
  loop until maxwall = -1

end sub

Code: Select all


''bluatigro 13 feb 2015
''color.bas

''some colors consts + functions

#ifndef COLOR_H
#define COLOR_H

#include "math.bas"

''primary colors
const as ulong black      = &hff000000
const as ulong red        = &hffff0000
const as ulong green      = &hff00ff00
const as ulong yellow     = &hffffff00
const as ulong blue       = &hff0000ff
const as ulong magenta    = &hffff00ff
const as ulong cyan       = &hff00ffff
const as ulong white      = &hffffffff
''mix colors
const as ulong orange     = &hffff7f00
const as ulong gray       = &hff7f7f7f
const as ulong pink       = &hffff7f7f
const as ulong purple     = &hff7f007f
const as ulong darkRed    = &hff7f0000
const as ulong darkYellow = &hff7f7f00
const as ulong darkGreen  = &hff007f00
const as ulong darkBlue   = &hff00007f

function mix( kla as ulong , f as single , klb as ulong ) as ulong
  dim as ulong ra , ga , ba , rb , gb , bb , r , g , b 
  ra = ( kla shr 16 ) and 255
  ga = ( kla shr 8 ) and 255
  ba = kla and 255
  rb = ( klb shr 16 ) and 255
  gb = ( klb shr 8 ) and 255
  bb = klb and 255
  r = ra + ( rb - ra ) * f
  g = ga + ( gb - ga ) * f
  b = ba + ( bb - ba ) * f
  return rgb( r , g , b )
end function

function rainbow( x as single ) as ulong
  dim as ulong r , g , b
  r = sin( rad( x ) ) * 127 + 128
  g = sin( rad( x - 120 ) ) * 127 + 128
  b = sin( rad( x + 120 ) ) * 127 + 128
  return rgb( r , g , b )
end function 

function rndcolor() as ulong
  return rgb( rnd * 255 , rnd * 255 , rnd * 255 )
end function

#endif

Code: Select all

''bluatigro 30-dec-2013
''game + animation lib : 
''some math const[s] + function[s]

#ifndef MATH_H
#define MATH_H

const as Single pi    = CSng( atn( 1.0 ) * 4.0 )
const as Single golden_ratio = CSng( ( sqr(5.0) - 1.0 ) / 2.0 )

function rad( x as single ) as single
''help function degrees to radians 
  return x * pi / 180
end function

function degrees( x as single ) as single
  return x * 180 / pi
end function

function range( l as single , h as single ) as single
  return rnd * ( h - l ) + l
end function

#endif
bluatigro
Posts: 660
Joined: Apr 25, 2012 10:35
Location: netherlands

Re: maze walk

Post by bluatigro »

update :
bigger maze
scroling maze

Code: Select all

''bluatigro 5 jan 2017
''maze test

#include "color.bas"
#include "_maze2d.bas"
#include "fbgfx.bi"
#if __FB_LANG__ = "fb"
  Using FB 
#endif


screen 20 , 32

create_new_maze 0 , 100 , 100
maze( mazemaxx - 1 , mazemaxy - 1 ) = 2

dim as integer x , y , tel = 1 , px = 1 , py = 1 , dx , dy

do
  screenlock
    cls
    for x = 0 to mazemaxx
      for y = 0 to mazemaxy
        if maze( x , y ) = 1 then
          line ( 1024/2 + x * 32 - px * 32 _
          , 768/2 + y * 32 - py * 32 ) - step ( 31 , 31 ) , blue , bf
        end if
        if maze( x , y ) = 2 then
          circle ( 1024/2 + x * 32 - px * 32 + 16 _
          , 768/2 + x * 32 - py * 32 + 16 ) , 7 , green ,,,, f
        end if
      next y
    next x
    circle ( 1024/2 + 16 , 768/2 + 16 ) , 10 , yellow ,,,, f
  screenunlock
  dx = 0
  dy = 0
  if multikey( sc_up ) then dy = -1
  if multikey( sc_down ) then dy = 1
  if multikey( sc_left ) then dx = -1
  if multikey( sc_right ) then dx = 1
  if maze( px + dx , py + dy ) = 1 then
    dx = 0
    dy = 0
  end if
  px += dx
  py += dy
  if px = mazemaxx - 1 _
  and py = mazemaxy - 1 then
    px = 1
    py = 1
    create_new_maze tel , mazemaxx , mazemaxy
    tel += 1
  end if
  sleep 250
loop until inkey = chr( 27 )
MrSwiss
Posts: 3910
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: maze walk

Post by MrSwiss »

@bluatigro,

seeing your included Math-Stuff (by color.bas), coded as Functions.
This can easily be sped up, by converting them to #Define(s) aka: 'single line' Macro(s).

It is coded as .bi file (Basic Include) instead of .bas (works the same anyway):

Code: Select all

' GFX_MATH.bi -- mathematics for fbGFX (Singles only) -- 2016, MrSwiss
' some math const(s) + macro(s) -- #Define = single line macro

#Ifndef __GFX_MATH_BI__
#Define __GFX_MATH_BI__


Const As Single PI  = Atn( 1f ) * 4f
Const As Single GoldenRatio = ( Sqr(5f) - 1f ) / 2f

Const As Single DtoR    = pi / 180f
Const As Single RtoD    = 180f / pi

#Define RAD(d)      ( CSng(d * DtoR) )
#Define DEG(r)      ( CSng(r * RtoD) )

Randomize(Timer, 3) ' seed for Rnd()
#Define RANGE(l, h) ( CSng(Rnd() * (h - l) + l) )


#EndIf ' __GFX_MATH_BI__
Best, if included at the top of *any* code file that uses fbGFX.
Post Reply