障害物をよける(自機の背景が黒いとよかったかも)
shots[index].x =ship->x/*船のいるところから弾が来る*/;
shots[index].y = 0;
shots[index].xx =0 ;
shots[index].yy = +1;
shots[index].active = TRUE;
/* 弾の移動 */ void move_shots(shot_info_t *shots, int max_size, const ship_info_t *ship) { int i; for (i = 0; i < max_size; ++i) { // !!! 弾の移動ルーチンに基づいた処理 // !!! int x, y; x = shots[i].x + shots[i].xx; y = shots[i].y + shots[i].yy; printf("x=%d y=%d sx=%d sy=%d\n",x,y,ship->x,ship->y); /*当たり判定*/ if(abs(x - ship->x) <=3 && abs(ship->y - y)<=3 ){ printf("gameover\n"); exit(0); } if ((x < 0) || (x > WIN_W-1)) { shots[i].active = FALSE; } if ((y < 0) || (y > WIN_H-1)) { shots[i].active = FALSE; } } }
/* 弾情報の初期化 */ void init_shots(shot_info_t* shots, int max_size) { int i; for (i = 0; i < max_size; ++i) { shots[i].active = FALSE; } }
... x=-1208399967 y=-1210973109 sx=0 sy=35 x=0 y=32 sx=0 sy=35 gameove
for (i = 0; i < max_size; ++i) { if (shots[i].active == FALSE) { continue; } ...
/* 自機の移動 */ get_input(&input); move_ship(&ship, &input); /* 球の移動 */ create_shot(shots, MAX_SHOTS, &ship); move_shots(shots, MAX_SHOTS, &ship); /* 画面の更新 */ now = get_ticks(); if ((now - prev) > FPS) { delay((now - prev) - FPS); }
弾が撃てる
/* 入力イベントの管理用 */ typedef struct { int quit; /* 終了イベント */ int is_left; /* 左移動の指令中 */ int is_right; /* 右移動の指令中 */ int is_up; /* 上移動の指令中 */ int is_down; /* 下移動の指令中 */ int is_shot; // ショット } input_t;
/* 自機の情報 */ typedef struct { int x; //自機の座標 int y; int xx; //移動量 int yy; } ship_info_t;
/* 自機の移動 */ move_ship(&ship, &input); /* 球の生成 */ create_shot(shots, MAX_SHOTS, &ship, &input); /* 球の移動 */ move_shots(shots, MAX_SHOTS, &ship);
/* 弾の生成 */ void create_shot(shot_info_t *shots, int max_size, const ship_info_t *ship, const input_t *input) { static int cycle = 1000; int index; int i; if (++cycle < ShotCreateCycle || input->is_shot!=1) { return; } cycle = 0;
/* 弾の生成 */ void create_shot(shot_info_t *shots, int max_size, const ship_info_t *ship, const input_t *input) { static int cycle = 1000; /* 開始直後は弾を打てない */ int index; int i; if ((++cycle < ShotCreateCycle) || (input->is_shot == FALSE)) { return; }
if (! input->is_shot) { return; }
降ってくる弾を避ける
% make && valgrind ./shooting make: `all' に対して行うべき事はありません。 ==3127== Memcheck, a memory error detector. ==3127== Copyright (C) 2002-2006, and GNU GPL'd, by Julian Seward et al. ==3127== Using LibVEX rev 1606, a library for dynamic binary translation. ==3127== Copyright (C) 2004-2006, and GNU GPL'd, by OpenWorks LLP. ==3127== Using valgrind-3.2.0, a dynamic binary instrumentation framework. ==3127== Copyright (C) 2000-2006, and GNU GPL'd, by Julian Seward et al. ==3127== For more details, rerun with: -v ==3127== --3127-- DWARF2 CFI reader: unhandled CFI instruction 0:50 --3127-- DWARF2 CFI reader: unhandled CFI instruction 0:50 --3127-- DWARF2 CFI reader: unhandled CFI instruction 0:50 --3127-- DWARF2 CFI reader: unhandled CFI instruction 0:50 ==3127== Conditional jump or move depends on uninitialised value(s) ==3127== at 0x80491BB: move_shots (shots_ctrl.c:92) ==3127== by 0x804899A: main (shooting.c:53) ==3127== ==3127== Conditional jump or move depends on uninitialised value(s) ==3127== at 0x80491C1: move_shots (shots_ctrl.c:92) %
int x, y; x = shots[i].x + shots[i].xx; y = shots[i].y + shots[i].yy; if ((x < 0) || (x > WIN_W-1)) { shots[i].active = FALSE; }