basic snake game

Previous topic - Next topic

tokke

Just starting with glbasic. made a little snake game. all placeholders graphics
http://dl.dropbox.com/u/28111609/snake.rar

I set the speed(dificulty) by changing the fps. any other way?

EDTI: O and i can't seem to work with sprcoll. It feels like it makes a box and i collide with it, even when there is no sprite to collide with. I managed to make it work. But horrible code.

Kitty Hello

Use boxcoll or maybe use a grid for the background and snake and test whats in the grid.
Hello, btw. :)

erico

Great!

Add enemies/boss, WASD control + mouse shooter on an arena style, put some make up on gfx and you have a winner! :good:

I noticed the blocks move per/block on a grid, not a pixel movement, on higher level it looks like skipping frames, on lower level it looks ok as a style.

Better not use the FPS as it will speed up/low everything.
You can control the speed of the snake with variables, similar to how the PONG example goes when moving the ball or bats.

As for collision, Kitty´s idea of a grid, should I understood this right, seems like a great solution for me.
Like having a dim variable on 2 dimensions and use those coordinates to  check collisions.

Welcome.

tokke

#3
I just started with this and plan on expanding.

Some stuff already on paper (not code) now just need to "find" the code.

Some things are just not clear for me.

I'll just try to explain:
-The "head" of the snake is 10x10 pixels. So i move 10 pixels in all directions. So how do I control the speed wihtout limiting fps? (where can i find the pong example?).
-So instead of using sprcoll for the walls, i just make a 2 DIM array, and assign booleans like wall[0][0]=1 would be a wall on pixel 0,0 and i can test if the coords. of the snake head are within those boundries? That is what you were saying?
-You talk about mouse shooter. Any code example for mouse shooting? I know what has to be done? But can't seem to find the code for it. my problem: Player location = x,y and mouse location = x2,y2. How do I let my "bullet" travel in a straight line from x,y to x2,y2 and go past it? that is the code i can not find.

And as i said, images are just place holders :)

O and sorry for the worst english ever. Native dutch speaking

kanonet

Quote-The "head" of the snake is 10x10 pixels. So i move 10 pixels in all directions. So how do I control the speed wihtout limiting fps? (where can i find the pong example?).
Simple: dont move 10 pixels each frame, move 1-3 for slower and up to 10 for faster. Just try to play with this a bit.
Quote-So instead of using sprcoll for the walls, i just make a 2 DIM array, and assign booleans like wall[0][0]=1 would be a wall on pixel 0,0 and i can test if the coords. of the snake head are within those boundries? That is what you were saying?
Exactly.
Quote-You talk about mouse shooter. Any code example for mouse shooting? I know what has to be done? But can't seem to find the code for it. my problem: Player location = x,y and mouse location = x2,y2. How do I let my "bullet" travel in a straight line from x,y to x2,y2 and go past it? that is the code i can not find.
Wait bullet? In snake? Darn i didnt play this game for many Years.^^ But in general its like this:
Code (glbasic) Select
fire shot:
bulletx=x
bullety=y
x2=mouseposition
y2=mouseposition
each frame move bullet:
inc bulletx, sgn(x2-bulletx)
inc bullety, sgn(y2-bullety)
check if youre at your target:
if bulletx=x2 and bullety=y2 or hit_something_in_world() then BOOOOM()

This is just pseudo code of a general idea how you do things like this, but maybe it helps you. Of cause it can get improved very much, e.g. for better looking bullet fly etc..
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

tokke

Quote from: kanonet on 2012-May-08
Simple: dont move 10 pixels each frame, move 1-3 for slower and up to 10 for faster. Just try to play with this a bit.

well, there is my problem, the dimension of my sprite (placeholder) is 10x10. I had dificulty writing the code for the tail, also 10x10/part. So I found a way. But the moment I move more,or less than the dimension of the sprite, i get gaps, or overlay.


QuoteExactly.

Thank you :), should be easy actually
QuoteWait bullet? In snake? Darn i didnt play this game for many Years.^^

Not my idea  =D

