Author Topic: Randomly generated maze and game question  (Read 3984 times)

codeLYOKO

  • Guest
Does anybody know a code to use in GLBasic that generates a random 5000X5000 three D maze each time the program is loaded, has a start and finish point, and you're able to try to solve it by moving through it by moving the mouse. I tried using the 3DMaze thing included with GLBasic, but 1. It doesn't generate randomly everytime you load it. 2. has no finish point. 3. is not 5000X5000. 4. uses a txt file to create the maze. (I don't want to have to plot the maze in a txt file. I want GLBasic to generate the maze randomly for me.) and 5. It desplays the maze map (the thing that tells you the architecture of the maze). And if all possible, would anybody also know a code that when you get close to the finish, the corridor to the finish point is bright, almost like you're in a cave, and the exit is bright.

Also, if possible, can anybody, in addition to the maze code, give me a code for life points. Example. At the beginning of the game you start out with 100 life points, but there is three creatures that follow you through the maze shooting lasers at you every 5 sconds that take off 10 life points if you get hit by the laser, and if you lose all of the life points, then the game is over. And you can destroy the creatures by firing your laser at them 5 times by clicking the mouse, and the place the laser strikes is determined by where the curser is.

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Randomly generated maze and game question
« Reply #1 on: 2006-Apr-01 »
Take a good look at the maze-demo program. See where it reads the map. Understand it! Then, make a maze 100x100 (5000x5000 is incredibly huge!) full of walls. Next, randomly remove wall-blocks. The see, if you can reach the end from the start, using FINDPATH command. I did this once. I can post a demo here.

The monsters should be other values in the maze, inserted randomly after the exit was found. Then you have to insert them in the ShowAll function.

It's pretty easy if you know how, but you're a very beginner, so start with the easy things.

I'd start by adding monsters in the text-file and display them.

codeLYOKO

  • Guest
Randomly generated maze and game question
« Reply #2 on: 2006-Apr-02 »
ok, thanks

Offline Kitty Hello

  • code monkey
  • Administrator
  • Prof. Inline
  • *******
  • Posts: 10661
  • here on my island the sea says 'hello'
    • View Profile
    • http://www.glbasic.com
Randomly generated maze and game question
« Reply #3 on: 2006-Apr-02 »
OK. Found it. It's very ugly code, though...
Code: GLBasic [Select]
// --------------------------------- //
// Project:
// Start: Saturday, April 02, 2005
// IDE Version: 2.50322

GLOBAL screenx, screeny
GETSCREENSIZE screenx, screeny

maxx=32
maxy=16

redo:
DIM maze[maxx+1][maxy+1]
DIM sol[0]

// Make a grid:
// ###########
// # # # # # #
// ###########
// # # # # # #
// ###########
FOR x=1 TO maxx-1 STEP 2
        FOR y=1 TO maxy-1 STEP 2
                maze[x][y]=1
        NEXT
NEXT

// Now open some paths from the already open places right or down
        kills = 0
WHILE FINDPATH(maze[], sol[], .1, 1,1,  maxx-1, maxy-1) = FALSE
        x=0; y=0; dx=1; dy=0
        WHILE maze[x][y]=0 OR maze[x+dx][y+dy]<>0
                x=INTEGER(xrand(maxx-2)/ 2)*2+1
                y=INTEGER(  RND(maxy-2)/ 2)*2+1

                dx = RND(1)
                dy = 1-dx
                IF x=maxx-1; dy=1; dx=0; ENDIF
                IF y=maxy-1; IF x=maxx-1; dx=0; dy=0; ELSE; dy=0; dx=1; ENDIF; ENDIF
        WEND
        maze[x+dx][y+dy] = 1
        kills = kills +1
        GOSUB ShowIt // - show what it's doing
        SHOWSCREEN
        IF kills/(maxx*maxy) > 0.28 THEN GOTO redo // too high kill-ratio -> too easy level
WEND
FILLRECT 0,0, screenx,screeny, RGB(0,0,255)
GOSUB ShowIt
SHOWSCREEN
MOUSEWAIT
GOTO redo

// Return a random number [0; maxval], but add more weight on 0 or maxval
FUNCTION xrand: maxval
LOCAL cfac, phi, val1, val2
        cfac = .7 // 0 = random, 1 = random, but more weight at head+tail
        phi = 360 * RND(10000)/10000
        val1 = (1+COS(phi)) / 2 // cosinus part - more weight at head+taile
        val2 = ABS(1-phi/180)   // linear part  - even weighted
        // interpolate between both types
        RETURN INTEGER(0.1 + maxval * (cfac*val1 + (1-cfac)*val2))
ENDFUNCTION

// Display the playfield
SUB ShowIt:
LOCAL x,y,dx,dy
dx = screenx/(maxx+2)
dy=screeny/(maxy+2)
FOR x=0 TO maxx
        FOR y=0 TO maxy
                IF (x=1 AND y=1) OR (x=maxx-1 AND y=maxy-1) THEN FILLRECT x*dx, y*dy, (x+1)*dx, (y+1)*dy, RGB(0xff,0,0)
                IF maze[x][y]=0 THEN FILLRECT x*dx, y*dy, (x+1)*dx, (y+1)*dy, RGB(0xff,0xff,0xff)
        NEXT
NEXT

ENDSUB