Am i using Class's correctly?
Author |
Message |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: 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
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: 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 Very Happy](http://compsci.ca/v3/images/smiles/icon_biggrin.gif) |
|
|
|
|
![](images/spacer.gif) |
Delos
![](http://www.members.shaw.ca/rfolz/delos_avatar.gif)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: 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? |
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: 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. |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: Sat Aug 12, 2006 1:10 pm Post subject: (No subject) |
|
|
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. |
|
|
|
|
![](images/spacer.gif) |
TokenHerbz
![](http://compsci.ca/v3/uploads/user_avatars/717170395558e628d9452b.jpg)
|
Posted: 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 Smile](http://compsci.ca/v3/images/smiles/icon_smile.gif) |
|
|
|
|
![](images/spacer.gif) |
[Gandalf]
![](http://compsci.ca/v3/uploads/user_avatars/189297994e4c716fec7f1.png)
|
Posted: 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?
Well, I'll try and remember your request. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
|
|