Snake game help

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Snake game help

Post by Malifeks »

I am trying to make a snake game for a school project. I am having trouble with a couple things but mainly i need to get my snakes tail to have a max length and slowly increase and when i have my box appear on the screen i cant get it to recognize its hitting the box so i can add points and draw another box. Any suggestions on any of my problems. : /

here is what i have so far:

Code: Select all

REM SNAKE

DIM SAND(50), BRI(50)
DIM BACK(21, 15)
CLS
SCREEN 13


MENU:
CLS
COLOR 2
PRINT "     ___                        ____ "
PRINT "    |     |\    |    /\   |  / |     "
PRINT "    |__   | \   |   /  \  | /  |     "
PRINT "       |  |  \  |  /    \ |/   |---  "
PRINT "       |  |   \ |  |----| | \  |     "
PRINT "    ----  |    \|  |    | |  \ |____ "
COLOR 1
PRINT "________________________________________"
PRINT "________________________________________"
COLOR 6
PRINT
PRINT SPACE$(18); "MENU"
COLOR 1
PRINT "________________________________________"
PRINT "________________________________________"
PRINT
COLOR 2
PRINT SPACE$(17); "PRESS:"
COLOR 15
PRINT
PRINT SPACE$(15); "1:NEW GAME"
COLOR 1
PRINT
PRINT SPACE$(11); "2:FOR INSTRUCTIONS"
COLOR 11
PRINT
PRINT SPACE$(16); "ESC:QUIT"

DO
PRESS$ = INKEY$
IF PRESS$ = "1" THEN GOTO MAIN
END IF
IF PRESS$ = "2" THEN GOTO INSTU
END IF
IF PRESS$ = CHR$(27) THEN GOTO ENDG
END IF
LOOP

INSTU:
CLS
PRINT "TEST"
PRINT "LOL"

 
MAIN:
 
CLS

X = 50
Y = 50

FOR Y = 1 TO 13
FOR X = 1 TO 15
READ CLR
PSET (X, Y), CLR
NEXT
NEXT
GET (1, 1)-(15, 13), SAND

FOR Y = 1 TO 13
FOR X = 1 TO 15
READ CLR
PSET (X, Y), CLR
NEXT
NEXT
GET (1, 1)-(15, 13), BRI

FOR Y = 1 TO 15
FOR X = 1 TO 21
READ BACK(X, Y)
NEXT
NEXT

5
FOR Y = 1 TO 15
FOR X = 1 TO 21

IF BACK(X, Y) = 1 THEN PUT (X * 15 - 15, Y * 13 - 13), SAND
IF BACK(X, Y) = 2 THEN PUT (X * 15 - 15, Y * 13 - 13), BRI

NEXT
NEXT


COLOR 15
PRINT
PRINT
PRINT
PRINT
PRINT " -----"
PRINT " SCORE"
PRINT " -----"
PRINT SCORE


SX = 150
SY = 100


 
RANDOMIZE (TIMER)
FOR I = 1 TO 1
 FROGX = 10 * (RND * 60 + 2)
 FROGY = 10 * (RND * 36 + 2)

 LINE (FROGX, FROGY)-(FROGX + 5, FROGY + 4), 5, BF
NEXT


DO
LINE (SX, SY)-(SX + 5, SY + 3), 1, BF
 
PRESS$ = INKEY$
IF PRESS$ = CHR$(0) + CHR$(72) AND Y <> 12 THEN SY = SY - 1
IF PRESS$ = CHR$(0) + CHR$(80) AND Y <> 179 THEN SY = SY + 1
IF PRESS$ = CHR$(0) + CHR$(75) AND X <> 14 THEN SX = SX - 1
IF PRESS$ = CHR$(0) + CHR$(77) AND X <> 293 THEN SX = SX + 1
IF SX = 295 THEN GOTO ENDG
IF SX = 74 THEN GOTO ENDG
IF SY = 179 THEN GOTO ENDG
IF SY = 12 THEN GOTO ENDG
IF SY = FROGY AND SX = FROGX THEN GOTO MENU
LOOP UNTIL PRESS$ = CHR$(27)



ENDG:
CLS
COLOR 2
PRINT "     ___                        ____ "
PRINT "    |     |\    |    /\   |  / |     "
PRINT "    |__   | \   |   /  \  | /  |     "
PRINT "       |  |  \  |  /    \ |/   |---  "
PRINT "       |  |   \ |  |----| | \  |     "
PRINT "    ----  |    \|  |    | |  \ |____ "
COLOR 1
PRINT "----------------------------------------"
COLOR 91
PRINT SPACE$(15); "GAME OVER"
PRINT SPACE$(10); "PRESS ESC TO CONTINUE"
PRINT "PRESS 1 TO START A NEW GAME"
COLOR 1
PRINT "----------------------------------------"
PRINT
PRINT
COLOR 4
PRINT SPACE$(9); "THANK YOU FOR PLAYING!!"
DO
PRESS$ = INKEY$
IF PRESS$ = "1" THEN GOTO MAIN
LOOP UNTIL PRESS$ = CHR$(27)
 


DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,06,91,91,91,91,06,91,91,91
DATA 91,06,91,91,91,91,91,91,91,91,06,91,91,91,91
DATA 91,91,06,91,91,91,91,91,91,91,91,91,91,06,91
DATA 91,91,91,91,91,91,91,91,06,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,06,91,91,91,91
DATA 91,91,91,06,91,91,91,91,06,91,91,91,91,06,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,06,91,91
DATA 91,91,91,91,91,91,06,91,91,91,91,91,91,91,91
DATA 91,06,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,06,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91



DATA 08,08,08,08,08,08,08,08,08,08,08,08,08,08,08
DATA 08,08,07,07,07,07,07,07,07,07,07,07,07,08,08
DATA 08,07,08,08,08,08,08,08,08,08,08,08,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,08,08,08,08,08,08,08,08,08,08,07,08
DATA 08,08,07,07,07,07,07,07,07,07,07,07,07,08,08
DATA 08,08,08,08,08,08,08,08,08,08,08,08,08,08,08


DATA 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
DATA 2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2

 
RexRanRum
Newbie
Posts: 5
Joined: Wed May 07, 2008 1:45 pm
Location: Pittsburgh, PA
Contact:

Snakin'

Post by RexRanRum »

The only way that I can think to have a maximum length would be to create an array of every location occupied by the snake. As it gets longer, the array would shift. Empty elements would be replaced by active ones, and when the maximum length was reached, the location would simply disappear. This method would require some sort of dummy coordinate to mark a segment that hasn't been extended yet.

Something like this:

Code: Select all

' Initialize
DIM segx(0 TO SNAKELEN)
DIM segy(0 TO SNAKELEN)
FOR s = 1 TO SNAKELEN
   segx(s) = -1
   segy(s) = -1
NEXT
segx(0) = INITIALX
segy(0) = INITIALY

' ...
' snake moves
FOR s = 1 to SNAKELEN
   segx(s) = segx(s - 1)
   segy(s) = segy(s - 1)
NEXT
segx(0) = MOVETOX
segy(0) = MOVETOY

' ...
' Check for collision
FOR s = 0 TO SNAKELEN
   IF segx(s) = x OR segy(s) = y THEN
      collide = -1
      EXIT FUNCTION
   END IF
NEXT
collide = 0
I think that's what you meant, anyway.
The code assumes that a collision will only be checked with non-negative coordinates, so any segment that hasn't been extended will simply be ignored. Could probably speed up that FOR loop by bailing if an unextended segment is reached, plus, y'know, not just using two arrays to store the snake coordinates if you want more than one snake playing at a time. The ideas are there, tho'.

By the way, I've never made a snake program before.
"I reject your reality and substitute my own!" ~ Adam Savage
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Malifeks:

It is obvious to me that you have not run this program! Which makes me think you might not have made it.

The program base is fairly done, the graphics seem too superior to have been made by you, without even having run the program.

As to limiting the snake's size, you can do that by using a two-dimensional array to hold the x,y values of each point, and carrying a count. Then, when its length (total of all its points) reaches the preassigned value, changing the first point of the snake (end of its tail) to the color of the background. From then on, you reassign all the points, say the last one becomes the secon-to-last one, and so on, until the first point is asssigned the nes x,y values.

As to hitting the box, well, as soon as the first point (the head of the snake) has one of is coordinate values, say x, equal the x value of either the left or the right side, you will have a hit; same for the y coordinate of the head of the snake and the y value of either the top or the bottom of the box.

After you find out how to run your code successfully, post again, with the above concepts incorporated, and we will look at it again. Good luck!
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

OK thanks for the help but really the graphics arent hard >_> i did make this program for a school project but thanks for the support.
I have not yet worked on the snake length because i have been at school. While at school i figured out how to get the program to recognize the dots and make new ones( by mistake imagine that ) lol this is what i now have ill post back after i work on the snake some.

Code: Select all

REM SNAKE

DIM SAND(50), BRI(50)
DIM BACK(21, 15)
CLS
SCREEN 13


