Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - PeeJay

Pages: 1 2 [3]
31
GLBasic - en / Suggestion - RIGHT$ (and LEFT$)
« on: 2008-Jan-14 »
to join MID$ - these commands take the rightmost (or leftmost) characters from a string - very useful for leading zeroes - for example

PRINT "Score: "+RIGHT$("000000"+score,6)

would take the last 6 characters of the string, giving:

Score: 000007
Score: 000042
Score: 012345

32
GLBasic - en / Help - Type problem
« on: 2008-Jan-14 »
I'm sure this is just me being stupid, but I have a problem with the following code that I can't figure out.

It is code to do a collision detection between shots fired and enemies

Code: (glbasic) [Select]
FOREACH en IN enemies[]
LOCAL enemyhit=FALSE
FOREACH sh IN shots[]
IF SPRCOLL (2,sh.x,sh.y,3,en.x,en.y)
DELETE sh
enemyhit=TRUE
ENDIF
NEXT
IF enemyhit=TRUE
DELETE en
ENDIF
NEXT
When run, the shots are being deleted okay, but the enemies are not. Anyone know why?!?

33
Nothing earth shattering here, but just a little side project I have started working on, and this code might be useful to someone ..... feel free to use and abuse :)

Code: (glbasic) [Select]
SETSCREEN 640,480,1
LIMITFPS 60

TYPE star
x
y
z
ENDTYPE
GLOBAL stars[] AS star

SetupStars()

WHILE TRUE

UpdateStars()
DrawStars()

WEND

END

FUNCTION SetupStars:

LOCAL loop,loop2,num
FOR loop=1 TO 4
IF loop=1 THEN num=6
IF loop=2 THEN num=15
IF loop=3 THEN num=40
IF loop=4 THEN num=100
FOR loop2=1 TO num
LOCAL st AS star
st.x=RND(640)
st.y=RND(480)
st.z=loop
DIMPUSH stars[],st
NEXT
NEXT

ENDFUNCTION

FUNCTION UpdateStars:

FOREACH st IN stars[]
st.y=st.y+(1/st.z)
IF st.y>480
st.x=RND(640)
st.y=st.y-481
ENDIF
NEXT

ENDFUNCTION

FUNCTION DrawStars:

BLACKSCREEN
FOREACH st IN stars[]
LOCAL col=255-(st.z*50)
SETPIXEL st.x,st.y,RGB(col,col,col)
NEXT
SHOWSCREEN

ENDFUNCTION

34
GLBasic - en / File operations question
« on: 2008-Jan-10 »
Is there are a way to delete a file previously created with PutFile? Or rename it?

The project I am working on produces a save game state part way through, but what I hope to do is to do a simple check to test the authenticity of this file (already coded), bring up a warning message if it fails the check (coded), but then either deletes the file, or renames it, so it doesn't go through the file checking procedure again. Does that make sense?

Thanks in advance.

35
When passing values to functions, is it possible to have default values, Gernot?

To give a simple example, currently we have to use:

Code: (glbasic) [Select]
DoText(50,50,"No Default Text Allowed","","","","","")
DoText(100,100,"This is ","some text ","to draw.","","","")
DoText(100,150,"This ","is some more.","","","","")
SHOWSCREEN
KEYWAIT

FUNCTION DoText: x,y,a$,b$,c$,d$,e$,f$
a$=a$+b$+c$+d$+e$+f$
PRINT a$,x,y
ENDFUNCTION
but if you replaced that with a system that allowed default values, it becomes:

Code: (glbasic) [Select]
DoText()
DoText(100,100,"This is ","some text ","to draw.")
DoText(100,150,"This ","is some more.")
SHOWSCREEN
KEYWAIT

FUNCTION DoText: x=50,y=50,a$="Default Text",b$="",c$="",d$="",e$="",f$=""
a$=a$+b$+c$+d$+e$+f$
PRINT a$,x,y
ENDFUNCTION
where b$,c$,d$,e$,f$ would automatically by null unless a string was passed to them.
a$ would be "Default Text" unless a string was passed to it.
x and y would be 50 unless values were passed to them.

36
GLBasic - en / Suggestion - more flexible AND
« on: 2008-Jan-01 »
A minor suggestion, but one that has proved useful in the past - to be able to use AND to do actions depending on whether the operand is true - see example code:

