sdl - C Space Invaders enemy movement -
i have write clone of space invaders game university in c language using sdl library graphics.i new c , programming in general struggle lot. have 1 row of enemies , i'm trying make move correctly. functions movement of aliens, check collision right/left wall(which sdl_rects near window edges) , if happens, enemies move 1 line lower in opposite direction. problem works okay ten enemies except first one. each time when collision left wall occurs first alien sort of moves away bit others instead of moving in 1 block want to. noticed if change first loop in move_aliens function start i=11 , i--, same thing happen enemy in last column. still dont know how fix it. appreciate if tell me i'm doing wrong, give me idea or sollution :). uploaded video of whats happening http://sendvid.com/dt1reizc
void move_down (gamestate *game) { int i=0; for(; < hmaliens; i++) game->alien1[i].y += 25; } int collided(int a1, int b1, int a2, int b2, int width1, int height1, int width2, int height2) { return (!((a1 > (a2+width2)) || (a2 > (a1+width1)) || (b1 > (b2+height2)) || (b2 > (b1+height1)))); } void move_aliens(gamestate *game) { int i=0; for(; < hmaliens; i++) { if (game->alien1[i].dir==left) game->alien1[i].x -= 10; if (game->alien1[i].dir==right) game->alien1[i].x += 10; if (collided(game->alien1[i].x, game->alien1[i].y, game->leftwall.x, game->leftwall.y, 50, 50, 1, 600)) { int = 0; for(; < hmaliens; i++) game->alien1[i].dir=right; move_down(game); } } if(collided(game->alien1[i].x, game->alien1[i].y, game->rightwall.x, game->rightwall.y, 50, 50, 1, 600)) { int = 0; for(; < hmaliens; i++) game->alien1[i].dir=left; move_down(game); } } } //edit hmaliens constant (11), number of living enemies @ start gamestate structure. left/right stand direction of movement (quite obvious) [enum direction {left, right};]. have in alien1 structure enum direction dir , in function load_game function set right.
typedef struct { player player; rightwall rightwall; leftwall leftwall; alien1 alien1[hmaliens]; sdl_texture *bulet; sdl_texture *ship; sdl_texture *alien1; sdl_renderer *renderer; } gamestate;
maybe like:
void move_aliens(gamestate *game) { int i=0; for(; < hmaliens; i++) { if (game->alien1[i].dir==left) game->alien1[i].x -= 10; if (game->alien1[i].dir==right) game->alien1[i].x += 10; } if (collided( game->alien1[0].x, game->alien1[0].y, game->leftwall.x, game->leftwall.y, 50, 50, 1, 600)) { int = 0; for(; < hmaliens; i++) game->alien1[i].dir=right; move_down(game); } if(collided( game->alien1[hmaliens-1].x, game->alien1[hmaliens-1].y, game->rightwall.x, game->rightwall.y, 50, 50, 1, 600)) { int = 0; for(; < hmaliens; i++) game->alien1[i].dir=left; move_down(game); } } i have tried make minimum changes. @milevyo's more extensive rewrite looks well.
the problem, think (just looking), has left collision test inside loop when hits, movement gets out of sync.
also pretty subtle right collision test used i previous loop happens index last element (the rightmost alien). changed explicitly use hmaliens - 1. when start destroying aliens, you'll have track first (leftmost) , last (rightmost) living aliens , use them collision tests.
your indentation , formatting little off makes code harder read. formatting important, , more when code gets more complicated.
Comments
Post a Comment