Basic Snake
Author |
Message |
richcash
|
Posted: Sat May 06, 2006 12:07 pm Post subject: Basic Snake |
|
|
Nothing fancy. Just a snake program.
setscreen ("graphics:640;480,offscreenonly")
var x, y, px, py : flexible array 1 .. 1 of int
var fx, fy, ex, score : int := 0
var ch : string (1) := "x"
colour (0)
colourback (7)
proc drawscreen
put score
drawfillbox (fx * 10, fy * 10, fx * 10 + 10, fy * 10 + 10, 14)
for n : 1 .. upper (x)
drawfillbox (x (n) * 10, y (n) * 10, x (n) * 10 + 10, y (n) * 10 + 10, 11)
end for
View.Update
cls
end drawscreen
randint (fx, 0, 63)
randint (fy, 0, 47)
x (1) := 1
y (1) := 1
loop
if hasch then
getch (ch)
end if
if ch = chr (200) then
y (1) += 1
elsif ch = chr (208) then
y (1) -= 1
elsif ch = chr (203) then
x (1) -= 1
elsif ch = chr (205) then
x (1) += 1
end if
if x (1) = fx and y (1) = fy then
score += 100
randint (fx, 0, 63)
randint (fy, 0, 47)
new x, upper (x) + 1
new y, upper (y) + 1
new px, upper (x) + 1
new py, upper (x) + 1
end if
if upper (x) not= 1 then
for n : 2 .. upper (x)
x (n) := px (n - 1)
y (n) := py (n - 1)
end for
end if
delay (50)
drawscreen
for n : 2 .. upper (x)
if x (1) = x (n) and y (1) = y (n) then
ex := 1
exit
end if
end for
exit when ex = 1 or x (1) > 63 or x (1) < 0 or y (1) > 47 or y (1) < 0
for n : 1 .. upper (x)
px (n) := x (n)
py (n) := y (n)
end for
end loop |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
upthescale
|
Posted: Sat May 06, 2006 1:18 pm Post subject: (No subject) |
|
|
use code tages...other than that nicely done |
|
|
|
|
 |
richcash
|
Posted: Sat May 06, 2006 1:49 pm Post subject: (No subject) |
|
|
I don't really know what code tags are. Could anyone tell me what to do when submitting a program or part of a program? |
|
|
|
|
 |
iker

|
Posted: Sat May 06, 2006 3:23 pm Post subject: (No subject) |
|
|
In the reply window, above the words you type, theres a bunch of buttons. If you press the one that says code, the words [code ][/code ]will pop up. In between the two brackets, place your source code. You can use any of these "tags" to change the words.
color size bold, anything realy.
If there is more then one thing that you would like to post, put it in a .zip or .rar folder and upload it with the Attachment Posting Control Panel located below the reply window. Anything else you need to know, might aswell just ask. |
|
|
|
|
 |
richcash
|
Posted: Sat May 06, 2006 11:05 pm Post subject: (No subject) |
|
|
Thanks, I'll use that in my next program. |
|
|
|
|
 |
NikG
|
Posted: Sun May 07, 2006 8:28 pm Post subject: (No subject) |
|
|
Wow! A very-decent snake game in only 61 lines.
I'm very impressed.
+15 bits.
edit: seems like you can go backwards when your snake has a length of 2.
also if you hold down a arrow key, and then press another key, there will be a delay before the 2nd key is registered. |
|
|
|
|
 |
richcash
|
Posted: Sun May 07, 2006 9:04 pm Post subject: (No subject) |
|
|
Thanks for all the help and compliments.
I didn't realise that there was a flaw when the snake was 2 boxes long, here's the soloution.
code: |
setscreen ("graphics:640;480,offscreenonly")
var x, y, px, py : flexible array 1 .. 1 of int
var fx, fy, ex, score : int := 0
var ch : string (1) := "x"
colour (0)
colourback (7)
proc drawscreen
put score
drawfillbox (fx * 10, fy * 10, fx * 10 + 10, fy * 10 + 10, 14)
for n : 1 .. upper (x)
drawfillbox (x (n) * 10, y (n) * 10, x (n) * 10 + 10, y (n) * 10 + 10, 11)
end for
View.Update
cls
end drawscreen
randint (fx, 0, 63)
randint (fy, 0, 47)
x (1) := 1
y (1) := 1
loop
if hasch then
getch (ch)
end if
if ch = chr (200) then
y (1) += 1
elsif ch = chr (208) then
y (1) -= 1
elsif ch = chr (203) then
x (1) -= 1
elsif ch = chr (205) then
x (1) += 1
end if
for n : 2 .. upper (x)
if x (1) = x (n) and y (1) = y (n) then
ex := 1
exit
end if
end for
if x (1) = fx and y (1) = fy then
score += 100
randint (fx, 0, 63)
randint (fy, 0, 47)
new x, upper (x) + 1
new y, upper (y) + 1
new px, upper (x) + 1
new py, upper (x) + 1
end if
if upper (x) not= 1 then
for n : 2 .. upper (x)
x (n) := px (n - 1)
y (n) := py (n - 1)
end for
end if
delay (50)
drawscreen
exit when ex = 1 or x (1) > 63 or x (1) < 0 or y (1) > 47 or y (1) < 0
for n : 1 .. upper (x)
px (n) := x (n)
py (n) := y (n)
end for
end loop
|
|
|
|
|
|
 |
[Gandalf]

|
Posted: Sun May 07, 2006 9:30 pm Post subject: (No subject) |
|
|
Very nice, I too am impressed by the nice program you have put together in so little code. Which is not to say that it is better now than if you had implemented some useful concepts like records and functions which may have made the code larger.
Two things which really stand out in the code are:
1. You should proceduralize you code farther than what you have already done with the drawscreen procedure. This will allow a smoother introduction into more advanced concepts later on, and will make your code more understandable.
2. I would recommend using Input.KeyDown() instead of getch(). We have a tutorial on this kind of input in the Turing Walkthrough where you can also learn many other things.
Note: You can edit the first post of this topic to include both code tags and your fixed code. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
richcash
|
Posted: Sun May 07, 2006 9:41 pm Post subject: (No subject) |
|
|
Yeah, I love Input.KeyDown but I used getch because I only want for one character to be input at a time (I don't want my snake to be able to go diagonally)
The reason I didn't do all of the stuff that you're supposed to do (more proc's, etc) is because I told my friend I would make my snake program in really few lines (lol). |
|
|
|
|
 |
NikG
|
Posted: Mon May 08, 2006 10:03 pm Post subject: (No subject) |
|
|
richcash wrote: Yeah, I love Input.KeyDown but I used getch because I only want for one character to be input at a time (I don't want my snake to be able to go diagonally)
The reason I didn't do all of the stuff that you're supposed to do (more proc's, etc) is because I told my friend I would make my snake program in really few lines (lol).
haha, well then you can remove the proc you do have and cut it down to 58 lines.
But anyways, Gandalf is right about proceduralization and such. If you plan to turn this into a full-fledged game (with perhaps an intro screen and customizable game options...), then you should make your code "friendlier" before proceeding. |
|
|
|
|
 |
wtd
|
Posted: Mon May 08, 2006 11:08 pm Post subject: (No subject) |
|
|
code: | if ch = chr (200) then
y (1) += 1
elsif ch = chr (208) then
y (1) -= 1
elsif ch = chr (203) then
x (1) -= 1
elsif ch = chr (205) then
x (1) += 1
end if |
This is just screaming out for a "case" statement.
Or at least a couple of uses of "or". |
|
|
|
|
 |
|
|