Snake simulation

Started by Grew, September 01, 2015, 02:54:06 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Grew


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
My game developer instagram :
https://www.instagram.com/ben_dev_game/

Grew



I have made some modifications in my program. Here is the final version :



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"
import "mod_mem"


type point;
    int x;
    int y;
end


begin


set_mode(800, 600, 32);
set_fps(0,0);
head(20, 32);
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(int tail_length, int diameter)
private
point* positions_backup;
int i, j;
point last_position;
end
begin
positions_backup = calloc(tail_length * diameter, sizeof(point));

graph = draw_fcircle(x, y, diameter / 2);
x = mouse.x = 0;
y = mouse.y = 0;
for(i = 1; i < tail_length; i++)
tail(i, positions_backup, diameter);
end
loop

x = mouse.x;
y = mouse.y;


if(x != last_position.x || y != last_position.y)
for(i = 0; i < fget_dist(last_position.x, last_position.y, x, y); i++)
for(j = tail_length * diameter ; j > 0; j--)
positions_backup[j] = positions_backup[j - 1];
end
positions_backup[0].x = last_position.x + get_distx(fget_angle(last_position.x, last_position.y, x, y), i);
positions_backup[0].y = last_position.y + get_disty(fget_angle(last_position.x, last_position.y, x, y), i);
end
end

frame;
move_draw(graph, x, y);
last_position.x = x;
last_position.y = y;
end
end


process tail(int index, point* positions, int diameter)
begin
graph = draw_circle(x, y, diameter / 2);
loop
x = positions[index * diameter].x;
y = positions[index * diameter].y;
move_draw(graph, x, y);
frame;
end
end
My game developer instagram :
https://www.instagram.com/ben_dev_game/