Computer Science Canada


Author:  xmen [ Fri Apr 02, 2004 3:37 pm ]

ok in this code with ball collision:

View.Set ("position:centre;centre,graphics:300;300,title:Bouncing Balls,nobuttonbar")
var totalballs : int := 15 %NOTE : to increase how many balls will spawn this number must be at least equal to the max number of balls
var x, y, dx, dy : array 1 .. totalballs of int
var dx_temp, dy_temp : array 1 .. totalballs of real
var movedist1, movedist2, collangle, mass, a1, a2, nX, nY, optimisedP : real

var px, py : int
px := maxx div 2
py := maxy div 2
var keys : array char of boolean
var hit : boolean := false
var respawn : boolean := true
var respawnx, respawny : int
var lives : int := 3
const ballradius := 8
const ballcollidedistance := ballradius * 2
var timer : int
setscreen ("offscreenonly")
mass := 1

for i : 1 .. totalballs
    x (i) := Rand.Int (20 + ballradius, maxx - 20 - ballradius)
    y (i) := Rand.Int (20 + ballradius, maxy - 20 - ballradius)
    dx (i) := Rand.Int (-3, 3)
    dy (i) := Rand.Int (-3, 3)
end for

proc drawscreen
    for i : 1 .. maxx by 20
        Draw.FillStar (i, 0, i + 20, 20, 2)
        Draw.FillStar (i, maxy - 20, i + 20, maxy, 2)
    end for
    for k : 20 .. maxy - 20 by 20
        Draw.FillStar (0, k, 20, k + 20, 2)
        Draw.FillStar (maxx - 20, k, maxx, k + 20, 2)
    end for
    locate (3, 3)
end drawscreen

proc ball_movement
    for i : 1 .. totalballs
        %wall bouncing
        if x (i) > maxx - 20 - ballradius then
            dx (i) := -dx (i)
            x (i) += dx (i)
        end if
        if x (i) < 20 + ballradius then
            dx (i) := -dx (i)
            x (i) += dx (i)
        end if
        if y (i) > maxy - 20 - ballradius then
            dy (i) := -dy (i)
            y (i) += dy (i)
        end if
        if y (i) < 20 + ballradius then
            dy (i) := -dy (i)
            y (i) += dy (i)
        end if

        x (i) += dx (i)
        y (i) += dy (i)
        drawfilloval (x (i), y (i), ballradius, ballradius, blue)
    end for
end ball_movement

proc balls_collide
    for i : 1 .. totalballs
        for k : i .. totalballs

            if k not= i then
                if Math.Distance (x (i), y (i), x (k), y (k)) < ballcollidedistance then
                    if y (k) - y (i) not= 0 and x (k) - x (i) not= 0 then
                        collangle := arctand ((y (k) - y (i)) / ((x (k) - x (i))))

                        nX := cosd (collangle)
                        nY := sind (collangle)

                        a1 := x (i) * nX + y (i) * nY
                        a2 := x (k) * nX + y (k) * nY

                        optimisedP := (2.0 * (a1 - a2)) / (mass + mass)

                        x (i) := x (i) - (round (optimisedP) * round (mass) * round (nX))
                        y (i) := y (i) - (round (optimisedP) * round (mass) * round (nY))
                        x (k) := x (k) + (round (optimisedP) * round (mass) * round (nX))
                        y (k) := y (k) + (round (optimisedP) * round (mass) * round (nY))
                        % moves the balls forward a step so they dont get stuck with each other( but the balls will still stick)
                        x (i) += dx (i)
                        y (i) += dy (i)
                        x (k) += dx (k)
                        y (k) += dy (k)
                    end if
                end if
            end if
        end for
    end for
end balls_collide

proc player_control
    Input.KeyDown (keys)
    if keys (KEY_UP_ARROW) then
        py += 2
    end if
    if keys (KEY_DOWN_ARROW) then
        py -= 2
    end if
    if keys (KEY_RIGHT_ARROW) then
        px += 2
    end if
    if keys (KEY_LEFT_ARROW) then
        px -= 2
    end if
    drawfilloval (px, py, ballradius, ballradius, brightred)
end player_control

proc player_boundaries
    if px > maxx - 50 - ballradius then
        px := maxx - 50 - ballradius
    end if
    if px < 50 + ballradius then
        px := 50 + ballradius
    end if
    if py > maxy - 50 - ballradius then
        py := maxy - 50 - ballradius
    end if
    if py < 50 + ballradius then
        py := 50 + ballradius
    end if
end player_boundaries

proc relocate
    lives -= 1
    delay (1000)
    px := 20 + ballradius
    py := 20 + ballradius
end relocate

    timer := Time.Elapsed
    totalballs := round (timer / 5000)
    exit when totalballs > 20 or hit = true


    for k : 1 .. totalballs
        if Math.Distance (px, py, x (k), y (k)) < ballcollidedistance then
            hit := true
        end if
    end for
    delay (10)
end loop

delay (500)
locate (7, 5)

put "Thanks for playing,\n       -Cervantes"

how does the collision part (credit THOUGHTFUL) works?? i dun reli understand but i need to inorder to explain to my teacher
so if someone knos plz tell me thx

Author:  recneps [ Fri Apr 02, 2004 5:33 pm ]
Post subject: 

i dont know sin/cos/tan.
if you do not know them, the easiest way is keep track of all the points, and math.distance between them all
if its less than radius(times 2!!!) then make them move opposite ways.

Author:  Hackster [ Fri Apr 02, 2004 6:45 pm ]
Post subject: 

UUm did you say explain to your teacher ??? does that mean you are goimg to pass Cervante's program off as your own or is it that he/she doesn't know the code either and they want to know ???

Author:  Jodo Yodo [ Fri Apr 02, 2004 7:16 pm ]
Post subject: 

Jebus, xmen, PLEASE stop posting forums where the entire thing is capital letters, and with multiple question marks. This is like, the fifth forum you've made like this.

Author:  Jodo Yodo [ Fri Apr 02, 2004 7:18 pm ]
Post subject: 

Due to my incredibly 1337 skills in Math, I can tell you that sine and cosine are used to calculate a certain point on the winding function (this is trigonometry). It's advanced Grade 10 Enriched math, and it does some funky stuff with the Pythagorean theorum (I like trigonometry, it's really useful). And are you seriously getting our programs and then submitting them to your teacher? Because you've asked for like, eight programs so far. Shame on you.


Author:  xmen [ Fri Apr 02, 2004 9:36 pm ]
Post subject: 

well yes i did ask n look for programs from this site, n yes mostly for hwk...however im like combining programs together depending on my needs, moreoever i still put credits on
i actually told my teacher that i got some coddes from sites, he said its ok as long as i understand them n can explain......this is the purpose of learning
so u guys may think im stealin programs or wutever, but im just learning from people just like many others do

Author:  Tony [ Fri Apr 02, 2004 9:45 pm ]
Post subject: 

wow xman - if you got a question about the code (especially that you found on this site anyways) why not post a reply to that thread instead of just taking the code and pasting it elsewhere... without even giving a credit of where you took it from Rolling Eyes

But yeah, collision is based on trig and momentum. You cover basic trig end of grade 10 math and inertia in grade 12 physics Confused

Author:  Dan [ Fri Apr 02, 2004 9:47 pm ]
Post subject: 

lreaning = reading some ones code and then making somting like it based on the conspetes.

stealing = copying some ones code and hading it in as your own.

you should at least ask the authour of the code if you are direcly copying and pasting code in to your own or just hading it in as your own.

Author:  Cervantes [ Sat Apr 03, 2004 2:08 pm ]
Post subject: 

If your teacher asks you about collision code... or better yet, before he marks your program, tell him that you did not right the code for the collision data. Tell him the math is too advanced, and that you haven't learned it in math class yet (not good if you had math last semester). Just use the collision data code, with credit to thoughtful, and rewrite the entire program, or as much as you can do (with credit to me for things you can't do, if there are anythings in there that you can't do Wink )