/*広告*/
/*STGの作り方 中間ソース6*/
#include "DxLib.h" #define LEFT_LIMIT -220 #define RIGHT_LIMIT 220 #define TOP_LIMIT 240 #define BOTTOM_LIMIT -240 #define PLAYER_SHOT_MAX 30 #define DISP_ENEMY_MAX 5 #define STAGE_ENEMY_MAX 10 #define ENEMY_DATA_MAX 9 #define ENEMY_SHOT_MAX 20 void my_init_back_img(void); void my_init_variable(void); void my_init_player(void); void my_init_player_shot(void); void my_init_enemy(void); void my_init_enemy_shot(void); void my_set_player_shot(int ); void my_set_enemy(void); void my_set_enemy_shot(double,double,int,int); void my_move_back(void); void my_move_player(void); void my_move_player_shot(void); void my_move_enemy(void); void my_move_enemy_shot(void); void my_to_center(void); void my_draw_back(void); void my_draw_player(void); void my_draw_player_shot(void); void my_draw_enemy(void); void my_draw_enemy_shot(void); void my_draw_score_board(void); void my_timer(void); int my_get_key(void); struct PLAYER{ double x, y; double draw_x, draw_y; int speed; int shot_type; int bomb; int power; int life; double range; }; struct PLAYER p1; struct PLAYER_SHOT{ double x[5]; double y[5]; double draw_x[5]; double draw_y[5]; int init_flag; int move_flag; int move_type; int flag[5]; double angle[5]; int max_bullet; int gamecount_point; double range; }; struct PLAYER_SHOT ps1[PLAYER_SHOT_MAX]; struct ENEMY{ int serial_num; int appear_point; double first_x, first_y; double x, y; double draw_x, draw_y; double m_x,m_y; double a_x,a_y; double f_x,f_y; int init_flag; int move_flag; double angle; int move_type; int shot_type; int pattern; double var[5]; int gamecount_point; int power; double range; int item_flag; }; struct ENEMY enemy[DISP_ENEMY_MAX]; struct ENEMY_SHOT{ double first_x; double first_y; double x[10]; double y[10]; double draw_x[10]; double draw_y[10]; double angle[10]; int init_flag; int move_flag; int move_type; int max_bullet; int gamecount_point[5]; double range; int enemy_num; }; struct ENEMY_SHOT enemy_shot[ENEMY_SHOT_MAX]; int ary_enemy_data[STAGE_ENEMY_MAX * ENEMY_DATA_MAX] = { 0,100,-180,260,0,0,1,10,0, 1,300,-140,260,0,0,1,10,0, 2,500,-100,260,0,0,1,10,0, 3,700,-60,260,0,0,1,10,0, 4,900,-20,260,0,0,1,10,0, 5,1100,20,260,0,0,1,10,0, 6,1300,60,260,0,0,1,10,0, 7,1500,100,260,0,0,1,10,0, 8,1700,140,260,0,0,1,10,0, 9,1900,180,260,0,0,1,10,0 }; int key[256]; double back_img_y; int back_img; int gamecount; int Color_White; int Color_Gray; int Color_Red; int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int){ ChangeWindowMode(TRUE); DxLib_Init(); SetDrawScreen(DX_SCREEN_BACK); my_init_back_img(); my_init_variable(); my_init_player(); my_init_player_shot(); my_init_enemy(); my_init_enemy_shot(); while (ScreenFlip()==0 && ProcessMessage()==0 && ClearDrawScreen()==0 && my_get_key()==0){ my_set_enemy(); my_move_back(); my_move_player(); my_move_player_shot(); my_move_enemy(); my_move_enemy_shot(); my_to_center(); my_draw_back(); my_draw_player(); my_draw_player_shot(); my_draw_enemy(); my_draw_enemy_shot(); my_draw_score_board(); my_timer(); } DxLib_End(); return 0; } void my_init_back_img(){ back_img = LoadGraph("../back_img.jpg"); } void my_init_variable(){ gamecount = 0; Color_White = GetColor(255, 255, 255); Color_Gray = GetColor(100, 100, 100); Color_Red = GetColor(255, 0, 0); back_img_y = 0; } void my_init_player(){ p1.x = 0; p1.y = -200; p1.draw_x = 0; p1.draw_y = 0; p1.speed = 4; p1.shot_type = 0; p1.bomb = 1; p1.power = 1; p1.life = 3; p1.range = 10; } void my_init_player_shot(){ for(int i = 0;i < PLAYER_SHOT_MAX;i++){ ps1[i].init_flag = 0; ps1[i].move_flag = 0; ps1[i].move_type = 0; ps1[i].max_bullet = 0; ps1[i].gamecount_point = 0; ps1[i].range = 10; for(int j = 0;j < 5;j++){ ps1[i].x[j] = 0; ps1[i].y[j] = 0; ps1[i].draw_x[j] = 0; ps1[i].draw_y[j] = 0; ps1[i].flag[j] = 0; ps1[i].angle[j] = 0; } } } void my_init_enemy(){ for (int i = 0; i < DISP_ENEMY_MAX; i++){ enemy[i].serial_num = 0; enemy[i].appear_point = 0; enemy[i].first_x = 0; enemy[i].first_y = 0; enemy[i].x = 0; enemy[i].y = 0; enemy[i].draw_x = 0; enemy[i].draw_y = 0; enemy[i].m_x = 0; enemy[i].m_y = 0; enemy[i].a_x = 0; enemy[i].a_y = 0; enemy[i].f_x = 0; enemy[i].f_y = 0; enemy[i].init_flag = 0; enemy[i].move_flag = 0; enemy[i].angle = 0; enemy[i].move_type = 0; enemy[i].shot_type = 0; enemy[i].pattern = 0; enemy[i].gamecount_point = 0; enemy[i].power = 0; enemy[i].range = 0; enemy[i].item_flag = 0; for(int j = 0;j < 5;j++){ enemy[i].var[j] = 0; } } } void my_init_enemy_shot(){ for (int i = 0; i < ENEMY_SHOT_MAX; i++){ enemy_shot[i].first_x = 0; enemy_shot[i].first_y = 0; enemy_shot[i].init_flag = 0; enemy_shot[i].move_flag = 0; enemy_shot[i].move_type = 0; enemy_shot[i].max_bullet = 0; enemy_shot[i].range = 10; enemy_shot[i].enemy_num = 0; for(int j = 0;j < 10;j++){ enemy_shot[i].x[j] = 0; enemy_shot[i].y[j] = 0; enemy_shot[i].draw_x[j] = 0; enemy_shot[i].draw_y[j] = 0; enemy_shot[i].angle[j] = 0; } for(int j = 0;j < 5;j++){ enemy_shot[i].gamecount_point[j] = 0; } } } void my_move_back(){ back_img_y++; if (back_img_y > 480){ back_img_y = 0; } } void my_move_player(){ if (key[KEY_INPUT_RIGHT] >= 1 && p1.x < RIGHT_LIMIT){ p1.x += p1.speed; } if (key[KEY_INPUT_DOWN] >= 1 && p1.y > BOTTOM_LIMIT){ p1.y -= p1.speed; } if (key[KEY_INPUT_LEFT] >= 1 && p1.x > LEFT_LIMIT){ p1.x -= p1.speed; } if (key[KEY_INPUT_UP] >= 1 && p1.y < TOP_LIMIT){ p1.y += p1.speed; } if (key[KEY_INPUT_Z] % 5 == 1){ my_set_player_shot(p1.shot_type); } } void my_set_player_shot(int shot_type){ for(int i = 0;i < PLAYER_SHOT_MAX;i++){ if(ps1[i].move_flag == 0){ ps1[i].move_type = shot_type; ps1[i].move_flag = 1; break; } } } void my_move_player_shot(){ for(int i = 0;i < PLAYER_SHOT_MAX;i++){ if(ps1[i].move_flag == 1){ switch(ps1[i].move_type){ case 0: if(ps1[i].init_flag == 0){ ps1[i].max_bullet = 1; for(int j = 0;j < ps1[i].max_bullet;j++){ ps1[i].x[j] = p1.x; ps1[i].y[j] = p1.y; ps1[i].flag[j] = 1; } ps1[i].gamecount_point = gamecount; ps1[i].init_flag = 1; } else{ if(gamecount < ps1[i].gamecount_point + 100){ for(int j = 0;j < ps1[i].max_bullet;j++){ ps1[i].y[j] += 8; } } else{ ps1[i].move_flag = 0; ps1[i].init_flag = 0; } } break; default: break; } } } } void my_set_enemy(){ for (int i = 0; i < STAGE_ENEMY_MAX; i++){ if (ary_enemy_data[(i * ENEMY_DATA_MAX) + 1] == gamecount && gamecount > 0){ for (int j = 0; j < DISP_ENEMY_MAX; j++){ if (enemy[j].move_flag == 0){ enemy[j].move_flag = 1; enemy[j].serial_num = ary_enemy_data[i * ENEMY_DATA_MAX]; enemy[j].appear_point = ary_enemy_data[(i * ENEMY_DATA_MAX) + 1]; enemy[j].first_x = ary_enemy_data[(i * ENEMY_DATA_MAX) + 2]; enemy[j].first_y = ary_enemy_data[(i * ENEMY_DATA_MAX) + 3]; enemy[j].move_type = ary_enemy_data[(i * ENEMY_DATA_MAX) + 4]; enemy[j].shot_type = ary_enemy_data[(i * ENEMY_DATA_MAX) + 5]; enemy[j].power = ary_enemy_data[(i * ENEMY_DATA_MAX) + 6]; enemy[j].range = ary_enemy_data[(i * ENEMY_DATA_MAX) + 7]; enemy[j].item_flag = ary_enemy_data[(i * ENEMY_DATA_MAX) + 8]; break; } } } } } void my_move_enemy(){ for (int i = 0; i < DISP_ENEMY_MAX; i++){ if (enemy[i].move_flag == 1){ switch (enemy[i].move_type){ case 0: if (enemy[i].init_flag == 0){ enemy[i].x = enemy[i].first_x; enemy[i].y = 260; enemy[i].gamecount_point = gamecount; enemy[i].init_flag = 1; } else{ if (gamecount < enemy[i].gamecount_point + 500){ if (gamecount < enemy[i].gamecount_point + 220){ enemy[i].y--; } if (gamecount == enemy[i].gamecount_point + 240){ my_set_enemy_shot(enemy[i].x,enemy[i].y,enemy[i].shot_type,i); } if (gamecount > enemy[i].gamecount_point + 260){ enemy[i].y++; } } else{ enemy[i].move_flag = 0; enemy[i].init_flag = 0; } } break; default: break; } } } } void my_set_enemy_shot(double x,double y,int shot_type,int enemy_num){ for(int i = 0;i < ENEMY_SHOT_MAX;i++){ if(enemy_shot[i].move_flag == 0){ enemy_shot[i].move_flag = 1; enemy_shot[i].first_x = x; enemy_shot[i].first_y = y; enemy_shot[i].move_type = shot_type; enemy_shot[i].enemy_num = enemy_num; break; } } } void my_move_enemy_shot(){ for(int i = 0;i < ENEMY_SHOT_MAX;i++){ if(enemy_shot[i].move_flag == 1){ switch(enemy_shot[i].move_type){ case 0: if (enemy_shot[i].init_flag == 0){ enemy_shot[i].x[0] = enemy_shot[i].first_x; enemy_shot[i].y[0] = enemy_shot[i].first_y; enemy_shot[i].max_bullet = 1; enemy_shot[i].gamecount_point[0] = gamecount; enemy_shot[i].init_flag = 1; } else{ if (gamecount < enemy_shot[i].gamecount_point[0] + 300){ enemy_shot[i].y[0] -= 2; } else{ enemy_shot[i].move_flag = 0; enemy_shot[i].init_flag = 0; } } break; default: break; } } } } void my_to_center(){ p1.draw_x = p1.x + 220; p1.draw_y = ((-1) * p1.y) + 240; for(int i = 0;i < PLAYER_SHOT_MAX;i++){ if(ps1[i].move_flag == 1 && ps1[i].init_flag == 1){ for(int j = 0;j < ps1[i].max_bullet;j++){ ps1[i].draw_x[j] = ps1[i].x[j] + 220; ps1[i].draw_y[j] = ((-1) * ps1[i].y[j]) + 240; } } } for (int i = 0; i < DISP_ENEMY_MAX; i++){ if (enemy[i].move_flag == 1 && enemy[i].init_flag == 1){ enemy[i].draw_x = enemy[i].x + 220; enemy[i].draw_y = ((-1) * enemy[i].y) + 240; } } for(int i = 0;i < ENEMY_SHOT_MAX;i++){ if(enemy_shot[i].move_flag == 1 && enemy_shot[i].init_flag == 1){ for(int j = 0;j < enemy_shot[i].max_bullet;j++){ enemy_shot[i].draw_x[j] = enemy_shot[i].x[j] + 220; enemy_shot[i].draw_y[j] = ((-1) * enemy_shot[i].y[j]) + 240; } } } } void my_draw_back(){ DrawGraph(0, back_img_y, back_img, TRUE); DrawGraph(0, back_img_y - 480, back_img, TRUE); DrawLine(0, 240, 440, 240, Color_White); DrawLine(220, 0, 220, 480, Color_White); DrawFormatString(220, 240, Color_White, "(0,0)"); } void my_draw_player(){ DrawFormatString(p1.draw_x, p1.draw_y, Color_White, "P"); } void my_draw_player_shot(){ for(int i = 0;i < PLAYER_SHOT_MAX;i++){ if(ps1[i].move_flag == 1 && ps1[i].init_flag == 1){ for(int j = 0;j < ps1[i].max_bullet;j++){ if(ps1[i].flag[j] == 1){ DrawFormatString(ps1[i].draw_x[j],ps1[i].draw_y[j],Color_White, "弾"); } } } } } void my_draw_enemy(){ for (int i = 0; i < DISP_ENEMY_MAX; i++){ if (enemy[i].move_flag == 1 && enemy[i].init_flag == 1){ DrawFormatString(enemy[i].draw_x, enemy[i].draw_y, Color_Red, "e[%d]", enemy[i].serial_num); } } } void my_draw_enemy_shot(){ for(int i = 0;i < ENEMY_SHOT_MAX;i++){ if(enemy_shot[i].move_flag == 1 && enemy_shot[i].init_flag == 1){ for(int j = 0;j < enemy_shot[i].max_bullet;j++){ DrawFormatString(enemy_shot[i].draw_x[j],enemy_shot[i].draw_y[j],Color_Red, "弾"); } } } } void my_draw_score_board(){ DrawBox(440,0,640,480,Color_Gray,true); DrawFormatString(460, 20, Color_White, "gamecount = %d", gamecount); DrawFormatString(460, 380, Color_White, "操作方法"); DrawFormatString(460, 400, Color_White, "移動:矢印キー"); DrawFormatString(460, 420, Color_White, "ショット:Zキー"); } void my_timer(){ gamecount++; } int my_get_key(){ char keys[256]; GetHitKeyStateAll(keys); for (int i = 0; i < 256; i++){ if (keys[i] != 0){ key[i]++; } else{ key[i] = 0; } } return 0; }/*Copyright 2016 K.N/petitetech.com*/
/*広告*/