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

Username:   Password: 
 RegisterRegister   
 Am i using Class's correctly?
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
TokenHerbz




PostPosted: Fri Aug 11, 2006 3:44 am   Post subject: Am i using Class's correctly?

Am i using the class correctly?


code:

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 <= 0 then
            x := maxx
        end if
        if y >= maxy then
            y := 0 - size
        elsif y + size <= 0 then
            y := maxy
        end if
    end move

    proc draw
        Draw.FillOval (x, y, size, size, c)
    end draw

end Ball


var balls : flexible array 1 .. 1 of ^Ball
new balls (1)
balls (1) -> 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
Sponsor
Sponsor
Sponsor
sponsor
TheOneTrueGod




PostPosted: Fri Aug 11, 2006 7:54 am   Post subject: (No subject)

Aye, you are -- Everything the class does is handled inside the class, so there is no overflow into the program. Good job Very Happy
Delos




PostPosted: Fri Aug 11, 2006 12:45 pm   Post subject: Re: Am i using Class's correctly?

TokenHerbz wrote:

code:

    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:
Turing:

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




PostPosted: Sat Aug 12, 2006 7:12 am   Post subject: (No subject)

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




PostPosted: Sat Aug 12, 2006 8:14 am   Post subject: (No subject)

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]




PostPosted: Sat Aug 12, 2006 1:10 pm   Post subject: (No subject)

Ha! I outdo Mr. Cervantes yet again! Wink

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




PostPosted: Sat Aug 12, 2006 1:57 pm   Post subject: (No subject)

Perhaps then you'd wish to write a tut on this Gandalf?

It wouldn't just benifit myself... Smile
[Gandalf]




PostPosted: Sat Aug 12, 2006 8:19 pm   Post subject: (No subject)

Ahh... I don't especially feel like it at the moment, and it would require looking back on more Turing. Maybe next summer? Smile

Well, I'll try and remember your request.
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: