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

Username:   Password: 
 RegisterRegister   
 Reseting A Variable
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Mr. T




PostPosted: Mon Mar 21, 2005 11:33 pm   Post subject: Reseting A Variable

I have gone through many possible combinations but i dont know why the timeBonus ISNT reseting??

code:
put "Your score is: ", score %score with out additional time bonus
        var bonus:int:=60000 div 1000 %1 min to get bonus (bonus = 60000 milliseconds = 60 seconds =1 min)
        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
        put "[press enter to continue to next round]" %gives the user time to read the score
        Input.Pause %waits for a button to be pressed before entering the next level


any suggestions of what i shuold put in my reset variable section?
Sponsor
Sponsor
Sponsor
sponsor
Naveg




PostPosted: Mon Mar 21, 2005 11:36 pm   Post subject: (No subject)

all that commenting makes it very hard to read

otherwise, it looks like bonus is always 60, which would reset timeBonus everytime you used it
Mr. T




PostPosted: Mon Mar 21, 2005 11:44 pm   Post subject: (No subject)

Confused thats exactly what isnt happening... cuz for some reason timeBonus just seems to continue from where it left off at the end of each level Confused im confused
StarGateSG-1




PostPosted: Mon Mar 21, 2005 11:53 pm   Post subject: (No subject)

ummm. you need to reset manually which you aren't doing.
Mr. T




PostPosted: Mon Mar 21, 2005 11:54 pm   Post subject: (No subject)

so what exactly should i put into my reset variable section then??? i tried a few combinations, and none worked.
zylum




PostPosted: Tue Mar 22, 2005 12:09 am   Post subject: (No subject)

what do you mean reseting? what value do you want it to be?
Mr. T




PostPosted: Tue Mar 22, 2005 12:11 am   Post subject: (No subject)

i want the same scoring system to apply to each level
1. var bonus:int:=60000 div 1000
2. var timeBonus:int :=bonus-Time.Elapsed div 1000
3. score:=score+timeBonus
.... maybe if i post my whole code, it will be easier to see what i mean.
code:


%variable declaration
View.Set ("graphics:500;400,title:Uncle Worm           Score: 0,nocursor")
colourback (black)
cls
colour (brightgreen)
var food2 : int
var foodSize : int
var makeFood:boolean:=true
var box1 : int := 0
var box4 : int := 0
var col : int := 7
var countdown : int := 5
var ballx1 : int := 200
var bally1 : int := 150
var ballsize : 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
%variable declaration for title page
var ch : string (1)
var num : int := 1
var font1, font2, font3, font4 : int
var col1, col2, col3 : int := brightred
font1 := Font.New ("serif:24")
font2 := Font.New ("arial:14")
Font.Draw ("Start", 50, 30, font1, brightgreen)
Font.Draw ("Instructions", 150, 30, font1, col2)
Font.Draw ("Quit", 330, 30, font1, col3)
var food1 : int

%title page
loop
    getch (ch)
    cls
    if ch = (KEY_LEFT_ARROW) then
        num -= 1
    end if
    if ch = (KEY_RIGHT_ARROW) then
        num += 1
    end if
    if num = 0 then
        num := 3
    elsif num = 4 then
        num := 1
    end if
    if num = 1 then
        col1 := brightgreen
    else
        col1 := brightred
    end if
    if num = 2 then
        col2 := brightgreen
    else
        col2 := brightred
    end if
    if num = 3 then
        col3 := brightgreen
    else
        col3 := brightred
    end if

    if ch = (KEY_ENTER) then
        case num of
            label 1 :exit %Proc for Start
            label 2 : %Proc for Save
            label 3 : %Proc for Load
        end case
    end if
    Font.Draw ("Start", 50, 30, font1, col1)
    Font.Draw ("Instructions", 150, 30, font1, col2)
    Font.Draw ("Quit", 330, 30, font1, col3)
end loop


%%%%%%%%%
% 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


