snake game counter help
Author |
Message |
Ellerton
|
Posted: Wed Jan 23, 2013 5:03 pm Post subject: snake game counter help |
|
|
What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
What is the problem you are having?
<Answer Here>
Describe what you have tried to solve this problem
<Answer Here>
Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>
Turing: |
<Add your code here>
|
Please specify what version of Turing you are using
<Answer Here> |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Ellerton
|
Posted: Wed Jan 23, 2013 5:05 pm Post subject: RE:snake game counter help |
|
|
cant get snake game counter to work
% Music
process DoMusic
loop
Music.PlayFile ("Halo Theme Song Dubstep.mp3")
Music.PlayFile ("Super Mario DUBSTEP REMIX _HEAVY DROP_.mp3")
Music.PlayFile ("Mortal Dubstep (Mortal Kombat Dubstep).mp3")
Music.PlayFile ("legend of zelda dubstep.mp3")
end loop
end DoMusic
fork DoMusic
% Main Game Starts Here
% Title Screen
procedure drawSnakeTitle (var x, y : int)
var clr : int
var clr2 : int
var titleFont := Font.New ("Algerian:30:bold")
loop
randint (clr, 0, 111)
exit when clr not= 7 or not (clr >= 16 and clr <= 28)
end loop
randint (clr2, 1, 200)
drawfilloval (x, y, 5, 5, clr)
Font.Draw ("'SNAKE' ", 245, 285, titleFont, clr2)
delay (100)
end drawSnakeTitle
drawfillbox (maxx, maxy, 0, 0, black)
colorback (black)
color (white)
var titleFont := Font.New ("Algerian:30:bold")
Font.Draw ("Welcome to", 200, 350, titleFont, white)
Font.Draw ("Please choose an option", 65, 225, titleFont, white)
Font.Draw ("1. Continue", 200, 125, titleFont, white)
Font.Draw ("2. Quit", 200, 75, titleFont, white)
drawbox (500, 200, 125, 25, green) % Box
drawbox (200, 160, 444, 120, green) % Continue
drawbox (200, 105, 340, 65, green) % Quit
var clr : int
var circle : int
var clr2 : int
var x, y : int
x := 0
y := 70
% Mouse
var mousex, mousey, button : int
loop
drawSnakeTitle (x, y)
if x < 50 or y = 150 then
x := x + 10
if x >= 125 and x < 500 then
x := 505
end if
end if
if x = 50 then
y := y + 10
end if
if x = 585 then
y := y - 10
end if
if y = 70 and x > 565 then
x := x + 10
end if
if x >= maxx then
x := 0
end if
Mouse.Where (mousex, mousey, button)
if (mousex > 200 and mousex < 444 and mousey > 120 and mousey < 160 and button = 1) then
exit
elsif (mousex > 200 and mousex < 340 and mousey > 65 and mousey < 105 and button = 1) then
quit
end if
end loop
cls
% Game Background
drawfillbox (maxx, maxy, 0, 0, white)
colorback (white)
color (black)
% Snake Movement and Food
var counter : int
counter := 0
var score : int
var add : boolean := false
var losingFont := Font.New ("Algerian:30:bold")
var food1, food2 : int
var xdirec : int
var ydirec : int
xdirec := 10
ydirec := 0
var move : array char of boolean
var segments : int := 3
var snakex : array 1 .. 1000 of int
var snakey : array 1 .. 1000 of int
snakex (1) := 75
snakey (1) := 75
snakex (2) := 65
snakey (2) := 75
snakex (3) := 55
snakey (3) := 75
randint (food1, 0, maxx div 10 - 1)
food1 := food1 * 10 + 5
randint (food2, 0, maxy div 10 - 1)
food2 := food2 * 10 + 5
drawfilloval (food1, food2, 4, 4, green)
% Main Game Loop
setscreen ("offscreenonly")
loop
drawfilloval (food1, food2, 4, 4, black)
if whatdotcolour (snakex (1), snakey (1)) = black then
delay (5)
randint (food1, 0, maxx div 10 - 1)
food1 := food1 * 10 + 5
randint (food2, 0, maxy div 10 - 1)
food2 := food2 * 10 + 5
% Snake Growth
segments := segments + 1
end if
Input.KeyDown (move)
View.Update
delay (80)
if (move ('d') or move (KEY_RIGHT_ARROW)) and xdirec not= -10 then
xdirec := 10
ydirec := 0
elsif (move ('a') or move (KEY_LEFT_ARROW)) and xdirec not= 10 then
xdirec := -10
ydirec := 0
elsif (move ('w') or move (KEY_UP_ARROW)) and ydirec not= -10 then
ydirec := +10
xdirec := 0
elsif (move ('s') or move (KEY_DOWN_ARROW)) and ydirec not= 10 then
ydirec := -10
xdirec := 0
end if
% Moves body
for decreasing i : segments .. 2
snakex (i) := snakex (i - 1)
snakey (i) := snakey (i - 1)
end for
% New head position
snakex (1) := snakex (1) + xdirec
snakey (1) := snakey (1) + ydirec
% Head Collision Detection
if whatdotcolour (snakex (1), snakey (1)) = blue then
cls
colorback (black)
Font.Draw ("Game Over", 245, 285, losingFont, blue)
exit
end if
cls
for decreasing i : segments .. 1
drawfilloval (snakex (i), snakey (i), 5, 5, blue)
end for
% Colision detection
if snakex (1) >= maxx or snakex (1) <= 0 or snakey (1) >= maxy or snakey (1) <= 0 then
cls
colorback (black)
Font.Draw ("game Over", 245, 285, losingFont, white)
end if
if whatdotcolour (snakex (1), snakey (1)) = blue then
counter := counter + 10
add := false
put "score " + intstr (score)
end if
View.Update
end loop |
|
|
|
|
|
Tony
|
Posted: Wed Jan 23, 2013 5:08 pm Post subject: Re: snake game counter help |
|
|
Great, you have a dedicated thread! Now, answer the following:
Ellerton @ Wed Jan 23, 2013 5:03 pm wrote: What is it you are trying to achieve?
<Replace all the <> with your answers/code and remove the <>>
What is the problem you are having?
<Answer Here>
Describe what you have tried to solve this problem
<Answer Here>
Please specify what version of Turing you are using
<Answer Here> |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Ellerton
|
Posted: Wed Jan 23, 2013 5:17 pm Post subject: RE:snake game counter help |
|
|
how do you edit it? |
|
|
|
|
|
Tony
|
Posted: Wed Jan 23, 2013 5:25 pm Post subject: RE:snake game counter help |
|
|
editing is disabled. Just follow up on this thread. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Ellerton
|
Posted: Wed Jan 23, 2013 7:46 pm Post subject: RE:snake game counter help |
|
|
i am trying to achieve a counter on my code
counter keeps giving an error
i have tried all that i know of |
|
|
|
|
|
Tony
|
Posted: Wed Jan 23, 2013 9:01 pm Post subject: RE:snake game counter help |
|
|
what kind of an error? That is, what does the error say? Is there a specific line of code associated with that error? |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
Ellerton
|
Posted: Thu Jan 24, 2013 9:12 am Post subject: RE:snake game counter help |
|
|
It Keeps saying counter has no variable |
|
|
|
|
|
Sponsor Sponsor
|
|
|
Tony
|
Posted: Thu Jan 24, 2013 4:11 pm Post subject: RE:snake game counter help |
|
|
no variable? Are you sure it doesn't say "no value"? You must assign an initial value before a variable can be used. E.g.
code: |
var count : int
put count
|
What would the put statement print? It's undefined, so that's an error. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
|
evildaddy911
|
Posted: Thu Jan 24, 2013 5:08 pm Post subject: RE:snake game counter help |
|
|
line 90-91 of his code:
var counter : int
counter := 0
that's the only time he declares a variable called "counter", and it's a global variable so there shouldn't be an error
EDIT
just ran the code; "counter" has no problem. however, "score" does. simply do as tony just did and youll be fine |
|
|
|
|
|
Ellerton
|
Posted: Fri Jan 25, 2013 11:17 am Post subject: RE:snake game counter help |
|
|
i fixed it yester day but i need the score to go up by ten each time the snake eats the food
procedure drawSnakeTitle (var x, y : int)
var clr : int
var clr2 : int
var titleFont := Font.New ("Algerian:30:bold")
loop
randint (clr, 0, 111)
exit when clr not= 7 or not (clr >= 16 and clr <= 28)
end loop
randint (clr2, 1, 200)
drawfilloval (x, y, 5, 5, clr)
Font.Draw ("'SNAKE' ", 245, 285, titleFont, clr2)
delay (100)
end drawSnakeTitle
% Music
process DoMusic
loop
Music.PlayFile ("Super Mario DUBSTEP REMIX _HEAVY DROP_.mp3")
Music.PlayFile ("Halo Theme Song Dubstep.mp3")
Music.PlayFile ("Mortal Dubstep (Mortal Kombat Dubstep).mp3")
Music.PlayFile ("legend of zelda dubstep.mp3")
end loop
end DoMusic
fork DoMusic
% Main Game Starts Here
% Title Screen
drawfillbox (maxx, maxy, 0, 0, black)
colorback (black)
color (white)
var titleFont := Font.New ("Algerian:30:bold")
Font.Draw ("Welcome to", 200, 350, titleFont, white)
Font.Draw ("Please choose an option", 65, 225, titleFont, white)
Font.Draw ("1. Continue", 200, 125, titleFont, white)
Font.Draw ("2. Quit", 200, 75, titleFont, white)
drawbox (500, 200, 125, 25, green) % Box
drawbox (200, 160, 444, 120, green) % Continue
drawbox (200, 105, 340, 65, green) % Quit
var clr : int
var circle : int
var clr2 : int
var x, y : int
x := 0
y := 70
% Mouse
var mousex, mousey, button : int
loop
drawSnakeTitle (x, y)
if x < 50 or y = 150 then
x := x + 10
if x >= 125 and x < 500 then
x := 505
end if
end if
if x = 50 then
y := y + 10
end if
if x = 585 then
y := y - 10
end if
if y = 70 and x > 565 then
x := x + 10
end if
if x >= maxx then
x := 0
end if
Mouse.Where (mousex, mousey, button)
if (mousex > 200 and mousex < 444 and mousey > 120 and mousey < 160 and button = 1) then
exit
elsif (mousex > 200 and mousex < 340 and mousey > 65 and mousey < 105 and button = 1) then
quit
end if
end loop
cls
% Game Background
drawfillbox (maxx, maxy, 0, 0, white)
colorback (white)
color (black)
% Snake Movement and Food
var counter : int
counter := 0
var losingFont := Font.New ("Algerian:30:bold")
var food1, food2 : int
var xdirec : int
var ydirec : int
xdirec := 10
ydirec := 0
var move : array char of boolean
var segments : int := 3
var snakex : array 1 .. 1000 of int
var snakey : array 1 .. 1000 of int
snakex (1) := 75
snakey (1) := 75
snakex (2) := 65
snakey (2) := 75
snakex (3) := 55
snakey (3) := 75
randint (food1, 0, maxx div 10 - 1)
food1 := food1 * 10 + 5
randint (food2, 0, maxy div 10 - 1)
food2 := food2 * 10 + 5
% Instruction screen
var press : array char of boolean
View.Update
put " +---------------+"
put " | How To Play |"
put " +---------------+"
put ""
put ""
put " Movement: "
put ""
put " Use the W,A,S,D keys or Arrow keys to move."
put ""
put ""
put " Do not hit the walls or the black line."
put " If you do any of those then you will lose."
put " Do not Hit your own tail."
put ""
put ""
put "Press 'C' to continue"
loop
Input.KeyDown (press)
if (press ('c')) then
cls
exit
end if
end loop
% Main Game Loop
setscreen ("offscreenonly")
loop
counter := counter + 1
drawline (0, maxy - 20, maxx, maxy - 20, black)
put counter, "" ..
drawfilloval (food1, food2, 4, 4, black)
if whatdotcolour (snakex (1), snakey (1)) = black then
delay (5)
randint (food1, 0, maxx div 10 - 1)
food1 := food1 * 10 + 5
randint (food2, 0, maxy div 10 - 1 - 20)
food2 := food2 * 10 + 5
% Snake Growth
segments := segments + 1
end if
Input.KeyDown (move)
View.Update
delay (80)
if (move ('d') or move (KEY_RIGHT_ARROW)) and xdirec not= -10 then
xdirec := 10
ydirec := 0
elsif (move ('a') or move (KEY_LEFT_ARROW)) and xdirec not= 10 then
xdirec := -10
ydirec := 0
elsif (move ('w') or move (KEY_UP_ARROW)) and ydirec not= -10 then
ydirec := +10
xdirec := 0
elsif (move ('s') or move (KEY_DOWN_ARROW)) and ydirec not= 10 then
ydirec := -10
xdirec := 0
end if
% Moves body
for decreasing i : segments .. 2
snakex (i) := snakex (i - 1)
snakey (i) := snakey (i - 1)
end for
% New head position
snakex (1) := snakex (1) + xdirec
snakey (1) := snakey (1) + ydirec
% Head Collision Detection
if whatdotcolour (snakex (1), snakey (1)) = blue then
colorback (black)
cls
Font.Draw ("Game Over", 245, 285, losingFont, white)
View.Update
delay (50)
exit
end if
cls
for decreasing i : segments .. 1
drawfilloval (snakex (i), snakey (i), 5, 5, blue)
end for
% Colision detection
if snakex (1) >= maxx or snakex (1) <= 0 or snakey (1) >= maxy - 20 or snakey (1) <= 0 then
colorback (black)
cls
Font.Draw ("game Over", 245, 285, losingFont, white)
end if
end loop |
|
|
|
|
|
|
|