MENU:
CLS
COLOR 2
PRINT "     ___                        ____ "
PRINT "    |     |\    |    /\   |  / |     "
PRINT "    |__   | \   |   /  \  | /  |     "
PRINT "       |  |  \  |  /    \ |/   |---  "
PRINT "       |  |   \ |  |----| | \  |     "
PRINT "    ----  |    \|  |    | |  \ |____ "
COLOR 1
PRINT "________________________________________"
PRINT "________________________________________"
COLOR 6
PRINT
PRINT SPACE$(18); "MENU"
COLOR 1
PRINT "________________________________________"
PRINT "________________________________________"
PRINT
COLOR 2
PRINT SPACE$(17); "PRESS:"
COLOR 15
PRINT
PRINT SPACE$(15); "1:NEW GAME"
COLOR 1
PRINT
PRINT SPACE$(11); "2:FOR INSTRUCTIONS"
COLOR 11
PRINT
PRINT SPACE$(16); "ESC:QUIT"

DO
PRESS$ = INKEY$
IF PRESS$ = "1" THEN GOTO MAIN

IF PRESS$ = "2" THEN GOTO INSTU

IF PRESS$ = CHR$(27) THEN GOTO ENDG

LOOP

INSTU:
CLS
PRINT "TEST"
PRINT "LOL"

 
MAIN:
 
CLS

X = 50
Y = 50

FOR Y = 1 TO 13
FOR X = 1 TO 15
READ CLR
PSET (X, Y), CLR
NEXT
NEXT
GET (1, 1)-(15, 13), SAND

FOR Y = 1 TO 13
FOR X = 1 TO 15
READ CLR
PSET (X, Y), CLR
NEXT
NEXT
GET (1, 1)-(15, 13), BRI

FOR Y = 1 TO 15
FOR X = 1 TO 21
READ BACK(X, Y)
NEXT
NEXT

5
FOR Y = 1 TO 15
FOR X = 1 TO 21

IF BACK(X, Y) = 1 THEN PUT (X * 15 - 15, Y * 13 - 13), SAND
IF BACK(X, Y) = 2 THEN PUT (X * 15 - 15, Y * 13 - 13), BRI

NEXT
NEXT


COLOR 15
PRINT
PRINT
PRINT
PRINT
PRINT " -----"
PRINT " SCORE"
PRINT " -----"
PRINT SCORE



SX = 150
SY = 100

FROG:
RANDOMIZE (TIMER)
FOR I = 1 TO 1
 
 FROGX = INT(RND * SX)
 FROGY = INT(RND * SY)
 IF FROGX < 75 THEN GOTO FROG
 IF FROGY <12> 295 THEN GOTO FROG
 IF FROGY > 179 THEN GOTO FROG
 LINE (FROGX, FROGY)-(FROGX + 5, FROGY + 4), 5, BF
NEXT


DO
LINE (SX, SY)-(SX + 5, SY + 3), 1, BF
 
PRESS$ = INKEY$
IF PRESS$ = CHR$(0) + CHR$(72) AND Y <> 12 THEN SY = SY - 1
IF PRESS$ = CHR$(0) + CHR$(80) AND Y <> 179 THEN SY = SY + 1
IF PRESS$ = CHR$(0) + CHR$(75) AND X <> 14 THEN SX = SX - 1
IF PRESS$ = CHR$(0) + CHR$(77) AND X <> 293 THEN SX = SX + 1
IF SX = 295 THEN GOTO ENDG
IF SX = 74 THEN GOTO ENDG
IF SY = 179 THEN GOTO ENDG
IF SY = 12 THEN GOTO ENDG
IF SY = FROGY AND SX = FROGX THEN GOTO FROG
LOOP UNTIL PRESS$ = CHR$(27)



ENDG:
CLS
COLOR 2
PRINT "     ___                        ____ "
PRINT "    |     |\    |    /\   |  / |     "
PRINT "    |__   | \   |   /  \  | /  |     "
PRINT "       |  |  \  |  /    \ |/   |---  "
PRINT "       |  |   \ |  |----| | \  |     "
PRINT "    ----  |    \|  |    | |  \ |____ "
COLOR 1
PRINT "----------------------------------------"
COLOR 91
PRINT SPACE$(15); "GAME OVER"
PRINT SPACE$(10); "PRESS ESC TO CONTINUE"
PRINT SPACE$(8); "PRESS 1 TO START A NEW GAME"
COLOR 1
PRINT "----------------------------------------"
PRINT
PRINT
COLOR 4
PRINT SPACE$(9); "THANK YOU FOR PLAYING!!"
DO
PRESS$ = INKEY$
IF PRESS$ = "1" THEN GOTO MAIN
LOOP UNTIL PRESS$ = CHR$(27)
 


DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,06,91,91,91,91,06,91,91,91
DATA 91,06,91,91,91,91,91,91,91,91,06,91,91,91,91
DATA 91,91,06,91,91,91,91,91,91,91,91,91,91,06,91
DATA 91,91,91,91,91,91,91,91,06,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,06,91,91,91,91
DATA 91,91,91,06,91,91,91,91,06,91,91,91,91,06,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,06,91,91
DATA 91,91,91,91,91,91,06,91,91,91,91,91,91,91,91
DATA 91,06,91,91,91,91,91,91,91,91,91,91,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,06,91,91,91
DATA 91,91,91,91,91,91,91,91,91,91,91,91,91,91,91



DATA 08,08,08,08,08,08,08,08,08,08,08,08,08,08,08
DATA 08,08,07,07,07,07,07,07,07,07,07,07,07,08,08
DATA 08,07,08,08,08,08,08,08,08,08,08,08,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,07,07,07,07,07,07,07,07,07,08,07,08
DATA 08,07,08,08,08,08,08,08,08,08,08,08,08,07,08
DATA 08,08,07,07,07,07,07,07,07,07,07,07,07,08,08
DATA 08,08,08,08,08,08,08,08,08,08,08,08,08,08,08


DATA 0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
DATA 2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2
DATA 0,0,0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2

oh and also considering ive been working on this for about a month and a half its not really that much done.
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

with what i have i have no idea how to get the last segments to erase etc ill have to just forget that and try to figure out other things and ill just have a lame game where the tail doesnt erase >.<
RexRanRum
Newbie
Posts: 5
Joined: Wed May 07, 2008 1:45 pm
Location: Pittsburgh, PA
Contact:

Tail End

Post by RexRanRum »

To erase the tail, simply redraw the background over it before you pop off the end/replace the furthest segment with the new head position.

It really depends on how you are going to code the snake, but no matter what, you'll always store the snake segments in an array (or arrays), and erasing will always involve drawing the background over it right before you replace the tail segment's values.
"I reject your reality and substitute my own!" ~ Adam Savage
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

I cannot figure any of this out ive been trying forever now i cant get my snake to have a size limit i need very basic instructions if someone has them i'm a pretty big noob at this
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Malifeks, here is a sample program for you. I am not using the code option, which you reported changed your code:

'===============================================
'SnakeRAE serves as a guide for making snake-like games.
SCREEN 13: w = 320: h = 240

CLS
LOCATE 8, 13: PRINT "S N A K E"

'colors used
bg = 1 'blue background
head = 4 + 8 'bright red head
body = 2 'green body


'default length
L = h / 5

'maximum length
Lmax = h - 8 * 5

LOCATE 20, 2: PRINT "ENTER LENGTH OF SNAKE (default"; L; ")";
INPUT L$

'Length of snake
IF VAL(L$) > 0 THEN L = VAL(L$)
IF L > Lmax THEN L = Lmax

DIM sx(1 TO L) AS INTEGER, sy(1 TO L) AS INTEGER

'default snake is a straight line starting at x = 7, on row 10
FOR i = 1 TO L
sx(L + 1 - i) = 6 + i: sy(L + 1 - i) = 10
NEXT i

'paint background
LINE (0, 0)-(w, Lmax), bg, BF

LOCATE 23, 2: PRINT "Arrow keys move snake, Esc key ends"

DO
'draw snake body
FOR i = 1 TO L
PSET (sx(i), sy(i)), body
NEXT i

'draw snake head
PSET (sx(1) - 1, sy(1) - 1), head
PSET (sx(1) + 0, sy(1) - 1), head
PSET (sx(1) + 1, sy(1) - 1), head

PSET (sx(1) - 1, sy(1) + 0), head
PSET (sx(1) + 0, sy(1) + 0), head
PSET (sx(1) + 1, sy(1) + 0), head

PSET (sx(1) - 1, sy(1) + 1), head
PSET (sx(1) + 0, sy(1) + 1), head
PSET (sx(1) + 1, sy(1) + 1), head

k$ = "": WHILE k$ = "": k$ = RIGHT$(INKEY$, 1): WEND

'erase snake body
FOR i = 1 TO L
PSET (sx(i), sy(i)), bg
NEXT i

'erase snake head
PSET (sx(1) - 1, sy(1) - 1), bg
PSET (sx(1) + 0, sy(1) - 1), bg
PSET (sx(1) + 1, sy(1) - 1), bg

PSET (sx(1) - 1, sy(1) + 0), bg
PSET (sx(1) + 0, sy(1) + 0), bg
PSET (sx(1) + 1, sy(1) + 0), bg

