Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help on resetting variables in loop of game
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
xyne_eqars




PostPosted: 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



Turing:





Please specify what version of Turing you are using
Turing 4.1.2
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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
xyne_eqars




PostPosted: 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
Nathan4102




PostPosted: 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
xyne_eqars




PostPosted: Thu Apr 17, 2014 3:57 pm   Post subject: RE:Help on resetting variables in loop of game

Turing brackets?
Dreadnought




PostPosted: 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.

code:
[syntax="Turing"]

... code ...

[/syntax]
xyne_eqars




PostPosted: Thu Apr 17, 2014 6:33 pm   Post subject: Re: Help on resetting variables in loop of game

code:
Turing:

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, font2 : int
font1 := Font.New ("sans serif:16:bold")
font2 := Font.New ("sans serif:16:bold,italic")
assert font1 > 0
assert font2 > 0

Font.Draw ("Welcome to the Bouncy Circle game!", 16, 400, font1, 79)
Font.Draw ("Use the arrow keys.", 100, 325, font1, 103)
Font.Draw ("Careful, it does get harder!", 65, 300, font2, 53)
put ""
Font.Draw ("Press e for easy level", 80, 220, font1, 50)
Font.Draw ("Press m for medium level", 70, 200, font1, 50)
Font.Draw ("Press h for hard level", 80, 180, font1, 50)

View.Update

var inp : string (1)
getch (inp)
%----------------------------------------------------------------------------
cls

drawfill (maxx, maxy, bgcolor, bgcolor)

drawfillbox (userPos - psize, 10, userPos + psize, 20, pcolor)
%----------------------------------------------------------------------------
loop
    if inp = "e" then
        i_speed := 16
        i_cycles := 0
        i_lives := 5
        i_score := 0
        i_level := 0
        psize := 36
    end if
   
    if inp = "m" then
        i_speed := 13
        i_cycles := 0
        i_lives := 4
        i_score := 0
        i_level := 0
        psize := 33
    end if
   
    if inp = "h" then
        i_speed := 10
        i_cycles := 0
        i_lives := 3
        i_score := 0
        i_level := 0
        psize := 30
    end if


    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 font3, font4 : int
    font3 := Font.New ("sans serif:18:bold")
    font4 := Font.New ("sans serif:12:bold")
    assert font3 > 0
    assert font4 > 0
   
    Font.Draw ("GAME OVER!", 125, 375, font3, 40)
    put "Your final score was ", i_score
   
        var i_highscore : int := 0
    if i_highscore < i_score then
    i_highscore := i_score
        put "Congrats! You beat the highscore!"
        put "The new highscore is ", i_highscore
    end if
   
    Font.Draw ("Would you like to try again?", 100, 325, font4, 76)
    Font.Draw ("Enter y for yes and n for no.", 97, 300, font4, 76)
   
    View.Update
           
    var playagain : string (1)
    getch (playagain)

    if playagain = "y" then
   
    else
        exit when playagain = "n"
    end if

end loop



Dreadnought




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
xyne_eqars




PostPosted: 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
Dreadnought




PostPosted: 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.
TWizard




PostPosted: 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.

Turing:


var variable1Here, variable2Here : int

procedure nameHere
variable1Here := 1
variable2Here := 3
end nameHere



and then calling the procedure when ever you want to do something with it. Using a procedure you can change global variables with ease.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 11 Posts ]
Jump to:   


Style:  
Search: