
-----------------------------------
TokenHerbz
Fri Aug 11, 2006 3:44 am

Am i using Class's correctly?
-----------------------------------
Am i using the class correctly?



setscreen ("offscreenonly")
class Ball
    export draw, move, set_move, set_position

    var size : int := 10
    var x, y : int := Rand.Int (200, 400)
    var vx, vy : int
    var speed : int
    var c : int := 7

    proc set_move
        vx := Rand.Int (-5, 5)
        vy := Rand.Int (-5, 5)
    end set_move

    proc set_position (xpos, ypos : int)
        x := xpos
        y := ypos
    end set_position

    proc move
        x += vx
        y += vy

        if x >= maxx then
            x := 0 - size
        elsif x + size = maxy then
            y := 0 - size
        elsif y + size  set_move

var mx, my, mb : int

var keys: array char of boolean

proc assign_new_balls
    new balls (upper (balls))
    balls (upper (balls)) -> set_position (mx, my)
    balls (upper (balls)) -> set_move
end assign_new_balls

loop
    Input.KeyDown(keys)
    Mouse.Where (mx, my, mb)
    if mb = 1 then
        new balls, upper (balls) + 1
        assign_new_balls
    end if

    if keys (KEY_ENTER) then
        new balls, 1
        assign_new_balls
    end if

    cls
    for i : 1 .. upper (balls)
        balls (i) -> move
        balls (i) -> draw
    end for
    View.Update
    delay (20)
end loop


-----------------------------------
TheOneTrueGod
Fri Aug 11, 2006 7:54 am


-----------------------------------
Aye, you are -- Everything the class does is handled inside the class, so there is no overflow into the program.  Good job :D

-----------------------------------
Delos
Fri Aug 11, 2006 12:45 pm

Re: Am i using Class's correctly?
-----------------------------------


    var size : int := 10
    var x, y : int := Rand.Int (200, 400)
    var vx, vy : int
    var speed : int
    var c : int := 7

    proc set_move
        vx := Rand.Int (-5, 5)
        vy := Rand.Int (-5, 5)
    end set_move



I would, however, stay away from any sort of hard-coded values like these that could be more usefully made to be dynamic.  Occaisionally, you will run into values that need to be hardcoded, but these particular ones would work better being parameters:

proc initialize (in_x, in_y, in_vx, in_vy, in_speed, in_colour : int)
   x := in_x
   %...
   % Could also add specific checks here to ensure that values are valid...
   % For instance, if a negative colour is indicated, you could flag it or just set it to a 'default' colour.
end initialize

% ...

new Ball, balls (upper (balls))
balls (upper (balls)) -> initialize (100, 100, Rand.Int (-5, 5), Rand.Int (0, 2), Rand.Int (0, 5), 7)
% Bask in the power!


Good stuff, keep up the work with classes.

-----------------------------------
TokenHerbz
Sat Aug 12, 2006 7:12 am


-----------------------------------
If an error does occur in turing, how do you get turing to notice it, and not crash, so you are able to change the variable?

Iv'e tried tests myself, and it seems even if i dont display the variables, if an error occures thats the end of the program, is there a way to bypass that?

-----------------------------------
Cervantes
Sat Aug 12, 2006 8:14 am


-----------------------------------
Error handling in Turing is extremely limited. Check the Error module in Turing Help. You can do a few things to trip errors and such, but nothing to rescue an error, as I can see.

-----------------------------------
[Gandalf]
Sat Aug 12, 2006 1:10 pm


-----------------------------------
Ha!  I outdo Mr. Cervantes yet again! ;)

TokenHerbz, the error handling in Turing is done using the handler keyword.  Try looking it up in the documentation, since we don't really have a tutorial on it.

-----------------------------------
TokenHerbz
Sat Aug 12, 2006 1:57 pm


-----------------------------------
Perhaps then you'd wish to write a tut on this Gandalf?

It wouldn't just benifit myself... :)

-----------------------------------
[Gandalf]
Sat Aug 12, 2006 8:19 pm


-----------------------------------
Ahh...   I don't especially feel like it at the moment, and it would require looking back on more Turing.  Maybe next summer? :)

Well, I'll try and remember your request.
