- This wiki is out of date, use the continuation of this wiki instead
 
Tutorial:Cosine
From FenixWiki
(Difference between revisions)
												
			
			| Revision as of 22:59, 29 May 2007 (edit) Sandman (Talk | contribs) ← Previous diff  | 
				Revision as of 23:21, 29 May 2007 (edit) (undo) Sandman (Talk | contribs) Next diff →  | 
			||
| Line 4: | Line 4: | ||
| <pre> | <pre> | ||
| Program cosine; | Program cosine; | ||
| + | Const | ||
| + | seconds = 2.3; | ||
| + | ball_width = 20; | ||
| + | ball_height = 20; | ||
| + | screen_width = 320; | ||
| + | screen_height = 200; | ||
| + | screen_depth = 8; | ||
| + | screen_fps = 60; | ||
| + | cos_factor = 2000; | ||
| + | sin_factor = 4000; | ||
| + | timer_spawn = 0; | ||
| + | timer_ball = 1; | ||
| Begin | Begin | ||
| - |     set_fps( | + | // make the movement a bit more smooth | 
| + | set_fps(screen_fps,0); | ||
| - |     timer[ | + | // spawn a ball process every <seconds> seconds with random colors. | 
| + | timer[timer_spawn]= seconds*100; | ||
| Repeat | Repeat | ||
| - |         if(timer[ | + | if(timer[timer_spawn]>=seconds*100) | 
| - |             timer[ | + | timer[timer_spawn]-=seconds*100; | 
| - |             x = new_map( | + | x = new_map(ball_width,ball_height,screen_depth); | 
| map_clear(0,x,rgb(rand(0,255),rand(0,255),rand(0,255))); | map_clear(0,x,rgb(rand(0,255),rand(0,255),rand(0,255))); | ||
| ball(x); | ball(x); | ||
| Line 19: | Line 33: | ||
| Until(key(_esc)) | Until(key(_esc)) | ||
| + | // kill all other remaining processes and exit | ||
| let_me_alone(); | let_me_alone(); | ||
| + | exit(); | ||
| End | End | ||
| Process ball(graph) | Process ball(graph) | ||
| + | Private | ||
| + | int start_time; | ||
| Begin | Begin | ||
| - |      | + | // remember starting time | 
| + | start_time = timer[timer_ball]; | ||
| + | |||
| + | // position the ball according to the passed time (timer[timer_ball]) | ||
| Loop | Loop | ||
| - |         x =  | + | x = screen_width /2 + cos((timer[timer_ball]-start_time)*cos_factor) | 
| - |         y =  | + | * (screen_width /2-ball_width /2); | 
| - | + | y = screen_height/2 + sin((timer[timer_ball]-start_time)*sin_factor) | |
| + | * (screen_height/2-ball_height/2); | ||
| frame; | frame; | ||
| End | End | ||
| Line 36: | Line 58: | ||
| End | End | ||
| </pre> | </pre> | ||
| - | Used in example: [[set_fps]](), [[new_map]](), [[map_clear]](), [[key]](), [[let_me_alone]](), [[cos]](), [[sin]](), [[timer]], [[x]], [[y]] | + | Used in example: [[set_fps]](), [[new_map]](), [[map_clear]](), [[rand]](), [[key]](), [[let_me_alone]](), [[cos]](), [[sin]](), [[timer]], [[x]], [[y]] | 
Revision as of 23:21, 29 May 2007
Some sine and cosine program. Check it out. More of example code, but useful anyway.
Example code
Program cosine;
Const
    seconds       = 2.3;
    ball_width    = 20;
    ball_height   = 20;
    screen_width  = 320;
    screen_height = 200;
    screen_depth  = 8;
    screen_fps    = 60;
    cos_factor    = 2000;
    sin_factor    = 4000;
    timer_spawn   = 0;
    timer_ball    = 1;
Begin
    // make the movement a bit more smooth
    set_fps(screen_fps,0);
    // spawn a ball process every <seconds> seconds with random colors.
    timer[timer_spawn]= seconds*100;
    Repeat
        if(timer[timer_spawn]>=seconds*100)
            timer[timer_spawn]-=seconds*100;
            x = new_map(ball_width,ball_height,screen_depth);
            map_clear(0,x,rgb(rand(0,255),rand(0,255),rand(0,255)));
            ball(x);
        end
        frame;
    Until(key(_esc))
    // kill all other remaining processes and exit
    let_me_alone();
    exit();
End
Process ball(graph)
Private
    int start_time;
Begin
    // remember starting time
    start_time = timer[timer_ball];
    // position the ball according to the passed time (timer[timer_ball])
    Loop
        x = screen_width /2 + cos((timer[timer_ball]-start_time)*cos_factor)
                              * (screen_width /2-ball_width /2);
        y = screen_height/2 + sin((timer[timer_ball]-start_time)*sin_factor)
                              * (screen_height/2-ball_height/2);
        frame;
    End
End
Used in example: set_fps(), new_map(), map_clear(), rand(), key(), let_me_alone(), cos(), sin(), timer, x, y