%creates food in random locations
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
        drawoval (ballx1, bally1, ballsize + ring, ballsize + 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 (ballx1, bally1, ballsize, ballsize, col)
    if move then
        bally1 += 1
    elsif move2 then
        bally1 -= 1
    elsif move3 then
        ballx1 += 1
    elsif move4 then
        ballx1 -= 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 (ballx1, bally1, ballsize, ballsize, 12)     %deletes the tail
    delay (10)

    %wall collision detection
    if ballx1 = 4 or ballx1 = maxx - 4 or bally1 = 4 or bally1 = maxy - 4 then %collision detection to detect whether the ball has hit the walls
        View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY) + ",title:Uncle Worm           Score: " + intstr (score) + "             You Lose!") %if balls hits the wall, you lose!
        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
            View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY) + ",title:Uncle Worm           Score: " + intstr (score)) %if food eaten, score added to total at the top of the screen
            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) - 10, maxy, (maxx div 2) + 10, maxy - 5, 0)*/
            then Font.Draw("EXIT", (maxx div 2)-25,maxy-20, font2, 0) %level's exit point
            makeFood:=false %once exit appears, no more food is drawn
            end if
       
        if whatdotcolour (ballx1+ballsize,bally1+ballsize)=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)
        put "Your score is: ", score %score with out additional time bonus
        var bonus:int:=60000 div 1000 %1 min to get bonus (bonus = 60000 milliseconds = 60 seconds =1 min)
        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
        put "[press enter to continue to next round]" %gives the user time to read the score
        Input.Pause %waits for a button to be pressed before entering the next level
               /*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) + ",title:Uncle Worm           Score: " + intstr (score)) %displaying new screen dimensions and score
        ballx1:=newMaxX div 2 %new x co-ords for the start of a new level
        bally1:=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
zylum




PostPosted: Tue Mar 22, 2005 12:14 am   Post subject: (No subject)

the reason youre time bonus stays at 60 is because youre program runs too fast. you need some body code before you give timeBonus a value because such little time goes by that there really isnt a change in time (a few milliseconds)

code:
var score : int := 0
put "Your score is: ", score

delay (2000)

var bonus : int := 60000 div 1000
var timeBonus : int := bonus - Time.Elapsed div 1000
put "Your time bonus is: ", timeBonus   
score := score + timeBonus
put "Your overall score is: ", score 
put "[press enter to continue to next round]"       
Input.Pause     
Sponsor
Sponsor
Sponsor
sponsor
Mr. T




PostPosted: Tue Mar 22, 2005 12:18 am   Post subject: (No subject)

the problem seems to lie in the timeBonus
notice how each level it progressively decreases
i dont want that
----
it should only fluctuate based on the amount of time spent in a level
zylum




PostPosted: Tue Mar 22, 2005 12:28 am   Post subject: (No subject)

forget my post above. i didnt understand the problem until you posted the code. what you have to do is keep track of the time just before the level starts. then for the total time spent in that level, you just subtract the current time by the time at the beginning of the level... look how i used the STARTTIME variable in your code:

code:
%variable declaration
View.Set ("graphics:500;400,title:Uncle Worm           Score: 0,nocursor")
colourback (black)
cls
colour (brightgreen)
var food2 : int
var foodSize : int
var makeFood : boolean := true
var box1 : int := 0
var box4 : int := 0
var col : int := 7
var countdown : int := 5
var ballx1 : int := 200
var bally1 : int := 150
var ballsize : 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
%variable declaration for title page
var ch : string (1)
var num : int := 1
var font1, font2, font3, font4 : int
var col1, col2, col3 : int := brightred
font1 := Font.New ("serif:24")
font2 := Font.New ("arial:14")
Font.Draw ("Start", 50, 30, font1, brightgreen)
Font.Draw ("Instructions", 150, 30, font1, col2)
Font.Draw ("Quit", 330, 30, font1, col3)
var food1 : int

var STARTTIME : int := Time.Elapsed

%title page
loop
    getch (ch)
    cls
    if ch = (KEY_LEFT_ARROW) then
        num -= 1
    end if
    if ch = (KEY_RIGHT_ARROW) then
        num += 1
    end if
    if num = 0 then
        num := 3
    elsif num = 4 then
        num := 1
    end if
    if num = 1 then
        col1 := brightgreen
    else
        col1 := brightred
    end if
    if num = 2 then
        col2 := brightgreen
    else
        col2 := brightred
    end if
    if num = 3 then
        col3 := brightgreen
    else
        col3 := brightred
    end if

    if ch = (KEY_ENTER) then
        case num of
            label 1 :
                exit      %Proc for Start
            label 2 : %Proc for Save
            label 3 : %Proc for Load
        end case
    end if
    Font.Draw ("Start", 50, 30, font1, col1)
    Font.Draw ("Instructions", 150, 30, font1, col2)
    Font.Draw ("Quit", 330, 30, font1, col3)
end loop


%%%%%%%%%
% 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


%creates food in random locations
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
        drawoval (ballx1, bally1, ballsize + ring, ballsize + 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
STARTTIME := Time.Elapsed
loop
    drawbox (box1, newMaxY - 1, newMaxX - 1, box4, 14)
    drawfilloval (ballx1, bally1, ballsize, ballsize, col)
    if move then
        bally1 += 1
    elsif move2 then
        bally1 -= 1
    elsif move3 then
        ballx1 += 1
    elsif move4 then
        ballx1 -= 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 (ballx1, bally1, ballsize, ballsize, 12)     %deletes the tail
    delay (10)

    %wall collision detection
    if ballx1 = 4 or ballx1 = maxx - 4 or bally1 = 4 or bally1 = maxy - 4 then %collision detection to detect whether the ball has hit the walls
        View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY) + ",title:Uncle Worm           Score: " + intstr (score) + "             You Lose!") %if balls hits the wall, you lose!
        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
            View.Set ("graphics:" + intstr (newMaxX) + ";" + intstr (newMaxY) + ",title:Uncle Worm           Score: " + intstr (score)) %if food eaten, score added to total at the top of the screen
            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) - 10, maxy, (maxx div 2) + 10, maxy - 5, 0)*/
            then
        Font.Draw ("EXIT", (maxx div 2) - 25, maxy - 20, font2, 0)    %level's exit point
        makeFood := false   %once exit appears, no more food is drawn
    end if

    if whatdotcolour (ballx1 + ballsize, bally1 + ballsize) = 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)
        put "Your score is: ", score %score with out additional time bonus
        var bonus : int := 60000 div 1000 %1 min to get bonus (bonus = 60000 milliseconds = 60 seconds =1 min)
        var timeBonus : int := bonus - (Time.Elapsed-STARTTIME) div 1000 %1 min timer (Time.Elapsed = 1000 milliseconds = 1 min)
        STARTTIME := Time.Elapsed
        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
        put "[press enter to continue to next round]" %gives the user time to read the score
        Input.Pause %waits for a button to be pressed before entering the next level
              /*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) + ",title:Uncle Worm           Score: " + intstr (score)) %displaying new screen dimensions and score
        ballx1 := newMaxX div 2 %new x co-ords for the start of a new level
        bally1 := 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
Mr. T




PostPosted: Tue Mar 22, 2005 12:37 am   Post subject: (No subject)

thx it works great...but just for my own knowledge can u explain this line to me
code:

var timeBonus : int := bonus - (Time.Elapsed-STARTTIME) div 1000
zylum




PostPosted: Tue Mar 22, 2005 12:49 am   Post subject: (No subject)

i thought i did explaing it... STARTTIME is the time when the level starts. to find the total time spent in that one level, you subtract the start time from the current time (Time.Elapsed - STARTTIME). After giving time bonus a new value, you set the value of STARTTIME to the current time because you are starting a new level... oh wait thats a mistake, it should be reset after you press the enter key to start the new level, otherwise time is being wasted while waiting for keyboard input...

you should have this:

code:
var timeBonus : int := bonus - (Time.Elapsed-STARTTIME) 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
        put "[press enter to continue to next round]" %gives the user time to read the score
        Input.Pause %waits for a button to be pressed before entering the next level
              /*getch (ch)
         if ch =(KEY_ENTER)
         then*/
        STARTTIME := Time.Elapsed
StarGateSG-1




PostPosted: Tue Mar 22, 2005 12:23 pm   Post subject: (No subject)

I guess I don't need to respond except to say that zylum has it right on.
You had all the variable reseting and never chnaged the level time back so it just kept starting were it left off.
Mr. T




PostPosted: Tue Mar 22, 2005 9:55 pm   Post subject: (No subject)

kk thanks
Mr. T




PostPosted: Tue Mar 22, 2005 11:59 pm   Post subject: (No subject)

with the way i set up my code... is it possible to effectively use a flexible array to make the snake grow? or would i have to change stuff?
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 2  [ 16 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: