Posted: Wed Jan 11, 2006 6:14 pm Post subject: Help with snake game project
Hey guys...like many others before me lol i have decided to make a snake game. I have the basic stuff down for it like controls and whatnot but whenever i go over a piece of food it doesnt recognize it. Can someone help me out?
code:
setscreen ("graphics:v16")
var foodx,foody,score,winID : int := 0
var x , y : int := 30
var font1 : int := Font.New ("serif:45")
var font2 : int := Font.New ("serif:12:bold")
var chars: array char of boolean
var tailX, tailY : flexible array 0 .. 1 of int
tailX (1) := 0
tailY (1) := 0
winID := Window.Open ("graphics")
drawfillbox(0,0,maxx,maxy,black)%Background
drawfillbox(10,10,629,345,brown)%Border Color
drawfillbox(15,15,624,340,green)%Playing Field color
Font.Draw("Snake v4.0.4c" , 150 , 353 , font1 , red)
Font.Draw("Score: " , 10 , 353 , font2 , red)
for i : 0 .. 6400
randint (foodx, 5 + 5, 618 - 5 - 5)
randint (foody, 5 + 5, 334 - 5 - 5)
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y <= 334 then
y := y + 1
end if
end if
if chars (KEY_RIGHT_ARROW) then
if x <=618 then
x := x + 1
end if
end if
if chars (KEY_LEFT_ARROW) then
if x >= 21 then
x := x - 1
end if
end if
if chars (KEY_DOWN_ARROW) then
if y >= 21 then
y := y - 1
end if
end if
drawfilloval(x, y, 5, 5,white)
delay(5)
drawfilloval(x, y, 5, 5,green)
Draw.FillOval (foodx div 10 * 10, foody div 10 * 10, 5, 5, brightred)
if x >= maxx - 20 or x <= 20 or y >= 355 - 20 or y <= 20 then
Window.Close (winID)
cls
put "Game over! Your score is ", score, ". Good Job!"
View.Update
delay (10000)
end if
if foodx = x and foody = y then
score := score + 1
new tailX, upper (tailX) + 1
new tailY, upper (tailY) + 1
exit
end if
View.Update
end loop
end for
Any help at all would be greatly appreciated!
-stinger4life
Martin says: I changed the title. Can you people please start putting more descriptive titles? Read the top sticky in this forum. If this keeps up, I'm going to start deleting posts. Thanks.
Sponsor Sponsor
[Gandalf]
Posted: Wed Jan 11, 2006 6:42 pm Post subject: (No subject)
You still have quite a bit ahead of you. I suggest once you get the basics done, read up on more of the tutorials in the Turing Walkthrough and improve your program. Try using procedures in your code, at least as an organizational tool.
As for your problem...
Change
code:
if foodx = x and foody = y then
to
code:
if Math.Distance (foodx, foody, x, y) <= 5 then
For the original requirements to be met, the foodx and x would have to be identical, which rarely happens. Using Math.Distance() you can check that a certain coordinate (x and y) passes within 5 pixels (the radius of the food) of foodx and foody. Converting your snake game to a grid-based system would help in detection as well.
stinger4life
Posted: Wed Jan 11, 2006 6:45 pm Post subject: (No subject)
yea i keep hearing about this grid system...where do i go to access that? and thanks for helping with my error
I know its messy right now but by the end of it ill organize it abit
stinger4life
Posted: Wed Jan 11, 2006 6:48 pm Post subject: (No subject)
sorry for double post but it says "Distance" is not in a export list for math. What do i do?
[Gandalf]
Posted: Wed Jan 11, 2006 7:01 pm Post subject: (No subject)
Too see an example of a grid based snake game, check out my snake game.
It says distance is not in the export list of Math since you probably don't have Turing 4.0.5. This can easily be solved by creating your own function and calling it instead of Math.Distance.
code:
function MathDistance (x1, y1, x2, y2 : real) : real
var dx : real := x1 - x2
var dy : real := y1 - y2
result sqrt (dx * dx + dy * dy)
end MathDistance
stinger4life
Posted: Wed Jan 11, 2006 7:06 pm Post subject: (No subject)
Yea i have 4.0.4c...as my game states Snake 4.0.4c! lol Do they have an update on Holts site for 4.0.5? Also, thanks for the function procedure and ill check out ur snake game
[Gandalf]
Posted: Wed Jan 11, 2006 7:12 pm Post subject: (No subject)
Heh, no problem.
HoltSoft's website does not host the Turing 4.0.5 update any longer, especially since it was a full install not only an update. They also have Turing 4.1 now. You might be able to get a newer version by asking your school, or you might find it somewhere else if you look hard enough...?
Oh, and it's not "function procedure", since a procedure is just a kind of function (one that does not return anything), not the other way around.
stinger4life
Posted: Wed Jan 11, 2006 7:16 pm Post subject: (No subject)
Okay ive checked your game...is there a tutorial on how to make my game into a grid? And when my snake "eats" theres still like a portion of "food" left.
New Code:
code:
setscreen ("graphics:v16")
var foodx,foody,score,winID,speed : int := 0
var x , y : int := 30
var font1 : int := Font.New ("serif:45")
var font2 : int := Font.New ("serif:12:bold")
var chars: array char of boolean
var tailX, tailY : flexible array 0 .. 1 of int
tailX (1) := 0%Set value for tailX and tailY variables
tailY (1) := 0
winID := Window.Open ("graphics")%Set winID variable
function MathDistance (x1, y1, x2, y2 : real) : real
var dx : real := x1 - x2
var dy : real := y1 - y2
result sqrt (dx * dx + dy * dy)
end MathDistance
put "What difficulty would you like to play? (Enter Number)"%Asks user for difficulty of game
put "**Scale** (Hardest)1 2 3 4 5 6 7 8 9 10(Easiest)"
get speed
cls %Clears screen and boots into the game
drawfillbox(0,0,maxx,maxy,black)%Background
drawfillbox(10,10,629,345,brown)%Border Color
drawfillbox(15,15,624,340,green)%Playing Field color
Font.Draw("Snake v4.0.4c" , 150 , 353 , font1 , red)
Font.Draw("Score: " , 10 , 353 , font2 , red)
for i : 0 .. 6400
randint (foodx, 5 + 5, 618 - 5 - 5)
randint (foody, 5 + 5, 334 - 5 - 5)
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y <= 334 then
y := y + 1
end if
end if
if chars (KEY_RIGHT_ARROW) then
if x <=618 then
x := x + 1
end if
end if
if chars (KEY_LEFT_ARROW) then
if x >= 21 then
x := x - 1
end if
end if
if chars (KEY_DOWN_ARROW) then
if y >= 21 then
y := y - 1
end if
end if
drawfilloval(x, y, 5, 5,white)
delay(speed)
drawfilloval(x, y, 5, 5,green)
Draw.FillOval (foodx div 10 * 10, foody div 10 * 10, 5, 5, brightred)
if x >= maxx - 20 or x <= 20 or y >= 355 - 20 or y <= 20 then
Window.Close (winID)
cls
put "Game over, but congratualations on your score of ", score, ". Better luck next time"
View.Update
delay (10000)
end if
if MathDistance (foodx, foody, x, y) <= 5 then
score := score + 1
new tailX, upper (tailX) + 1
new tailY, upper (tailY) + 1
exit
end if
View.Update
end loop
end for
If ive perhaps ordered it wrong could u maybe reorder it properly.[/code]
Sponsor Sponsor
[Gandalf]
Posted: Wed Jan 11, 2006 7:32 pm Post subject: (No subject)
OK, well, can't help you with everything...
Try understanding this for grids, although it seems a bit excessive code for a snake game.
As for your food remaining, you should be clsing the screen every time you iterate through the loop. Then you have to make sure that you are drawing the boundaries and everything inside the loop as well or else they will dissappear.
RedRogueXIII
Posted: Wed Jan 11, 2006 8:10 pm Post subject: (No subject)
once the thing is gone redraw your background and then redraw your snake.
call a View.Update then draw your background and then your snake/ food
(to get smooth animation when seting the window to graphics add a comma and add "offscreenonly" which will help smoothen the choppy animation
code:
Window.Open ("graphics, offscreenonly")
also for your original code you could have just widened the possible area using a bigger if statement.
code:
if x <= foodx + 10 and x >= foodx - 10 and y <= foody + 10 and y >= foody - 10 then
score := score + 1
new tailX, upper (tailX) + 1
new tailY, upper (tailY) + 1
exit
end if
stinger4life
Posted: Thu Jan 12, 2006 3:20 pm Post subject: (No subject)
I decided to scrap the grid idea ill just go free roam. I put the offscreen grpahics thing in but the guy still flashes. Also i was wondering if there is a way to get my score to show up beside where i put score and in the same color and stuff. Ive tried different things but you cant put a variable in fontdraw. If i cant do that then is there a way to put the score in the bar on top of the screen?
Delos
Posted: Thu Jan 12, 2006 3:30 pm Post subject: (No subject)
If you're using Font.Draw() bear in mind that the text to be displayed must be in string format. It won't recognize pure ints or real numbers. Ergo, simply use a type-mask to allow for its inclusion.
[syntax="turing"]
var num : int := 5
put "This is a number: ", num
put "This is a number as a string: " + intstr (num)
[/sytnax]
Notice the difference between the two lines? One of them had the two clauses seperated by a comma, meaning two completely seperate pieces of data; as opposed to the + which means catenation of strings (joining several strings together into one long string).
Try that instead.
stinger4life
Posted: Thu Jan 12, 2006 4:14 pm Post subject: (No subject)
k tyvm man that helped a lot i didnt even know you could convert integer to string...teacher dint teach us that...also when i put it in the score gets put on top of the previous...how do i fix this?
code:
setscreen ("graphics:v16,offscreenonly")
var foodx,foody,score,winID,speed : int := 0
var x , y : int := 30
var font1 : int := Font.New ("serif:45")
var font2 : int := Font.New ("serif:12:bold")
var chars: array char of boolean
winID := Window.Open ("graphics:v16,offscreenonly")%Set winID variable
function MathDistance (x1, y1, x2, y2 : real) : real
var dx : real := x1 - x2
var dy : real := y1 - y2
result sqrt (dx * dx + dy * dy)
end MathDistance
put "What difficulty would you like to play? (Enter Number)"%Asks user for difficulty of game
put "**Scale** (Hardest)1 2 3 4 5 6 7 8 9 10(Easiest)"
get speed
cls %Clears screen and boots into the game
drawfillbox(0,0,maxx,maxy,black)%Background
drawfillbox(10,10,629,345,brown)%Border Color
drawfillbox(15,15,624,340,green)%Playing Field color
Font.Draw("Mouse v4.0.4c" , 150 , 353 , font1 , red)
Font.Draw("Score: " , 10 , 353 , font2 , red)
for i : 0 .. 6400
randint (foodx, 25, 600 - 25)
randint (foody, 25, 330 - 25)
loop
Input.KeyDown (chars)
if chars (KEY_UP_ARROW) then
if y <= 334 then
y := y + 1
end if
end if
if chars (KEY_RIGHT_ARROW) then
if x <=618 then
x := x + 1
end if
end if
if chars (KEY_LEFT_ARROW) then
if x >= 21 then
x := x - 1
end if
end if
if chars (KEY_DOWN_ARROW) then
if y >= 21 then
y := y - 1
end if
end if
drawfilloval(x, y, 10, 10,grey)
delay(speed)
drawfilloval(x, y, 10, 10,green)
Draw.FillOval (foodx div 10 * 10, foody div 10 * 10, 5, 5, yellow)
if x >= maxx - 25 or x <= 25 or y >= 355 - 25 or y <= 25 then
Window.Close (winID)
cls
put "Game over, but congratualations on your score of ", score, ". Better luck next time"
View.Update
delay (10000)
end if
if MathDistance (foodx, foody, x, y) <= 10 then
score := score + 1
Font.Draw("Score: "+ intstr (score) , 10 , 353 , font2 , red)
exit
end if
View.Update
end loop
end for
Albrecd
Posted: Thu Jan 12, 2006 5:39 pm Post subject: (No subject)
In order to display only one number as the score, you can either use a cls somewhere (I wouldn't recomend this because then you have to redraw everything) or draw a black box before you put the score, in the same place as you put the score. This will cover the previous score and you can draw the new one on top.[/code]
stinger4life
Posted: Thu Jan 12, 2006 5:54 pm Post subject: (No subject)
Oh My God how could i not think of that lol...im so stupid...thank you very much. I was thinking it was gunna be some long new hard code that id have to try and figure out how to put in...turns out its basic drawing k ill do that. And i still cant get that offscreen only thing to work...i notice that the circle i use to redraw on top of the grey is being displays and the rest gets overlooked...any suggestions? If you need more specific ill take a screenshot or something.