Quote
But in general its like this:
Code (glbasic) Select
fire shot:
bulletx=x
bullety=y
x2=mouseposition
y2=mouseposition
each frame move bullet:
inc bulletx, sgn(x2-bulletx)
inc bullety, sgn(y2-bullety)
check if youre at your target:
if bulletx=x2 and bullety=y2 or hit_something_in_world() then BOOOOM()


I got that far, but as in your code, my bullet would travel in a diagonal line, until x or y = x2 or y2 and then a straight line up, down, left or right.


QuoteThis is just pseudo code of a general idea how you do things like this, but maybe it helps you. Of cause it can get improved very much, e.g. for better looking bullet fly etc..

apreciate the help, great community

kanonet

Quote from: tokke on 2012-May-08well, there is my problem, the dimension of my sprite (placeholder) is 10x10. I had dificulty writing the code for the tail, also 10x10/part. So I found a way. But the moment I move more,or less than the dimension of the sprite, i get gaps, or overlay.
Without knowing your code, i can just guess what you do and what maybe went wrong, but here is an idea:
How do you store the positions of the images (that are not the head of the snake), in absolute numbers? In this case overlapping and gaps will happen if you move. You should just store the position of the head in absolute coordinates and the positions of the other bodyparts relativ to the body (like: next part behind head: dx=-10 dy=0 etc.), so you just move your head 3 pixel and ad this to all your body parts when you draw them. In pseudo code:
Code (glbasic) Select
move head:
inc headx, 3 // of cause you have your controls here
inc heady, 0 // same
now draw everything:
Draw head headx,heady
draw bodypart headx+dx, heady+dy

This would be one of the possible solutions, but i dont know your code, so i dont know if this would fit to your project.

Btw maybe it would more easy to move 10 pixle each frame, but use a frame skipping technic for speed control, e.g. in easy mode just move in 1 out of 10 frames, later move 2/10, 3/10... etc. maybe it will not always look perfekt, but it may be easier to program. But in may Opinion my 1st solution is the better one, but fell free to test both.
QuoteI got that far, but as in your code, my bullet would travel in a diagonal line, until x or y = x2 or y2 and then a straight line up, down, left or right.
Yes i know, thats why i said, it can get improved very much. To solve this particular problem you just need one more line, but its part of the fun about coding to find solution by yourself, so i didnt include it, shouldnt be to had to find out how to extend the code. :)
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

tokke

Damn, you make me feel like a little boy, all excited to get on this ride.

Now I want to get to work, open my laptop and code all evening.

and to answer your question about the tail parts:

I store the position of the head in 2, 1 dimension array's, at tailx[0] and taily[0]
increase a variable "tail" to count the amounts of tail pieces.
And move the numbers through the array's:

Code (glbasic) Select


loop:
tailx[0]=headx
taily[0]=heady

i=1

WHILE i<tail
DRAWSPRITE 1,tailx[i],taily[i]
i=i+1
WEND



So yea, i can not move more or less than the dimension of the image.

But thanks to your code, I just had a mindblowing thought :D...

erico

Even if you are moving things on a 10x10 blocks of pixels, you can add 2.5 or 5 and only compute the move when it reaches 10.
You can also create a timer

Code (glbasic) Select
GLOBAL timer
timer=0

WHILE TRUE
timer=timer+1
IF timer=250
xpos=xpos+10
timer=0
END IF
WEND


It is a "dirty" way of doing things, but gets there...
...every time your program cycles 250 times, your players x position increases 10 pixels.

kanonet

Yeah erico's time is what i called a frame skip method, easy to implement, but effective.

A more proper implementation (in my opinion) is to not always move 10 pixels, but smaller values, like mentioned before. But what i forgot is, that snake is a bit special, because you want it to be locked to your 10x10 grid. So you need some more modifications for this, like erico said, just move if its time for a new 10x10 block. Basically this could like like this (this time real code^^):
Code (glbasic) Select
SETSCREEN 800,600,0
LIMITFPS 50


LOCAL xmax%=80, ymax%=60

// snake body:
LOCAL tailx%[], taily%[], tails%=9

// movement:
LOCAL speed=0.2, dirx=-speed, diry // speed must be >0 and <=1

// starting positions:
LOCAL headx=40,heady=30 // virtual head position
DIM tailx[tails+1]; DIM taily[tails+1]
FOR i%=0 TO tails
tailx[i]=headx+tails-i
taily[i]=heady
NEXT
// real head is always tail[-1]

// food position:
LOCAL foodx%=RND(xmax-1), foody%=RND(ymax-1)


REPEAT
//controls
IF KEY(208) // up
dirx=0
diry=speed
ELSEIF KEY(200) // down
dirx=0
diry=-speed
ENDIF
IF KEY(203) // left
dirx=-speed
diry=0
ELSEIF KEY(205) // right
dirx=speed
diry=0
ENDIF
// move virtual head
INC headx, dirx
INC heady, diry
IF headx<0
INC headx, xmax
ELSEIF headx>xmax
DEC headx, xmax
ENDIF
IF heady<0
INC heady, ymax
ELSEIF heady>ymax
DEC heady, ymax
ENDIF
// ok, we need real movement?
IF tailx[-1]<>INTEGER(headx) OR taily[-1]<>INTEGER(heady)
// check for collisons
IF FALSE // collisions with snake or bad object, include your own ones, im to lazy :P use INTEGER(headx) and INTEGER(heady)
// fail- > end
ELSEIF INTEGER(headx)=foodx AND INTEGER(heady)=foody // collision with good object -> bigger snake
foodx=RND(xmax-1)
foody=RND(ymax-1)
INC tails
ELSE // no collision
DIMDEL tailx[], 0
DIMDEL taily[], 0
ENDIF

DIMPUSH tailx[], headx
DIMPUSH taily[], heady
ENDIF

FOR i%=0 TO tails
DRAWRECT tailx[i]*10, taily[i]*10, 10,10, RGB(255,0,0)
PRINT i, tailx[i]*10, taily[i]*10
NEXT
DRAWRECT foodx*10, foody*10, 10,10, RGB(0,255,0)

SHOWSCREEN
UNTIL KEY(1)
END

Simple and without collision detection (besides food), but it should give you a good idea how to adjust speed etc. dont use speed values bigger than 1, or you will get gabs and other unexpected results. Of cause this is just one possible way to do it, out of  thousands.
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

tokke

I have a breakthrough.

I was playing a ludum dare game and there were objects shot at a random position. I downloaded the source code, and found the code to fire at the position of the mouse...
it works with cos,sin,tan. So going to try to write a small example (fire from center to mouse pos-game) and then try to implement it in the snake XD

snake shooter AW YEAH

The movement is almost solved (I think :) )

kanonet

Tip for shooting: if you use the Pythagorean theorem + my above shooting-"code" you can avoid using sin/cos.
Btw do you shot at a static point or a moving target? If you target is moving, will your bullet follow the target or keep flying in a straight line?
Lenovo Thinkpad T430u: Intel i5-3317U, 8GB DDR3, NVidia GeForce 620M, Micron RealSSD C400 @Win7 x64

erico

There is code to do the sin/cos shooting on this forum, I´m sure I have seen it before and possibly posted on that thread...
...posted how I avoid it as it does fry my mind a little :-[
There is one code about a top down car racing movement (like asteroids) that does a great job ´explaining´ how to pull the trick, from that to bullets should not be hard.

I usually do the add to x,y approach to this.

Keep it up and going.



tokke

Quote from: kanonet on 2012-May-14
Tip for shooting: if you use the Pythagorean theorem + my above shooting-"code" you can avoid using sin/cos.
Btw do you shot at a static point or a moving target? If you target is moving, will your bullet follow the target or keep flying in a straight line?

the sin/cos is actually pretty easy for me to program, I wrote it down on paper, explained it to a friend, made a quick code and it works pretty nice.

And about targets... don't know yet, wouldn't probably follow :p

but thanks for the help and for getting me all excited for programming again  =D