tron

le jeu des motos dans tron - retour accueil

git clone git://bebou.netlib.re/tron

Log | Files | Refs | README |

commit 9e2c6509dcdb355ee3f6be705f2bae94b4b37142
parent 1cb0560496574b366e6c51a8c51914a74abc4380
Auteurice: Arthur Pons <arthur.pons@unistra.fr>
Date:   Thu, 19 Jun 2025 12:01:03 +0200

On termine si y'a collision

Diffstat:
Mtron.c | 30+++++++++++++++++++++++++++---
1 file changed, 27 insertions(+), 3 deletions(-)

diff --git a/tron.c b/tron.c @@ -12,22 +12,38 @@ struct car { coord position; coord* path; int direction; + int previous_direction; }; void display(struct car *cars, int ticks) { for (int i=0;i<2;i++) { - tb_printf(cars[i].path[ticks].x,cars[i].path[ticks].y,0,0,"-"); + if (cars[i].direction==cars[i].previous_direction && cars[i].direction%2) + tb_printf(cars[i].path[ticks-1].x,cars[i].path[ticks-1].y,0,0,"─"); + else if(cars[i].direction==cars[i].previous_direction) + tb_printf(cars[i].path[ticks-1].x,cars[i].path[ticks-1].y,0,0,"│"); tb_printf(cars[i].position.x,cars[i].position.y,0,0,"$"); tb_printf(0,i,0,0,"car %d - x: %d, y: %d, direction %d",i,cars[i].position.x,cars[i].position.y,cars[i].direction); } } void update(struct car *car, coord *incs, int ticks) { + car->previous_direction=car->direction; car->path[ticks]=car->position; car->position.x=car->position.x+incs[car->direction].x; car->position.y=car->position.y+incs[car->direction].y; } +int is_in_conflict(struct car *cars, int ticks) { + for (int i=0;i<2;i++) { + for (int j=0;j<ticks;j++) { + if (cars[i].position.x==cars[i].path[j].x && + cars[i].position.y==cars[i].path[j].y) + return 1; + } + } + return 0; +} + int main(int argc, char **argv) { int ticknb=0; @@ -40,23 +56,30 @@ int main(int argc, char **argv) { struct car c1; c1.position.x=10; c1.position.y=10; c1.path=malloc(sizeof(coord)*300); c1.direction=DOWN; + c1.previous_direction=c1.direction; struct car c2; c2.position.x=5; c2.position.y=5; c2.path=malloc(sizeof(coord)*300); c2.direction=UP; + c2.previous_direction=c2.direction; struct car cars[2]; cars[0]=c1; cars[1]=c2; + int dead_car; + tb_init(); tb_hide_cursor(); tb_set_input_mode(TB_INPUT_ESC | TB_INPUT_MOUSE); struct tb_event ev; while(1) { - tb_printf(0,3,0,0,"ticknb : %d",ticknb++); for(int i=0;i<2;i++) update(&cars[i],incs,ticknb); + if(is_in_conflict(cars,ticknb)) { + tb_shutdown(); + return 0; + } display(cars,ticknb); tb_present(); - tb_peek_event(&ev, 100); + tb_peek_event(&ev, 1000); switch(ev.ch) { case 97: /*a*/ break; @@ -84,6 +107,7 @@ int main(int argc, char **argv) { default: break; } + tb_printf(0,3,0,0,"ticknb : %d",ticknb++); } return 0; }