GLBasic forum

Main forum => GLBasic - en => Topic started by: quick2code on 2009-Sep-30

Title: Game Conversion
Post by: quick2code on 2009-Sep-30
     I'm having so many issues with converting source code from books and such. Something simple as bouncing where you multiply a variable by -1 to change direction doesn't work.

    Also, programming a path for a sprite is extremely weird in GlBasic. I tell the sprite to move to a point, stop and move to another. For some reason, the sprite jumps all the way back many pixels. I have to figure out a number to subtract from x or y to make it move smoothly.

   Kitty, could you please create a bunch of movement examples for 2D like, bouncing, point to point, sliding, stopping at a point, screen wrapping, etc. The examples did not help. I don't want anyone to do the work for me, it's just that GlBasic has its own way of doing things and it's frustrating trying to figure it all out. (English please.) Thank you.
Title: Re: Game Conversion
Post by: Quentin on 2009-Sep-30
normally a program does exactly, what you've told him to do :)
There are quite a lot of examples here in the forum from simple to complicated. It would be easier to help you if you tell us your problem more in detail, best with code examples.
Title: Re: Game Conversion
Post by: quick2code on 2009-Oct-01
I've browsed the samples and didn't find what I was looking for. Here's a cruddy point to point example. For some reason I have to negate a variable (pY,pX) so a sprite wont jump forward.  

Code (glbasic) Select


pX = 0
pY = 0
dirSwitch = 1
tinDirSwitch = 1
moveNum = moveNum + dirSwitch
backNum = backNum + tinDirSwitch

INC pX, moveNum
IF pX > 100
pX = 100
pY = -100
INC pY, backNum

ENDIF

IF pY > 200
pY = 200
pX = -200
INC pX, moveNum

ENDIF

IF pX > 300

pX = -450
INC pX, (moveNum)*1.5

ENDIF



But like I asked before, just some simple movement tests just to see how GlBasic operates.
Title: Re: Game Conversion
Post by: Hemlos on 2009-Oct-01
If you mix integers and floats you will run into problems.
I find things much smoother, if I declare variables as floats.
IE.
Velocity  = 1.0
Or in your Case:
pX = 1.0
pY = 1.0

This works to reverse a direction:

Velocity = Velocity * -1.0

You will especially need this if you start playing with circle math.
Title: Re: Game Conversion
Post by: kamakazieturtle on 2009-Oct-01
You could use an array for the spaces your sprite is going to move to if it is going to be like a scene. Just make it big enough to hold the x,y coordinates of the path and make them in whatever increment the speed would be. For example:

Code (glbasic) Select


i = 0

image = 1
LOADSPRITE "image.png",image
imagex = 0
imagey = 0

DIM pathx[6]
DIM pathy[6]

pathx[0] = 50
pathy[0] = 50
pathx[1] = 50
pathy[1] = 52
pathx[2] = 50
pathy[2] = 54
pathx[3] = 50
pathy[3] = 56
pathx[4] = 52
pathy[4] = 56
pathx[5] = 54
pathy[5] = 56

main:
BLACKSCREEN

IF i < 6
imagex = pathx[i]
imagey = pathy[i]
i = i + 1
ELSE
END
ENDIF

DRAWSPRITE image,imagex,imagey

SHOWSCREEN
GOTO main

Title: Re: Game Conversion
Post by: Hemlos on 2009-Oct-01
He is doing tests.
The main issue with his test above is mixing floats and integers.
This is why his code wont work.
All numbers must be float, or integer.

In order to multiply 1.5 speed, the original variable in question must be processed as a float.
Title: Re: Game Conversion
Post by: kamakazieturtle on 2009-Oct-01
Quote from: Hemlos on 2009-Oct-01
He is doing tests.
The main issue with his test above is mixing floats and integers.
This is why his code wont work.
All numbers must be float, or integer.

In order to multiply 1.5 speed, the original variable in question must be processed as a float.

Yeah, I was typing my post when you were posting yours, haha.
Title: Re: Game Conversion
Post by: quick2code on 2009-Oct-01
Thanks everybody for the quick replies. How bout we keep this thread going and nail down the basics? Bouncing, sliding, stop and start? Only if you have time.
Title: Re: Game Conversion
Post by: Moru on 2009-Oct-01
Here is an example of how to bounce a ball. You need to supply the ball yourself :-)
I'm not sure what you mean with the other things but you can do most things with this simple code already.
Just change the dx and dy (speed and direction) as you want it to move. If you add one to dy the sprite will move faster downwards for example. (See the gravity part)

Code (glbasic) Select
// --------------------------------- //
// Project: ForumExample
// Start: Thursday, October 01, 2009
// IDE Version: 7.115


SHOWSCREEN // If we skip this, the first key() will return 1 even though we didn't press any key. Also gets rid of the "loading..." message.

LOADSPRITE "media/ball.png", 1

LOCAL x, y // The location of the sprite
LOCAL dx, dy // The speed of the sprite in x and y direction
LOCAL sx, sy // Screen size
LOCAL spr_w, spr_h // The size of the sprite
LOCAL state=0 // This we use for telling our loop to quit

GETSCREENSIZE sx, sy // This gets us the resolution of the current screen
GETSPRITESIZE 1, spr_w, spr_h // Get the size of the sprite

//-----------------
// Bounce a sprite
//-----------------

// Init:
x = sx/2 // Middle of screen
y = sy/2
dx = 3 // Set speed to 3 pixels per screen refresh
dy = 3

WHILE state = 0
IF KEY(57) = 1 THEN state = -1 // Time to quit this loop

// Make sure we stay on screen, negate the direction if touching the edge
IF x+dx < 0 OR x+dx+spr_w > sx THEN dx=-dx
IF y+dy < 0 OR y+dy+spr_h > sy THEN dy=-dy
// Move the sprite
x = x+dx
y = y+dy
// Gravity
dy = dy+.1
// Draw the sprite
DRAWSPRITE 1, x, y
SHOWSCREEN
WEND

state = 0 // Reset the state for the next test