PSET (sx(1) - 1, sy(1) + 1), bg
PSET (sx(1) + 0, sy(1) + 1), bg
PSET (sx(1) + 1, sy(1) + 1), bg



'identify arrow key pressed and set d
IF k$ = "H" THEN d = -1 'pressed up arrow key
IF k$ = "P" THEN d = 1 'pressed down arrow key
IF k$ = "K" THEN d = -1 'pressed left arrow key
IF k$ = "M" THEN d = 1 'pressed right arrow key

'test arrow key pressed and draw snake body in new location
IF k$ = "H" OR k$ = "P" THEN 'pressed up or down arrow key
IF sy(1) + d <> sy(2) THEN
FOR i = L TO 2 STEP -1
sx(i) = sx(i - 1): sy(i) = sy(i - 1)
NEXT i
sy(1) = sy(1) + d
END IF
END IF
IF k$ = "K" OR k$ = "M" THEN 'pressed left or right arrow key
IF sx(1) + d <> sx(2) THEN
FOR i = L TO 2 STEP -1
sx(i) = sx(i - 1): sy(i) = sy(i - 1)
NEXT i
END IF
sx(1) = sx(1) + d
END IF

'draw snake head in new location
shx(1) = sx(1) + d: shy(1) = sy(1) + d
shx(2) = sx(1) + d: shy(2) = sy(1) + d
LOOP UNTIL k$ = CHR$(27)

END
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

I guess Malifeks' snake is hibernating! LOL!
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

Lol sorry.... had some other projects due just got back to the game atm.... that sample is awesome i think im startin to get it a little more now :D ill post back whenever i get farther.
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

i incorporated that program above into my program but what i need now is how to get constant motion from the snake like how would i make it so it doesnt stop moving that direction after i hit that key. all help is appreciated
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Well, Malifecs, coding is such a vast field, that one can only concentrate on one thing at a time. At this point, I feel that I supplied you with some code that you should study and understand. Then, you can do a few, experimental changes, with some forethought about what you expect to happen, in order to learn. When you have done that, I invite you to post your new code then, if you run into problems, and I, or someone else here, will be more than happy to help you correct your code. But, I don't feel it to be right to just ask "for the next batch of code", so, I bow out from your resquest, and expect you to do as I said here, study, learn, expriment, and then ask questions on your specific code problems.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

ok thats fine i have been messing around with it i was just wodnering if u can give me some hints to how i can do it like not give me a code just the idea of it i guess....
Ralph
Veteran
Posts: 148
Joined: Fri Feb 09, 2007 3:10 pm
Location: Katy, Texas

Post by Ralph »

Let's say you have a 4-pixel-long snake, and that is located at coordinates x,y, as follows: (4,10), (4,11), (5,11), and (5,12). To move it so the last pixel, (5,11), which I will call the head, moves down one pixel, with the rest following, just review my SnakeRAE code, as it is all there. Now, to move it continuously in the direction of the last arrow-key entry, since my code has already captured the arrow-key reference to the last entry, say down, you just have to create a loop, where the head coordinates are replaced, successively, by (5,12), (5,13), (5,14), etc., with the other coordinates suitable replaced, until you press a new arrow key. You could do this by changing my line:

k$="":WHILE k$ = "":k$ = INKEY$:WEND

to, simply,

k$ = INKEY$

Hope you can figure out the rest. Remember, if you want to learn, you read the code I posted until you understand each line completely, before moving on. Then, you use what you have learned, and experiment with other possibilities you think of. If you get truly stuck at some point, you post your piece of code with the specific problem, explain what you are trying to do, post the error message or wrong results you are geting )explain the problem), and ask for some guidance.

Now, if you only want the complete code for a game or other endevour, you should advertise that you are willing to pay, and, I'm sure, you will get more than one interested programmer. In this case, you are not willing to learn how to program, you just want to buy a game that you have in mind.

So, we expect amateurs who post here to have a strong desire to learn how to program in QBasic, and to have made some code towards their proyect. Otherwise, we do not really want to help.
Ralph, with QuickBASIC 4.5, operating under Windows XP, wiht anHP LaserJet 4L Printer. Bilingual in English/Spanish
Malifeks
Coder
Posts: 12
Joined: Mon Apr 28, 2008 9:03 pm

Post by Malifeks »

Ok thanks a lot sorry if it seems like im mooching off your knowledge but your program you made seriously helped me a ton all i need to do is get that constant motion but with your explanation i dont think it should be too hard.... i usually just work on this at school so if i come to any problems ill post back after school, plus i can ask my peers as well. But seriously thanks for the advice its really useful.
Post Reply