Code: (glbasic) [Select]
x=INTEGER(RND(50))

PRINT 100,100,"Number is "+("less than 25" AND x<25)+("equal to 25" AND x=25)+("more than 25" AND x>25
or

Code: (glbasic) [Select]
x=0+(10 AND something)+(20 AND somethingelse)+(50 AND anotherthing)Just an idea for more efficient coding .....

37
This is the routine I am using to draw bitmapped fonts onto the screen. I have also added a function to enable word wrap.

Since the font is a single image, it also demonstrates a very simple way of using ImageStrips.

Code: (glbasic) [Select]
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
//                                                                      //
// Project: Bitmapped Font Printing for GLBasic                         //
//          Example of Imagestrip Usage                                 //
//          Word Wrap Function                                          //
//                                                                      //
// (C)opyright PeeJay December 30th 2007      www.peejays-remakes.co.uk //
//                                                                      //
// Code is free to use for FREEWARE projects only, though credit would  //
// still be welcomed if you decide to use it                            //
//                                                                      //
// Please contact me through my website for conditions on using this    //
// code in shareware, commercial, or other revenue generating projects  //
//                                                                      //
// This code shows an example of how you can use any bitmapped font     //
// without the limitations of the standard GL Basic routines. The font  //
// is stored in an imagestrip. There is also a simple function to allow //
// for a word wrap facility.                                            //
//                                                                      //
// Functions are currently coded for a screen width of 640, and for a   //
// font with individual character size 24x24 (all easily changed!)      //
//                                                                      //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

SETSCREEN 640,480,0 // 640x480 Window
LIMITFPS 60 // at 60 frames per second

LOADSPRITE "glfont.png",0

DrawText("PRINTING EXAMPLE",-1,20) // -1 = centralised

DrawText("LEFT",10,80)
DrawText("RIGHT",510,80)

WrapText("THIS TEXT IS AN EXAMPLE OF THE WORD WRAP FUNCTION IN ACTION",104,200,18)

SHOWSCREEN
KEYWAIT
END


FUNCTION DrawText: pjtxt$,pjtx,pjty

// Usage - text$, x co-ordinate (-1 for centralised), y co-ordinate

IF pjtx=-1 THEN pjtx=(640-LEN(pjtxt$)*24)/2
LOCAL pjloop,pjcr
FOR pjloop=0 TO LEN(pjtxt$)-1
pjcr=ASC(MID$(pjtxt$,pjloop,1))-32
VIEWPORT pjtx,pjty,24,24
DRAWSPRITE 0,0-(pjcr*24),0
VIEWPORT 0,0,0,0
pjtx=pjtx+24
NEXT

ENDFUNCTION

FUNCTION WrapText: pjw$,pjwx,pjwy,pjmax

// Usage - text$, x co-ordinate, y co-ordinate, max chars per line

LOCAL pjloop,pjtemp$

WHILE pjw$<>""
IF LEN(pjw$)<(pjmax+1)
DrawText(pjw$,pjwx,pjwy)
pjw$=""
ELSE
pjloop=pjmax
pjtemp$=pjw$
WHILE MID$(pjw$,pjloop,1)<>" "
pjloop=pjloop-1
WEND
DrawText(MID$(pjw$,0,pjloop),pjwx,pjwy)
pjwy=pjwy+24
pjw$=MID$(pjtemp$,pjloop+1,LEN(pjtemp$)-pjloop)
ENDIF
WEND

ENDFUNCTION
You'll also need the font, which is here:

http://www.peejays-remakes.co.uk/gifs/glfont.png

At the moment I have only done the uppercase letters (but only because I couldn't be bothered to add lower case ones to the image!)

Hopefully someone will find it useful!

38
Code Snippets / Tilemap Based Movement Engine
« on: 2007-Dec-27 »
Another "useful" chunk of code from my BlitzBasic days - this demonstrates how to move a player around a scrolling tilemap, and how to move the player to the edge of the tilemap at the periphery.

Code: (glbasic) [Select]
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//
//                                                                      //
// Project: Tile Based Movement Engine Example for GLBasic              //
//                                                                      //
// (C)opyright PeeJay December 13th 2007      www.peejays-remakes.co.uk //
//                                                                      //
// Code is free to use for FREEWARE projects only, though credit would  //
// still be welcomed if you decide to use it                            //
//                                                                      //
// Please contact me through my website for conditions on using this    //
// code in shareware, commercial, or other revenue generating projects  //
//                                                                      //
// This code shows an example of how a player can move around a         //
// scrolling tilemap, and how the player will move towards the edges of //
// the screen when approaching the edge of the map                      //
//                                                                      //
// Use keyboard cursor keys to move - Esc quits                         //
//                                                                      //
//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//\\//

SETSCREEN 640,480,0 // 640x480 Window
LIMITFPS 60 // at 60 frames per second

GLOBAL mapx,mapy
GLOBAL px,py
GLOBAL xdir,ydir
GLOBAL tilesize=32
GLOBAL speed=4

DIM level[50][50]

LOCAL loop

FOR loop=0 TO 49
level[0][loop]=1
level[49][loop]=1
level[loop][0]=1
level[loop][49]=1
level[RND(49)][RND(49)]=1
NEXT

mapx=10
mapy=10
px=mapx*tilesize
py=mapy*tilesize

WHILE TRUE

FOR loop=1 TO speed

CheckPlayer()

NEXT

DrawScreen()

WEND

END


FUNCTION CheckPlayer:

LOCAL xdir,ydir

xdir=KEY(205)-KEY(203)
ydir=KEY(208)-KEY(200)

IF WallDetect(px+xdir,py+ydir)=0
px=px+xdir
py=py+ydir
mapx=INTEGER(px/tilesize)
mapy=INTEGER(py/tilesize)
ENDIF

ENDFUNCTION

FUNCTION WallDetect: x,y

LOCAL mx,my,nomove

mx=INTEGER(x/tilesize)
my=INTEGER(y/tilesize)
nomove=0

IF level[mx][my]=1 THEN nomove=1

IF MOD(x,tilesize)>0
IF level[mx+1][my]=1 THEN nomove=1
ENDIF

IF MOD(y,tilesize)>0
IF level[mx][my+1]=1 THEN nomove=1
IF MOD(x,tilesize)>0
IF level[mx+1][my+1]=1 THEN nomove=1
ENDIF
ENDIF

RETURN nomove

ENDFUNCTION

FUNCTION DrawScreen:

startx=mapx-12
endx=startx+25

IF startx<0
startx=0
endx=25
ENDIF

IF endx>49
startx=24
endx=49
ENDIF

starty=mapy-12
endy=starty+25

IF starty<0
starty=0
endy=25
ENDIF

IF endy>49
starty=24
endy=49
ENDIF

IF px<304
scrx=px
ELSEIF px>((49*tilesize)-304)
scrx=304+304-(49*tilesize-px)
ELSE
scrx=304
ENDIF

IF py<224
scry=py
ELSEIF py>((49*tilesize)-224)
scry=224+224-(49*tilesize-py)
ELSE
scry=224
ENDIF

FOR loopy=starty TO endy
FOR loopx=startx TO endx
rectx=scrx-px+(loopx*tilesize)
recty=scry-py+(loopy*tilesize)
IF level[loopx][loopy]=0
DRAWRECT rectx,recty,tilesize,tilesize,RGB(0,0,64)
DRAWRECT rectx+2,recty+2,tilesize-4,tilesize-4,RGB(0,0,128)
ENDIF
IF level[loopx][loopy]=1
DRAWRECT rectx,recty,tilesize,tilesize,RGB(128,0,0)
ENDIF
NEXT
NEXT

DRAWRECT scrx,scry,tilesize,tilesize,RGB(0,128,0)

SHOWSCREEN

ENDFUNCTION

39
Code Snippets / Random Maze Generator Function
« on: 2007-Dec-27 »
This function will produce a random maze of practically any size.

EDIT: Code removed - see bugfix below

40
GLBasic - en / Request - DELAY (millisecs)
« on: 2007-Dec-27 »
Nice simple command to pause program execution for a set length of time.

While I have already written a function to do this:

Code: (glbasic) [Select]
FUNCTION Pause: ms

LOCAL time1,time2
time1=GETTIMERALL()

WHILE TRUE
time2=GETTIMERALL()
IF time2>time1+ms THEN BREAK
WEND

ENDFUNCTION
it would be nice to have it as part of the language.

Pages: 1 2 [3]