Computer Science Canada

Game Help - Using Rand.Int in a Variable

Author:  sliverofshadows475 [ Sun Dec 21, 2008 12:31 pm ]
Post subject:  Game Help - Using Rand.Int in a Variable

Hey guys, I've got a new problem with my game. Here's the syntax.

Turing:

View.Set ("graphics,offscreenonly")

var mypic :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/heavycruiser1.jpg")
var mypic2 :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/spaveinvaders101.jpg")
var mypic3 :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/spaveinvaders102.jpg")
var mypic4 :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/spaveinvaders103.jpg")
var mypic5 :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/spaveinvaders104.jpg")
var mypic6 :int:= Pic.FileNew ("C:/Users/Owner/Documents/Turing 4.1.1/Final Summitive Evaluation/spaveinvaders105.jpg")
var x : int := 300 % x axis
var y : int := 50  % y axis
var keys : array char of boolean %for Input.KeyDown
var hp : int :=1  % Hit Points aliens spaceships
var score : int :=0 %Total Points Earned
var life : int :=3  %Number of lives spaceship has
var FirePercentageAlien1 : int := Rand.Int (1,3)
var FirePercentageAlien2 : int := Rand.Int (1,3)
var FirePercentageAlien3 : int := Rand.Int (1,3)
var FirePercentageAlien4 : int := Rand.Int (1,3)
var FirePercentageAlien5 : int := Rand.Int (1,3)


   

loop


    put "Score = ",score
    put "Lives = ",life
    put "Fire Percentage = ",FirePercentageAlien1



    Input.KeyDown (keys)
    if keys (KEY_LEFT_ARROW) and x > 0 then % left arrow key
        x -= 1 %moves character left by 1 pixel
    end if
    if keys (KEY_RIGHT_ARROW) and x < maxx then % right arrow key
        x += 1    %moves character right by 1 pixel
    end if
    if keys (' ') then % space bar
        drawline (x+10,315,x+10,95,red)%red line in middle
        drawline (x+9,315,x+9,95,blue)%blue line on left of red
        drawline (x+11,315,x+11,95,blue)%blue line on right of red
        drawline (x+8,315,x+8,95,blue)%blue line on left of left blue
        drawline (x+12,315,x+12,95,blue) %blue line on right of right blue
    end if
    if whatdotcolour (x+10,295)=red then %collision detection for score counting
        score +=5 %increases score by 5 for every time the red line hits (x+10,295)
    end if


   
    Pic.Draw (mypic,x-20,y-20,0)%spaceship
    delay (2)
    Pic.Draw (mypic2,50,300,0)%alien 1
    Pic.Draw (mypic3,150,300,0)%alien 2
    Pic.Draw (mypic4,250,300,0)%alien 3
    Pic.Draw (mypic5,350,300,0)%alien 4
    Pic.Draw (mypic6,450,300,0)%alien 5


    View.Update %prevents flashing screen
    cls
end loop


The help I need is referring to lines 14-18: FirePercentageAlien1-5.

As you see, I have given the values as Rand.Int, allowing for the values to equal anything from 1 to 3.

I'm planning on using this as my Firing Percentage for my alien enemies. So, then if the FirePercentageAlien1 = 3, then it shoots.

The problem with this is that since it isn't looped, it only generates 1 number. I want it to continously generate numbers, but I've tried putting a loop into the variable, but it doesn't seem to work.

When this works properly, every second, I want the alien to have a 33.4% chance of firing a beam.

But, with this, the aliens have a 33.4% chance of continuously (sp?) firing a beam, throughout the whole game.

Any help would be greatly appreciated.

Thanks,
-sliverofshadows475

Author:  TheGuardian001 [ Sun Dec 21, 2008 12:52 pm ]
Post subject:  Re: Game Help - Using Rand.Int in a Variable

in Lines 14-18, get rid of the Rand.Int calls. Then, right at the start of the loop,

code:

FirePercentageAlien1 := Rand.Int(1,3)
FirePercentageAlien2 := Rand.Int(1,3)
%rest of alien variables here...
%the rest of your code here


should fix the problem.

Author:  sliverofshadows475 [ Sun Dec 21, 2008 12:54 pm ]
Post subject:  Re: Game Help - Using Rand.Int in a Variable

Thank you very much, it worked exactly as I needed it Very Happy

Author:  syntax_error [ Sun Dec 21, 2008 5:46 pm ]
Post subject:  RE:Game Help - Using Rand.Int in a Variable

Just noted some other things you might want to rethink about, you could hold all your images in an array as well and that would be a more cleaner looking code; another thing have all your images in the folder as your turing file so you only have to move the folder to transfer the game to someone else et cetera.

Author:  S_Grimm [ Sun Dec 21, 2008 6:32 pm ]
Post subject:  RE:Game Help - Using Rand.Int in a Variable

Don't Define the path as "C:\Documents\....." use root instead. So if you have the project stored in "C:\Final" have a folder in there called something like "resources", then call the new Pics by

code:

var mypic6 :int:= Pic.FileNew ("res/spaveinvaders105.jpg")



here's a test code using a .gif file.
code:

View.Set ("graphics,offscreenonly")

var mypic6 :int:= Pic.FileNew ("res/bicho.gif")

var x := 0
loop
if (x < 600) then

View.Update
cls
Pic.Draw (mypic6,x,300,0)%alien 5
x += 1
end if
exit when x = 600
end loop


: