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

Username:   Password: 
 RegisterRegister   
 How can I make Items randomly appear in this game.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
XShotGunZX




PostPosted: Wed Jun 16, 2010 8:56 pm   Post subject: How can I make Items randomly appear in this game.

What is it you are trying to achieve?
Ok so, this game is basically done, it has 3 balls that come one after another which soon increases to 4 and then 5.. thats the ordered process and these balls just need to make you lose health.. that part I'm done... but I want to make stuff like Health items, power ups, special balls that damage for alot.. appear randomly throughout the game.. how would I go about doing this...


What is the problem you are having?
For now, all I can do is set a specific time to which they can appear and a specific time which if they pass they do not appear any more. and this doesn't even work properly because due to the code, when I touch the health powerup with my brick.. it will give me the health boost, but it will create the graphic of the health powerup again just ONE more time, due to the code but this new graphic will not give any health boost and will go straight past the brick.


Describe what you have tried to solve this problem
I've tried using if statements and randints in order to randomly create an integer.. and if that integer is a certain number then the item will appear.. but the no matter where I put this.. the randint continuously goes on without delay so when it actually does get to the number required to make the item appear, it does not stay at that number so the item gets stuck on the screen.. so I removed that and just went to the TIMED idea.. but I would like to know how to make it randomly appear.


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
Well heres my code... y7 to y11 and x7 to x11 is all the movement and the graphics of the health icon, y6 and x6 is a special ball that blends into the background and is only visible at about midsceen and can cause lots of damage.. i would like this to become random as well.

Turing:


View.Set ("graphics:900;550,offscreenonly,nobuttonbar,title:OMG BALLS ARE FALLING FROM THE SKY")

var x, y, v, x2, y2, v2, x3, y3, v3, x4, y4, v4, x5, y5, v5, x6, y6, v6, xBrick, x2Brick, Time1, random, y7, x7, v7, y8, x8, y9, x9, x10, y10, x11, y11 : int := 0
var mypic : int := Pic.FileNew ("EarthPicture2.jpg")
var Health : int := 1050
var xPos, yPos, button : int
var bar : int := 120

process Time2
    loop
        Time1 := Time1 + 1
        delay (1000)
    end loop
end Time2
fork Time2

proc Update
    Mouse.Where (xPos, yPos, button)
    xBrick := xPos - bar div 2
    x2Brick := xBrick + bar

    y := y - v
    y2 := y2 - v2
    y3 := y3 - v3
    y4 := y4 - v4
    y5 := y5 - v5
    y6 := y6 - v6
    y7 := y7 - v7
    y8 := y8 - v7
    y9 := y9 - v7
    y10 := y10 - v7
    y11 := y11 - v7

    % STARTING BALL
    if y < 1 then
        y := maxy
        randint (x, 0, maxx)
        randint (v, 3, 12)
        Health := Health - 50
    elsif y < 146 and x > xBrick and x < x2Brick then
        Health := Health + 0
        y := maxy
        randint (x, 0, maxx)
        randint (v, 3, 12)
    end if

    % FAST STARTING BALL
    if y2 < 1 then
        y2 := maxy
        randint (x2, 0, maxx)
        randint (v2, 8, 12)
        Health := Health - 50
    elsif y2 < 146 and x2 > xBrick and x2 < x2Brick then
        Health := Health + 0
        y2 := maxy
        randint (x2, 0, maxx)
        randint (v2, 3, 12)
    end if

    % STARTING BALL
    if y3 < 1 then
        y3 := maxy
        randint (x3, 0, maxx)
        randint (v3, 3, 12)
        Health := Health - 50
    elsif y3 < 146 and x3 > xBrick and x3 < x2Brick then
        Health := Health + 0
        y3 := maxy
        randint (x3, 0, maxx)
        randint (v3, 3, 12)
    end if

    % MEDIUM LEVEL BALL (MEDIUM DIFFICULTY/LEVEL 4)
    if y4 < 1 and Time1 > 29 then
        y4 := maxy
        randint (x4, 0, maxx)
        randint (v4, 15, 20)
        Health := Health - 50
    elsif y4 < 146 and x4 > xBrick and x4 < x2Brick and Time1 > 20 then
        Health := Health + 0
        y4 := maxy
        randint (x4, 0, maxx)
        randint (v4, 15, 20)
    end if

    % HIGH LEVEL SPEED BALL (HARD DIFFICULTY/LEVEL 5)
    if y5 < 1 and Time1 > 59 then
        y5 := maxy
        randint (x5, 0, maxx)
        randint (v5, 25, 30)
        Health := Health - 50
    elsif y5 < 146 and x5 > xBrick and x5 < x2Brick and Time1 > 40 then
        Health := Health + 0
        y5 := maxy
        randint (x5, 0, maxx)
        randint (v5, 25, 30)
    end if

    % HEALTH ICON
    if y7 < 1 and Time1 > 17 and Time1 < 23 then
        y7 := maxy
        y8 := y7 - 6
        y9 := y7 + 6
        y10 := y7 - 20
        y11 := y7 + 20
        randint (x7, 0, maxx)
        x8 := x7 - 20
        x9 := x7 + 20
        x10 := x7 - 6
        x11 := x7 + 6
        randint (v7, 3, 3)
    elsif y7 < 146 and x7 > xBrick and x7 < x2Brick and Time1 > 17 and Time1 < 23 then
        Health := Health + 300
        y7 := maxy
        y8 := y7 - 6
        y9 := y7 + 6
        y10 := y7 - 20
        y11 := y7 + 20
        randint (x7, 0, maxx)
        x8 := x7 - 20
        x9 := x7 + 20
        x10 := x7 - 6
        x11 := x7 + 6
        randint (v7, 3, 3)
    end if

    % NINJA BALL
    if y6 < 1 and Time1 > 7 and Time1 < 38 then
        y6 := maxy
        randint (x6, 0, maxx)
        randint (v6, 5, 5)
    elsif y6 < 146 and x6 > xBrick and x6 < x2Brick and Time1 > 7 and Time1 < 38 then
        Health := Health + 0
        y6 := maxy
        randint (x6, 0, maxx)
        randint (v6, 4, 6)
    elsif y6 < 145 and Time1 > 7 and Time1 < 38 then
        Health := Health - 7
    end if

end Update

proc Drawobjects
    cls

    Pic.Draw (mypic, 0, 0, picUnderMerge)
    drawfillbox (xBrick, 130, x2Brick, 145, yellow)
    drawfilloval (x, y, 10, 10, brightpurple)
    drawfilloval (x2, y2, 10, 10, blue)
    drawfilloval (x3, y3, 10, 10, green)
    drawfilloval (x4, y4, 10, 10, purple)
    drawfilloval (x5, y5, 10, 10, brightgreen)
    drawfilloval (x6, y6, 25, 25, black)
    drawfilloval (x7, y7, 25, 25, white)
    drawfillbox (x8, y8, x9, y9, brightred)
    drawfillbox (x10, y10, x11, y11, brightred)
    drawfillbox (0, 0, Health, 25, brightred)

    View.Update
end Drawobjects

loop
    Update

    Drawobjects

    Time.DelaySinceLast (33)
    exit when Health < 1
end loop



Please specify what version of Turing you are using
turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Wed Jun 16, 2010 9:04 pm   Post subject: Re: How can I make Items randomly appear in this game.

XShotGunZX @ Wed Jun 16, 2010 8:56 pm wrote:

I've tried using if statements and randints in order to randomly create an integer.. and if that integer is a certain number then the item will appear..

Oh, so close!

That is actually how random events are typically done. At certain spots (perhaps all of the time!) there's a random chance for an event to occur. Your Rand.Ints are dice with as many sides as you want. So what should you do once the number you are looking for is rolled? You should take note of that occasion -- events, flag (boolean) variables, and timers are some of the possible approaches.

A different approach with timers, is to use a random length of time that events wait for before happening.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
XShotGunZX




PostPosted: Wed Jun 16, 2010 9:08 pm   Post subject: Re: How can I make Items randomly appear in this game.

Tony @ Wed Jun 16, 2010 9:04 pm wrote:
XShotGunZX @ Wed Jun 16, 2010 8:56 pm wrote:

I've tried using if statements and randints in order to randomly create an integer.. and if that integer is a certain number then the item will appear..

Oh, so close!

That is actually how random events are typically done. At certain spots (perhaps all of the time!) there's a random chance for an event to occur. Your Rand.Ints are dice with as many sides as you want. So what should you do once the number you are looking for is rolled? You should take note of that occasion -- events, flag (boolean) variables, and timers are some of the possible approaches.

A different approach with timers, is to use a random length of time that events wait for before happening.


