Snake not appearing
Author |
Message |
jellysausage
|
Posted: Sat May 26, 2007 4:47 pm Post subject: Snake not appearing |
|
|
hmm... so ive been working on my snake game-thing-a-ma-bobber.... and my previous program was rather.. eh.. un-optimized so ive optimized the best i could. someone with more experience than me told me to have all the algorithms and all first, then the animating last... they also recommended using View.Update.. which i figured out how to incorperate into my program. anymway, the topic is rather self explanitory--my snake is not appearing. i havent set up all the stuff about hitting yourself and you die or hitting the wall and you die... but those are easy, im still focusing on getting the snake to:
-eat and grow eat and grow
-be able to turn multiple times without the program crashing
here's my code:
%SETSCREEN
setscreen ("graphics, nocursor,")
%DECLARING VARIABLES
var bod : array 1 .. 2, 1 .. 100 of int
%first coordinate determines if variable
%is the x or y coordinate
%"1" is x coordinate, "2" is y coordinate
%second coordinate determines how many
%x and y coordinate there are; how many
%body pieces there are
var food : array 1 .. 2 of int
var key : string (1)
var bodycount, randomdirection, speed : int
var gotfood : boolean
%ALGORITHMS
process movehead
loop
if hasch then
getch (key)
%right
if key = KEY_RIGHT_ARROW then
loop
bod (1, 1) += 1
bod (2, 1) := bod (2, 1)
exit when hasch
end loop
%left
elsif key = KEY_LEFT_ARROW then
loop
bod (1, 1) -= 1
bod (2, 1) := bod (2, 1)
exit when hasch
end loop
%up
elsif key = KEY_UP_ARROW then
loop
bod (1, 1) := bod (1, 1)
bod (2, 1) += 1
exit when hasch
end loop
%down
elsif key = KEY_DOWN_ARROW then
loop
bod (1, 1) := bod (1, 1)
bod (2, 1) -= 1
exit when hasch
end loop
end if
end if
end loop
end movehead
process movebody
loop
if bodycount > 0 then
for j : 2 .. bodycount
for i : 1 .. 2
bod (i, j) := bod (i, j - 1)
delay (50)
end for
end for
end if
end loop
end movebody
process foodonfield
loop
%putting food on field, only puts food if
%snake has gotten food
if (bod (1, 1) >= food (1) - 9 and food (1) + 9 >= bod (1, 1)) or (bod (2, 1) >= food (2) - 9 and food (2) + 9 >= bod (2, 1)) then
gotfood := true
end if
if gotfood = true then
randint (food (1), 15, maxx - 15)
randint (food (2), 15, maxy - 15)
bodycount += 1
gotfood := false
end if
end loop
end foodonfield
%MAINSTREAM
%setting all beginning parameters
gotfood := false
bod (1, 1) := 300
bod (2, 1) := 200
speed := 50
bodycount := 0
food (1) := 400
food (2) := 100
%starting the snake off in a direction
randint (randomdirection, 1, 4)
if randomdirection = 1 then
%if 1, then go right
loop
bod (1, 1) += 10
bod (2, 1) := bod (2, 1)
exit when hasch
end loop
elsif randomdirection = 2 then
%if 2, then go left
loop
bod (1, 1) -= 10
bod (2, 1) := bod (2, 1)
exit when hasch
end loop
elsif randomdirection = 3 then
%if 3, then go up
loop
bod (1, 1) := bod (1, 1)
bod (2, 1) += 10
exit when hasch
end loop
else
%if not 1,2,3, then down
loop
bod (1, 1) := bod (1, 1)
bod (2, 1) -= 10
exit when hasch
end loop
end if
%Preparing all coordinates
fork movehead
fork movebody
fork foodonfield
%animating head and body
loop
drawfilloval (bod (1, 1), bod (2, 1), 10, 10, blue)
if bodycount > 0 then
for j : 2 .. bodycount
drawfilloval (bod (1, j), bod (2, j), 10, 10, blue)
end for
end if
View.Update
cls
delay (speed)
end loop
one otherthing i was wondering about was that if i wanted my snake to wrap around onto the otherside.. well.. basically.. if i wanted the snake to come out from the opposite side if it were to pass through one of the walls.. the code would be something like...
if bod(1,1)>=635 then
bod(1,1):=0
etc etc etc with the x coordinate.. my second question is... how would i get the thing to continue moving? suggestions are much appreciated. ill update as needed |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DIIST
|
Posted: Sat May 26, 2007 5:27 pm Post subject: Re: Snake not appearing |
|
|
First you should probably consider creating a flexible array of record. It will make your life a lot easier. This will also help to add and delete elements easily. Perfect for a snake game. Look into it.
Turing: |
type coord_2d :
record
x, y : int
end record
var bod : flexible array 1 .. 0 of coord_2d
new bod,15 %makes the body size to be 15 elements
|
I was tracing your execution, and i found the blue line going berserk.Please refrain from using processes, they are hard to control and could result in a crash the way your using them. Just create a giant loop and use procedures. This will make our lives easier to debug. Cleaning your code might solve your problem! |
|
|
|
|
|
Ultrahex
|
Posted: Sat May 26, 2007 5:48 pm Post subject: Re: Snake not appearing |
|
|
also using a class might help of type snake...
for example:
Turing: |
%% Point Record
type point :
record
x, y : int
end record
%% Snake Object
class snake
import point
export construct, move, draw, setdir, current, grow
%% Variables
var dir : int
var clr : int
var current : point
var tail : flexible array 1 .. 1 of point
%% Creates Snake
procedure construct (x : int, y : int, clr2 : int)
dir := 0
current.x := x;
current.y := y;
clr := clr2;
end construct
%% Moves Snake in Current Direction
procedure move ()
% dir = 0, means RIGHT
% dir = 1, means UP
% dir = 2, means LEFT
% dir = 3, means DOWN
if (dir = 0) then
current.x + = 1
elsif (dir = 1) then
current.y + = 1
elsif (dir = 2) then
current.x - = 1
elsif (dir = 3) then
current.y - = 1
end if
% Move Tail Element Down One, Overwriting End Tail Peice
for i : 2 .. upper (tail )
tail (i - 1) := tail (i )
end for
tail (upper (tail )) := current
end move
%% Makes Snake Grow
procedure grow ()
new tail, upper (tail ) + 1
for i : 1 .. upper (tail )- 1
tail (i + 1) := tail (i )
end for
end grow
%% Sets Snakes Direction
procedure setdir (n : int)
dir := n mod 4
end setdir
%% Draws Snake and its Tail
procedure draw ()
for i : 1 .. upper (tail )
drawfilloval (tail (i ).x * 10, tail (i ).y * 10, 5, 5, clr )
end for
end draw
end snake
var mainSnake : ^snake
new mainSnake
mainSnake -> construct (10, 10, brightgreen)
var chars : array char of boolean
setscreen ("offscreenonly")
loop
% DRAW BLACK BACKGROUND
drawfillbox(0, 0, 800, 600, black)
% MOVE AND DRAW SNAKE
mainSnake -> move ()
mainSnake -> draw ()
% DRAW APPLE AND CHECK IF PLAY HIT IT
drawfilloval (15 * 10, 15 * 10, 5, 5, brightred)
if (mainSnake -> current.x = 15 and mainSnake -> current.y = 15) then
mainSnake -> grow ()
end if
% PLAYER INPUT
Input.KeyDown (chars )
if (chars ('a')) then
mainSnake -> setdir (2)
elsif (chars ('d')) then
mainSnake -> setdir (0)
elsif (chars ('w')) then
mainSnake -> setdir (1)
elsif (chars ('s')) then
mainSnake -> setdir (3)
end if
% UPDATE SCREEN
delay (50)
View.Update
cls
end loop
|
|
|
|
|
|
|
jellysausage
|
Posted: Thu May 31, 2007 9:51 pm Post subject: Re: Snake not appearing |
|
|
AHA! it works!
setscreen ("graphics, nocursor,offscreenonly")
%DECLARING VARIABLES
var bodyx, bodyy : flexible array 1 .. 0 of int
var curx, cury : int := 0
var bodyspeed : int := 10
var food : array 1 .. 2 of int
var key, snakemenu : string (1)
var bodycount, randomdirection, speed, t : int
var direction : string
var death : boolean
proc right
direction := "right"
bodyx (1) += bodyspeed
bodyy (1) := bodyy (1)
end right
proc left
direction := "left"
bodyx (1) -= bodyspeed
bodyy (1) := bodyy (1)
end left
proc up
direction := "up"
bodyx (1) := bodyx (1)
bodyy (1) += bodyspeed
end up
proc down
direction := "down"
bodyx (1) := bodyx (1)
bodyy (1) -= bodyspeed
end down
proc foodonfield
%putting food on field, only puts food if
%snake has gotten food
if bodyx (1) >= food (1) - 20 and bodyx (1) <= food (1) + 20 and bodyy (1) >= food (2) - 20 and food (2) + 20 >= bodyy (1) then
randint (food (1), 50, maxx - 50)
randint (food (2), 50, maxy - 50)
new bodyx, bodycount + 1
new bodyy, bodycount + 1
bodycount += 1
bodyx (bodycount) := curx
bodyy (bodycount) := cury
end if
end foodonfield
%ALGORITHMS
proc movehead
if hasch then
getch (key)
%right
if key = KEY_RIGHT_ARROW and direction ~= "left" then
right
%left
elsif key = KEY_LEFT_ARROW and direction ~= "right" then
left
%up
elsif key = KEY_UP_ARROW and direction ~= "down" then
up
%down
elsif key = KEY_DOWN_ARROW and direction ~= "up" then
down
end if
else
if direction = "up" then
up
elsif direction = "down" then
down
elsif direction = "right" then
right
elsif direction = "left" then
left
end if
end if
end movehead
proc movebody
curx := bodyx (bodycount)
cury := bodyy (bodycount)
if bodycount > 1 then
for decreasing j : bodycount .. 2
bodyx (j) := bodyx (j - 1)
bodyy (j) := bodyy (j - 1)
end for
end if
end movebody
%player lost
proc lose
if bodyx (1) >= maxx - 10 or bodyx (1) <= 10 or bodyy (1) >= maxy - 10 or bodyy (1) <= 10 then
death := true
end if
end lose
%MAINSTREAM
%Setting all starting parameters
new bodyy, 1
new bodyx, 1
bodyx (1) := 300
bodyy (1) := 200
speed := 40
bodycount := 1
food (1) := 400
food (2) := 100
death := false
%starting the snake off in a direction
randint (randomdirection, 1, 4)
if randomdirection = 1 then
%if 1, then go right
right
elsif randomdirection = 2 then
%if 2, then go left
left
elsif randomdirection = 3 then
%if 3, then go up
up
else
%if not 1,2,3, then down
down
end if
%animating head and body
setscreen ("graphics")
loop
clock (t)
put "Body Coordinates:", bodyx (1), bodyy (1)
put "speed: ", speed
put "points: ", bodycount
put "Time: ", t
lose
movebody
movehead
foodonfield
%Drawing head
drawfilloval (bodyx (1), bodyy (1), 10, 10, blue)
%Drawing body
if bodycount > 1 then
for j : 2 .. bodycount
drawfilloval (bodyx (j), bodyy (j), 10, 10, blue)
end for
end if
%Drawing food
drawfilloval (food (1), food (2), 5, 5, red)
View.Update
delay (speed)
cls
if bodyx (1) >= maxx - 10 or bodyx (1) <= 10 or bodyy (1) >= maxy - 10 or bodyy (1) <= 10 then
put "GAME OVER!"
drawfilloval (bodyx (1), bodyy (1), 10, 10, blue)
end if
exit when death = true
end loop
thanks for help guys! |
|
|
|
|
|
|
|