Computer Science Canada Help on resetting variables in loop of game |
Author: | xyne_eqars [ Wed Apr 16, 2014 10:06 pm ] | ||
Post subject: | Help on resetting variables in loop of game | ||
What is it you are trying to achieve? Perfecting a game code for an assignment What is the problem you are having? Resetting the lives to 5 and not to -1 Describe what you have tried to solve this problem Tried using if statements and View.Update Both didn't work or possibly I didn't use them correctly Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) setscreen ("graphics:400;450,nobuttonbar") View.Set ("offscreenonly") %---------------------------------------------------------------------------- %Declaring constants const pcolor : int := 52 const bcolor : int := 14 const bgcolor : int := 31 const textcolor : int := 16 const forgiveness : int := 3 const levels : int := 12 const paddleSpeed : int := 4 %---------------------------------------------------------------------------- var x, y, i_size, xdir, ydir : int var userPos : int var chars : array char of boolean var i_lives : int := 5 var i_speed, i_level : int var i_cycles : int var i_score : int var psize : int := 36 %---------------------------------------------------------------------------- x := 11 y := 11 userPos := round (((maxx - 10) / 2) - 10) i_size := 5 xdir := 1 ydir := 1 i_level := 1 i_speed := 16 i_cycles := 0 i_score := 0 %---------------------------------------------------------------------------- var font1 : int font1 := Font.New ("sans serif:16:bold") assert font1 > 0 Font.Draw ("Welcome to the Bouncy Circle game!", 15, 400, font1, brightblue) Font.Draw ("Use the arrow keys.", 15, 375, font1, brightblue) Font.Draw ("Careful, it does get harder!", 15, 350, font1, brightblue) Font.Draw ("Press any key to start...", 15, 325, font1, brightblue) View.Update var inp : string (1) getch (inp) %---------------------------------------------------------------------------- cls drawfill (maxx, maxy, bgcolor, bgcolor) drawfillbox (userPos - psize, 10, userPos + psize, 20, pcolor) %---------------------------------------------------------------------------- loop i_speed := 16 i_cycles := 0 i_level := 5 i_score := 0 i_level := 0 loop delay (i_speed) drawfilloval (x, y, i_size, i_size, 0) drawfill (maxx, maxy, bgcolor, 1) if x > maxx - 10 then xdir := -1 elsif x < 11 then xdir := 1 end if if y > maxy - 10 then ydir := -1 elsif y < 2 then ydir := 1 drawfill (maxx, maxy, red, 1) i_lives := i_lives - 1 end if color (textcolor) colorback (bgcolor) locatexy (1, maxy - 10) put "Lives : ", i_lives put "Level : ", i_level put "Score : ", i_score exit when i_lives = 0 x := x + (2 * xdir) y := y + (2 * ydir) drawfillbox (userPos - psize, 10, userPos + psize, 20, bgcolor) Input.KeyDown (chars) if chars (KEY_RIGHT_ARROW) and userPos < maxx - psize then userPos += paddleSpeed elsif chars (KEY_LEFT_ARROW) and userPos > psize then userPos -= paddleSpeed end if drawfillbox (userPos - psize, 10, userPos + psize, 20, pcolor) if y = 21 or y = 22 then %---------------------------------------------------------------------------- if x + i_size > userPos - (psize + forgiveness) and x + i_size < userPos + (psize + forgiveness) then ydir := 1 end if end if i_cycles := i_cycles + 1 if i_cycles mod 10 = 0 then i_score := i_score + 1 end if if i_cycles > 200 * i_level and i_level < levels then i_level := i_level + 1 i_speed := i_speed - 1 i_cycles := 1 psize := psize - 2 end if if i_cycles > 10000 then i_cycles := 1 end if drawfilloval (x, y, i_size, i_size, bcolor) View.Update end loop cls var font2, font3 : int font2 := Font.New ("sans serif:18:bold") font3 := Font.New ("sans serif:12:bold") assert font2 > 0 assert font3 > 0 Font.Draw ("GAME OVER!", 15, 400, font2, red) put "Your final score was ", i_score Font.Draw ("Enter y to try again or enter any other key to quit.", 15, 350, font3, brightblue) View.Update var playagain : string (1) getch (playagain) if playagain = "y" then else exit when playagain not= "y" end if end loop
Please specify what version of Turing you are using Turing 4.1.2 |
Author: | Raknarg [ Thu Apr 17, 2014 12:12 am ] |
Post subject: | RE:Help on resetting variables in loop of game |
i_speed := 16 i_cycles := 0 i_level := 5 i_score := 0 i_level := 0 Maybe you could also set your i_lives variable to 5 |
Author: | xyne_eqars [ Thu Apr 17, 2014 3:19 pm ] |
Post subject: | RE:Help on resetting variables in loop of game |
i already tried to do that: i_speed := 16 i_cycles := 0 i_level := 0 i_score := 0 i_lives := 5 |
Author: | Nathan4102 [ Thu Apr 17, 2014 3:21 pm ] |
Post subject: | RE:Help on resetting variables in loop of game |
Post your entire updated program in turing brackets so we can see what you're doing wrong. It's likely the placement of the statements |
Author: | xyne_eqars [ Thu Apr 17, 2014 3:57 pm ] |
Post subject: | RE:Help on resetting variables in loop of game |
Turing brackets? |
Author: | Dreadnought [ Thu Apr 17, 2014 5:43 pm ] | ||
Post subject: | Re: Help on resetting variables in loop of game | ||
If you wrap your code like so, it will look beautiful.
|
Author: | xyne_eqars [ Thu Apr 17, 2014 6:33 pm ] | ||
Post subject: | Re: Help on resetting variables in loop of game | ||
code:
|
Author: | Dreadnought [ Thu Apr 17, 2014 7:13 pm ] |
Post subject: | Re: Help on resetting variables in loop of game |
You might want to reset the positions of things when you restart the game. |
Author: | xyne_eqars [ Thu Apr 17, 2014 7:16 pm ] |
Post subject: | RE:Help on resetting variables in loop of game |
do u mean of this? i_speed := 0 i_cycles := 0 i_lives := 5 i_score := 0 i_level := 0 psize := 36 |
Author: | Dreadnought [ Thu Apr 17, 2014 9:41 pm ] |
Post subject: | Re: Help on resetting variables in loop of game |
No, I meant that you should consider resetting the position of things like the yellow ball. |
Author: | TWizard [ Thu May 08, 2014 2:09 pm ] | ||
Post subject: | RE:Help on resetting variables in loop of game | ||
Personally, i think you should add procedures and call them in the loop, this will help with what portion of the code is doing what.
and then calling the procedure when ever you want to do something with it. Using a procedure you can change global variables with ease. |