so umm.. I dont understand.. what can I do? I know I can't use delays properly because of the Time.DelaySinceLast (33) and the program won't run smoothly anymore.. so what approach should I use?

I put this into the procedure and when it reaches 10 seconds it just freezes......

code:

    if Time1 > 10 then
        randint (random, 1, 2)
        loop
            if y7 < 1 and random = 1 then
                y7 := maxy
                y8 := y7 - 6
                y9 := y7 + 6
                y10 := y7 - 20
                y11 := y7 + 20
                randint (x7, 0, maxx)
                x8 := x7 - 20
                x9 := x7 + 20
                x10 := x7 - 6
                x11 := x7 + 6
                randint (v7, 3, 3)
            elsif y7 < 146 and x7 > xBrick and x7 < x2Brick and random = 1 then
                Health := Health + 300
                y7 := maxy
                y8 := y7 - 6
                y9 := y7 + 6
                y10 := y7 - 20
                y11 := y7 + 20
                randint (x7, 0, maxx)
                x8 := x7 - 20
                x9 := x7 + 20
                x10 := x7 - 6
                x11 := x7 + 6
                randint (v7, 3, 3)
            end if
            exit when y7 < 146 and x7 > xBrick and x7 < x2Brick or y7 < 1
        end loop
    end if
Tony




PostPosted: Wed Jun 16, 2010 9:38 pm   Post subject: RE:How can I make Items randomly appear in this game.

It doesn't "freeze", it just runs inside the loop for a while; but you don't update the screen while it does this, so it appears as if nothing is happening.

The delays stop the execution. This is not what you want. If you want to rely on certain time, look into the use of the Time module -- timemodule. There were quite a variety of different approaches listed though.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
XShotGunZX




PostPosted: Wed Jun 16, 2010 9:39 pm   Post subject: Re: RE:How can I make Items randomly appear in this game.

Tony @ Wed Jun 16, 2010 9:38 pm wrote:
It doesn't "freeze", it just runs inside the loop for a while; but you don't update the screen while it does this, so it appears as if nothing is happening.

The delays stop the execution. This is not what you want. If you want to rely on certain time, look into the use of the Time module -- timemodule. There were quite a variety of different approaches listed though.


i dont want to rely on time actually.. I think that would be the worst thing to rely on for my program.. but thats the only thing I can think of.. what do you think the best option is for my program? that will make items appear randomly from start to finish..
copthesaint




PostPosted: Thu Jun 17, 2010 11:00 am   Post subject: Re: RE:How can I make Items randomly appear in this game.

XShotGunZX @ Wed Jun 16, 2010 wrote:
Tony @ Wed Jun 16, 2010 9:38 pm wrote:
It doesn't "freeze", it just runs inside the loop for a while; but you don't update the screen while it does this, so it appears as if nothing is happening.

The delays stop the execution. This is not what you want. If you want to rely on certain time, look into the use of the Time module -- timemodule. There were quite a variety of different approaches listed though.


i dont want to rely on time actually.. I think that would be the worst thing to rely on for my program.. but thats the only thing I can think of.. what do you think the best option is for my program? that will make items appear randomly from start to finish..


You thought wrong, Time.ElapsedCPU, Thats a function you can use to get the time of the program, play around with it, and you'll get how to make a random timer. If you rely on a random number of loops, of if a variable is a certain int your will just cause problems for your self, there's a reason why Tony is right, and it's not just because hes an admin of compsci.ca, and not because hes been programing I'd say at least 10 years, and its not even because hes on he has degrees from University of Toronto, :p. No, but really it is the most efficent way of doing this in turing, and probably every other programing language and if your new, it's always wise to take advice from someone that has been programing that long, However there are other ways of doing this, and you can make up your own, but this is the most efficient you will find.
Tony




PostPosted: Thu Jun 17, 2010 11:59 am   Post subject: Re: RE:How can I make Items randomly appear in this game.

copthesaint @ Thu Jun 17, 2010 11:00 am wrote:
and its not even because hes on he has degrees from University of Toronto, :p.

I am still doing my undergrad at Waterloo Wink
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
copthesaint




PostPosted: Thu Jun 17, 2010 12:57 pm   Post subject: RE:How can I make Items randomly appear in this game.

:p how many years left? Anyways I am sure you get the point though XShot, we wouldn't suggest this, if it wasn't a good way to be doing this. Is time not the 4th demention of our existence? Though current, we move along it like everything else
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: