Flexible Arrays for snake/an "evil" process
Author |
Message |
Mr. T
|
Posted: Sat May 28, 2005 12:57 am Post subject: Flexible Arrays for snake/an "evil" process |
|
|
1. i read over the tutorials on flexible arrays, but i am still bewildered to how/where i can incorporate them to allow my snake to grow
2. i have a process which seems to work fine. if u think its worth eliminating, be my guest
----------
code: |
%%%%%%%%%%%%%%%%%%%%%%%%
%variable declaration
var scorewin, gamewin : int
scorewin := Window.Open ("nocursor,graphics:455;90,title:Uncle Worm Score: 0")
colourback (black)
cls
colour (brightgreen)
gamewin := Window.Open ("nocursor,position:bottom; right,graphics:500;400")
%%%%%%%%%%%%%%%%%%%%%%%%
var food1, food2 : int
var foodSize : int
var makeFood : boolean := true
var box1, box4 : int := 0
var col : int := 4
var countdown : int := 5
/*var snakex : int := 200*/
/*var snakey : int := 150*/
var snakesize : int := 5
var move, move2, move3, move4 := false
var key : array char of boolean
var ring : int := 0
var answer : string
move3 := true %starting direction of snake
var newMaxX : int := 500 %innitial screen dimensions (x co-ords)
var newMaxY : int := 400 %innitial screen dimensions (y co-ords)
var levelUpX : int := 50 %decrease in screen dimensions (x co-ords) upon completion of level
var levelUpY : int := 40 %decrease in screen dimensions (y co-ords) upon completion of level
var scoreLevel : int := 50
var foodBoolean : boolean
var score : int := 0 %score
var bonus : int := 60 %1 min to get bonus (bonus = 60000 milliseconds = 60 seconds =1 min)
var fontScore, fontExit : int
var col1, col2, col3 : int := brightred
fontScore := Font.New ("serif:40")
fontExit := Font.New ("arial:14")
var randomMotivation : int
var funnyMotivation : array 1 .. 5 of string
%%%%%%%%%%%%%%%%%%%%%%%%
%Preliminary Setup
funnyMotivation (1) := "HAX00R!!!"
funnyMotivation (2) := "OH SNAP!!!"
funnyMotivation (3) := "GHETTO!!!"
funnyMotivation (4) := "WERD UP!!!"
funnyMotivation (5) := "PWNAGE!!!"
type snakeDat :
record
x : int
y : int
end record
var snake : flexible array 1 .. 2 of snakeDat
snake (1).x := maxx div 2
snake (1).y := maxy div 2
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%
% START %
%%%%%%%%%
colour (0)
colourback (7)
cls
%gives time for user to get ready
loop
locate (12, 25) %locates the count down test
put "Starting in...", countdown %starts the countdown
delay (300) %delay between each consecutive decreasing number
countdown := countdown - 1 %decreasing increments
cls %erases the countdown text when countdown reaches 0
exit when countdown = 0 %game begins when countdown hits 0
end loop
%%%%%%%%%%%%%%%%%%%%%%%%
process windowUpdate
Window.Select (scorewin)
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score)) %if food eaten, score added to total at the top of the screen
randint (randomMotivation, 1, 5) %random motivation generator
Font.Draw (funnyMotivation (randomMotivation), (maxx div 2) - 125, (maxy div 2), fontScore, yellow) %random motivation ouput
end windowUpdate
%%%%%%%%%%%%%%%%%%%%%%%%
procedure food
if makeFood then %makeFood:=true
randint (food1, 1, newMaxX - 10) %random food location (but not in the walls)
randint (food2, 1, newMaxY - 10) %random food location (but not in the walls)
randint (foodSize, 2, 10) %random food size
drawfilloval (food1, food2, foodSize, foodSize, 2) %draws the food
end if
end food
%%%%%%%%%%%%%%%%%%%%%%%%
%point of contact upon death
procedure ringEnd
loop
Window.Select (scorewin)
cls
Font.Draw ("GAME OVER!!!", (maxx div 2) - 175, (maxy div 2), fontScore, ring)
Window.Select (gamewin)
drawoval (snake (1).x, snake (1).y, snakesize + ring, snakesize + ring, ring) %ring expands around the ball at the point of collision
ring += 2 %increments of the rings
delay (10) %delay between each ring increment
exit when ring = 100 %game ends when ring incremeent hits 100
end loop
end ringEnd
%%%%%%%%%%%%%%%%%%%%%%%%
%draws food
food
%%%%%%%%%%%%%%%%%%%%%%%%
%snake movement
loop
drawbox (box1, newMaxY - 1, newMaxX - 1, box4, 14)
drawfilloval (snake (1).x, snake (1).y, snakesize, snakesize, 7)
/*draw_snake*/
if move then
snake (1).y += 1
elsif move2 then
snake (1).y -= 1
elsif move3 then
snake (1).x += 1
elsif move4 then
snake (1).x -= 1
end if
Input.KeyDown (key)
if key (KEY_UP_ARROW) and move2 = false then %Two Conditions: 1. up arrow is pressed 2. move2 = false
move2 := false
move3 := false
move4 := false
move := true
elsif key (KEY_DOWN_ARROW) and move = false then %Two Conditions: 1. down arrow is pressed 2. move = false
move := false
move3 := false
move4 := false
move2 := true
elsif key (KEY_RIGHT_ARROW) and move4 = false then %Two Conditions: 1. right arrow is pressed 2. move4 = false
move := false
move2 := false
move4 := false
move3 := true
elsif key (KEY_LEFT_ARROW) and move3 = false then %Two Conditions: 1. left arrow is pressed 2. move3 = false
move := false
move2 := false
move3 := false
move4 := true
end if
drawfilloval (snake (1).x, snake (1).y, snakesize, snakesize, 12)
/*col:=7
draw_snake */
%deletes the tail
delay (10)
%%%%%%%%%%%%%%%%%%%%%%%%
%wall collision detection
if snake (1).x = 4 or snake (1).x = maxx - 4 or snake (1).y = 4 or snake (1).y = maxy - 4 then %collision detection to detect whether the ball has hit the walls
Window.Select (scorewin)
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score) + " You Lose!") %if balls hits the wall, you lose!
Window.Select (gamewin)
ringEnd %fancy death
exit %game ends
end if
%%%%%%%%%%%%%%%%%%%%%%%%
%food collision detection
if score < scoreLevel then %additional check to make sure the level isnt completed
if whatdotcolour (food1 + foodSize, food2 + foodSize) = 12 or whatdotcolour (food1 - foodSize, food2 - foodSize) = 12
then
score += 10 %when food collision detected points are added to score
new snake, upper (snake) + 1
fork windowUpdate %fork needed so that the delay within the process will only affect one screen
Window.Select (scorewin)
cls %scorewin clears refreshes the score every time a new apple is eaten
Window.Select (gamewin)
drawfilloval (food1, food2, foodSize, foodSize, 7) %so that no chunk of food is left behind
if score < scoreLevel then %if level isnt completed
food %continue to make food if its still the same level
end if
end if
end if
%%%%%%%%%%%%%%%%%%%%%%%%
%NEXT LEVEL
if score >= scoreLevel %ready to enter next level
then
drawfillbox ((maxx div 2) - 27, maxy, (maxx div 2) + 20, maxy - 25, 0)
Font.Draw ("EXIT", (maxx div 2) - 25, maxy - 20, fontExit, col1) %level's exit point
col1 += 1
if col1 = 40
then
col1 := 0
end if
makeFood := false %once exit appears, no more food is drawn
end if
if whatdotcolour (snake (1).x + snakesize, snake (1).y + snakesize) = 0 %if the snake reaches the exit (detects the WHITE(whatdotcolour) exit point)
then
cls %clears the screen for the scoring updates
%%%%%%%%%%%%%%%%%%%%%%%%
%scoring system (after each completed level)
Window.Select (scorewin)
put "Your score is: ", score %score with out additional time bonus
var timeBonus : int := bonus - Time.Elapsed div 1000 %1 min timer (Time.Elapsed = 1000 milliseconds = 1 min)
put "Your time bonus is: ", timeBonus %time bonus
score := score + timeBonus %updated score = score as of to that point + time bonus
put "Your overall score is: ", score %score as of to that point + time bonus
bonus := 60 + (Time.Elapsed div 1000) %bonus reset
put "[press enter to continue to next round]" %gives the user time to read the score
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score))
Window.Select (gamewin)
Input.Pause %waits for a button to be pressed before entering the next level
Window.Select (scorewin)
cls %clears the scorewin once the gamewin restarts
Window.Select (gamewin)
/*getch (ch)
if ch =(KEY_ENTER)
then*/
%%%%%%%%%%%%%%%%%%%%%%%%
%variable reset
newMaxX := newMaxX - levelUpX %updated screen dimensions = screen dimensions - 50
newMaxY := newMaxY - levelUpY %updated screen dimensions = screen dimensions - 40
View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY)) %displaying new screen for gamewin
snake (1).x := newMaxX div 2 %new x co-ords for the start of a new level
snake (1).y := newMaxY div 2 %new y co-ords for the start of a new level
colour (0) %reset the colour of any writing
colourback (7) %reset the colour of the background
cls %paints the background specified colour
makeFood := true %makign it possible to draw food
food %start redrawing food
scoreLevel := score + 50 %next level will be completed after an additional 50 points
end if
end loop
View.Update
%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Sat May 28, 2005 7:27 am Post subject: (No subject) |
|
|
I don't understand why you think you need that process. I would eliminate it, if I were you.
code: |
if you eat an apple then
incriment the upper bounds of your flexible array by 1
you don't need to set the x and y coordinates of the new segment
end if
%update positions of snake
for decreasing n : upper (snake) .. 2
snake (n) := snake (n - 1)
%where snake is an array of a record that contains x and y coordinates
end for
|
If you have the update segments code between the drawing and the eating the apples, then you don't have to set the x and y coords. This is how you should do it. The reason is, you eat an apple, then go into the decreasing for loop and the upper (snake) is set to the second greatest element of the flexible array.
Hope that helps. |
|
|
|
|
|
Mr. T
|
Posted: Sat May 28, 2005 2:34 pm Post subject: Alex's Opinion |
|
|
I'm still not getting how and where i can fit it in ack! |
|
|
|
|
|
Token
|
Posted: Sat May 28, 2005 2:52 pm Post subject: (No subject) |
|
|
well what your doing with the array is storing the location that each block of the snake is in, so0o what you do is every time you eat an apple you make the array able to hold one more cordinate. then what you'd would be somthing like this
code: |
for decreasing i : lengthOfSnake .. 2
snakex (i) := snakex (i - 1)
end for
snake (1) := %%%whatever the new one is, i didnt take the time to look at your code.
|
hope this helps |
|
|
|
|
|
Cervantes
|
Posted: Sat May 28, 2005 3:14 pm Post subject: (No subject) |
|
|
Token wrote: code: | snake (1) := %%%whatever the new one is, i didnt take the time to look at your code. |
Assuming snake (1) is the head of the snake (which it makes most sense to be), why would you adjust it's position to the new position. It's like you're sticking the snake's head in its ass! |
|
|
|
|
|
Mr. T
|
Posted: Sat May 28, 2005 4:03 pm Post subject: Alex's Opinion |
|
|
ok, i added in the flexible array stuff...but its screwy, cuz the game is running, but u cant see the snake.
code: |
%%%%%%%%%%%%%%%%%%%%%%%%
%variable declaration
var scorewin, gamewin : int
scorewin := Window.Open ("nocursor,graphics:455;90,title:Uncle Worm Score: 0")
colourback (black)
cls
colour (brightgreen)
gamewin := Window.Open ("nocursor,position:bottom; right,graphics:500;400")
%%%%%%%%%%%%%%%%%%%%%%%%
var food1, food2 : int
var foodSize : int
var makeFood : boolean := true
var box1, box4 : int := 0
var col : int := 4
var countdown : int := 5
/*var snakex : int := 200*/
/*var snakey : int := 150*/
var snakesize : int := 5
var move, move2, move3, move4 := false
var key : array char of boolean
var ring : int := 0
var answer : string
move3 := true %starting direction of snake
var newMaxX : int := 500 %innitial screen dimensions (x co-ords)
var newMaxY : int := 400 %innitial screen dimensions (y co-ords)
var levelUpX : int := 50 %decrease in screen dimensions (x co-ords) upon completion of level
var levelUpY : int := 40 %decrease in screen dimensions (y co-ords) upon completion of level
var scoreLevel : int := 50
var foodBoolean : boolean
var score : int := 0 %score
var bonus : int := 60 %1 min to get bonus (bonus = 60000 milliseconds = 60 seconds =1 min)
var fontScore, fontExit : int
var col1, col2, col3 : int := brightred
fontScore := Font.New ("serif:40")
fontExit := Font.New ("arial:14")
var randomMotivation : int
var funnyMotivation : array 1 .. 5 of string
%%%%%%%%%%%%%%%%%%%%%%%%
%Preliminary Setup
funnyMotivation (1) := "HAX00R!!!"
funnyMotivation (2) := "OH SNAP!!!"
funnyMotivation (3) := "GHETTO!!!"
funnyMotivation (4) := "WERD UP!!!"
funnyMotivation (5) := "PWNAGE!!!"
type snakeDat :
record
x : int
y : int
end record
var snake : flexible array 1 .. 2 of snakeDat
snake (1).x := maxx div 2
snake (1).y := maxy div 2
snake (2).x :=(maxx div 2)-10
snake (2).y :=maxy div 2
new snake, 2 %set snake's length to 2
%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%
% START %
%%%%%%%%%
colour (0)
colourback (7)
cls
%gives time for user to get ready
loop
locate (12, 25) %locates the count down test
put "Starting in...", countdown %starts the countdown
delay (300) %delay between each consecutive decreasing number
countdown := countdown - 1 %decreasing increments
cls %erases the countdown text when countdown reaches 0
exit when countdown = 0 %game begins when countdown hits 0
end loop
%%%%%%%%%%%%%%%%%%%%%%%%
process windowUpdate
Window.Select (scorewin)
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score)) %if food eaten, score added to total at the top of the screen
randint (randomMotivation, 1, 5) %random motivation generator
Font.Draw (funnyMotivation (randomMotivation), (maxx div 2) - 125, (maxy div 2), fontScore, yellow) %random motivation ouput
end windowUpdate
%%%%%%%%%%%%%%%%%%%%%%%%
procedure food
if makeFood then %makeFood:=true
randint (food1, 1, newMaxX - 10) %random food location (but not in the walls)
randint (food2, 1, newMaxY - 10) %random food location (but not in the walls)
randint (foodSize, 2, 10) %random food size
drawfilloval (food1, food2, foodSize, foodSize, 2) %draws the food
end if
end food
%%%%%%%%%%%%%%%%%%%%%%%%
%point of contact upon death
procedure ringEnd
loop
Window.Select (scorewin)
cls
Font.Draw ("GAME OVER!!!", (maxx div 2) - 175, (maxy div 2), fontScore, ring)
Window.Select (gamewin)
drawoval (snake (1).x, snake (1).y, snakesize + ring, snakesize + ring, ring) %ring expands around the ball at the point of collision
ring += 2 %increments of the rings
delay (10) %delay between each ring increment
exit when ring = 100 %game ends when ring incremeent hits 100
end loop
end ringEnd
%%%%%%%%%%%%%%%%%%%%%%%%
%draws food
food
for i : 1 .. upper (snake) %redraw snake
Draw.FillOval (snake (i).x, snake (i).y, 5, 5, 12)
end for
%%%%%%%%%%%%%%%%%%%%%%%%
%snake movement
loop
drawbox (box1, newMaxY - 1, newMaxX - 1, box4, 14)
/*draw_snake*/
if move then
snake (1).y += 1
elsif move2 then
snake (1).y -= 1
elsif move3 then
snake (1).x += 1
elsif move4 then
snake (1).x -= 1
end if
Input.KeyDown (key)
if key (KEY_UP_ARROW) and move2 = false then %Two Conditions: 1. up arrow is pressed 2. move2 = false
move2 := false
move3 := false
move4 := false
move := true
elsif key (KEY_DOWN_ARROW) and move = false then %Two Conditions: 1. down arrow is pressed 2. move = false
move := false
move3 := false
move4 := false
move2 := true
elsif key (KEY_RIGHT_ARROW) and move4 = false then %Two Conditions: 1. right arrow is pressed 2. move4 = false
move := false
move2 := false
move4 := false
move3 := true
elsif key (KEY_LEFT_ARROW) and move3 = false then %Two Conditions: 1. left arrow is pressed 2. move3 = false
move := false
move2 := false
move3 := false
move4 := true
end if
for decreasing i : upper (snake) .. 2 %set each of the snake's sections to equal the one before it
snake (i) := snake (i - 1)
end for
delay (10)
%%%%%%%%%%%%%%%%%%%%%%%%
%wall collision detection
if snake (1).x = 4 or snake (1).x = maxx - 4 or snake (1).y = 4 or snake (1).y = maxy - 4 then %collision detection to detect whether the ball has hit the walls
Window.Select (scorewin)
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score) + " You Lose!") %if balls hits the wall, you lose!
Window.Select (gamewin)
ringEnd %fancy death
exit %game ends
end if
%%%%%%%%%%%%%%%%%%%%%%%%
%food collision detection
if score < scoreLevel then %additional check to make sure the level isnt completed
if whatdotcolour (food1 + foodSize, food2 + foodSize) = 12 or whatdotcolour (food1 - foodSize, food2 - foodSize) = 12
then
score += 10 %when food collision detected points are added to score
new snake, upper (snake) + 1 %add a new element to it
snake (upper (snake)) := snake (upper (snake) - 1) %set new element's coordinates
fork windowUpdate %fork needed so that the delay within the process will only affect one screen
Window.Select (scorewin)
cls %scorewin clears refreshes the score every time a new apple is eaten
Window.Select (gamewin)
drawfilloval (food1, food2, foodSize, foodSize, 7) %so that no chunk of food is left behind
if score < scoreLevel then %if level isnt completed
food %continue to make food if its still the same level
end if
end if
end if
%%%%%%%%%%%%%%%%%%%%%%%%
%NEXT LEVEL
if score >= scoreLevel %ready to enter next level
then
drawfillbox ((maxx div 2) - 27, maxy, (maxx div 2) + 20, maxy - 25, 0)
Font.Draw ("EXIT", (maxx div 2) - 25, maxy - 20, fontExit, col1) %level's exit point
col1 += 1
if col1 = 40
then
col1 := 0
end if
makeFood := false %once exit appears, no more food is drawn
end if
if whatdotcolour (snake (1).x + snakesize, snake (1).y + snakesize) = 0 %if the snake reaches the exit (detects the WHITE(whatdotcolour) exit point)
then
cls %clears the screen for the scoring updates
%%%%%%%%%%%%%%%%%%%%%%%%
%scoring system (after each completed level)
Window.Select (scorewin)
put "Your score is: ", score %score with out additional time bonus
var timeBonus : int := bonus - Time.Elapsed div 1000 %1 min timer (Time.Elapsed = 1000 milliseconds = 1 min)
put "Your time bonus is: ", timeBonus %time bonus
score := score + timeBonus %updated score = score as of to that point + time bonus
put "Your overall score is: ", score %score as of to that point + time bonus
bonus := 60 + (Time.Elapsed div 1000) %bonus reset
put "[press enter to continue to next round]" %gives the user time to read the score
View.Set ("graphics:" + intstr (455) + ";" + intstr (90) + ",title:Uncle Worm Score: " + intstr (score))
Window.Select (gamewin)
Input.Pause %waits for a button to be pressed before entering the next level
Window.Select (scorewin)
cls %clears the scorewin once the gamewin restarts
Window.Select (gamewin)
/*getch (ch)
if ch =(KEY_ENTER)
then*/
%%%%%%%%%%%%%%%%%%%%%%%%
%variable reset
newMaxX := newMaxX - levelUpX %updated screen dimensions = screen dimensions - 50
newMaxY := newMaxY - levelUpY %updated screen dimensions = screen dimensions - 40
View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY)) %displaying new screen for gamewin
snake (1).x := newMaxX div 2 %new x co-ords for the start of a new level
snake (1).y := newMaxY div 2 %new y co-ords for the start of a new level
colour (0) %reset the colour of any writing
colourback (7) %reset the colour of the background
cls %paints the background specified colour
makeFood := true %makign it possible to draw food
food %start redrawing food
scoreLevel := score + 50 %next level will be completed after an additional 50 points
end if
end loop
%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
Token
|
Posted: Mon May 30, 2005 6:40 pm Post subject: (No subject) |
|
|
[quote="Cervantes"] Token wrote: ... It's like you're sticking the snake's head in its ass!...
lmao great way of putting it |
|
|
|
|
|
|
|