Hello,
I'm trying to make a "snake" simulation, where all parts of the body follows the head path.
Until now, I've made something near to what I want. But I think it can be smoother (and more reactive).
Can you help me to improve my "simulation" ?
When the program is running in 30 fps, the last circle does not moves immediately when the first circle does.
I will continue to improve my program
(You just have to copy paste the source in a prg file, no external resources needed)
controls :
mouse to move the snake head
_1 to set fps to 30
_2 to set fps to max
import "mod_video"
import "mod_key"
import "mod_text"
import "mod_sound"
import "mod_wm"
import "mod_map"
import "mod_draw"
import "mod_say"
import "mod_file"
import "mod_text"
import "mod_multi"
import "mod_mouse"
import "mod_proc"
import "mod_screen"
import "mod_rand"
import "mod_grproc"
import "mod_math"
import "mod_scroll"
import "mod_blendop"
import "mod_effects"
import "mod_timers"
#DEFINE CIRCLE_SIZE 32
type point;
int x;
int y;
end
local
point positions_backup[CIRCLE_SIZE];
point last_position;
int leader;
end
global
int tail_length = 50;
end
begin
set_mode(800, 600, 32);
set_fps(30,0);
head();
loop
if(key(_esc))
exit();
end
if(key(_1))
set_fps(30,0);
end
if(key(_2))
set_fps(0,0);
end
frame;
end
end
process head()
private
int i;
int previous_leader, new_leader;
int incr_dist;
point incr_point;
end
begin
graph = draw_fcircle(x, y, CIRCLE_SIZE / 2);
previous_leader = id;
for(i = 0; i < tail_length; i++)
new_leader = tail(previous_leader);
previous_leader = new_leader;
end
loop
x = mouse.x;
y = mouse.y;
if(x != last_position.x || y != last_position.y)
incr_dist = fget_dist(last_position.x, last_position.y, x, y);
if(incr_dist > 100) incr_dist = 100; end
for(i = 0; i < incr_dist; i++)
incr_point.x = last_position.x + get_distx(fget_angle(last_position.x, last_position.y, x, y), i);
incr_point.y = last_position.y + get_disty(fget_angle(last_position.x, last_position.y, x, y), i);
backup_point(incr_point);
end
end
frame;
move_draw(graph, x, y);
last_position.x = x;
last_position.y = y;
end
end
process tail(int leader)
private
int i;
int previous_leader, new_leader;
int incr_dist;
point incr_point;
end
begin
graph = draw_fcircle(x, y, CIRCLE_SIZE / 2);
loop
x = leader.positions_backup[CIRCLE_SIZE].x;
y = leader.positions_backup[CIRCLE_SIZE].y;
if(x != last_position.x || y != last_position.y)
incr_dist = fget_dist(x, y, last_position.x, last_position.y);
if(incr_dist > 100) incr_dist = 100; end
for(i = 0; i < incr_dist; i++)
incr_point.x = last_position.x + get_distx(fget_angle(last_position.x, last_position.y, x, y), i);
incr_point.y = last_position.y + get_disty(fget_angle(last_position.x, last_position.y, x, y), i);
backup_point(incr_point);
end
end
frame;
move_draw(graph, x, y);
last_position.x = x;
last_position.y = y;
end
end
function backup_point(point position)
begin
for(x = CIRCLE_SIZE; x > 0; x--)
father.positions_backup[x] = father.positions_backup[x-1];
end
father.positions_backup[0] = position;
end