インベーダーゲーム作成のアドバイス
Posted: 2014年5月01日(木) 15:03
現在コマンドラインで動く「インベーダーゲーム」を制作しているのですが、弾の軌道を関数で定義するとどうしても上手くいきません。
特に、弾を発射時の描画と移動定義が現在のところうまくいきません。下記がソースプログラムです。
「インベーダーゲーム」を作る上で改善したほうが良い場所もあればコメントくださいお願いします
特に、弾を発射時の描画と移動定義が現在のところうまくいきません。下記がソースプログラムです。
「インベーダーゲーム」を作る上で改善したほうが良い場所もあればコメントくださいお願いします
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#define MAP 10
#define T_MAX 3
#define ENEMY 3
int chx;
int chy;
struct tamadeta { /* 構造体の宣言 */
int x;
int y;
int flag;
};
struct tamadeta tama[T_MAX];
int rdm;
int stg[MAP][MAP]={0};
int i,j;
char st;
void init();
void display();
void Key();
int move(int,int,int);
int enmove();
int wall(int,int,int);
int main(void){
int time=0;
init();
while(1){
if(kbhit())Key();//キーボード入力
if(time<5000)time++;//タイマー
else {
for(i=0;i<T_MAX;i++){
move(tama[i].x,tama[i].y,tama[i].flag);
wall(tama[i].x,tama[i].y,tama[i].flag);;
}
display();
time=0;
}
if(st==' ')break;
}
return 0;
}
void Key(){
st=getch();
switch (st){
case 'K'://←
if(stg[chx][chy-1] !=1){
stg[chx][chy]=0;
chy--;
}
break;
case 'H'://↑
if(stg[chx-1][chy] !=1){
stg[chx][chy]=0;
chx--;
}
break;
case 'M'://→
if(stg[chx][chy+1] !=1){
stg[chx][chy]=0;
chy++;
}
break;
case 'P'://↓
if(stg[chx+1][chy] !=1){
stg[chx][chy]=0;
chx++;
}
break;
case 'z':
for(i=0;i<T_MAX;i++){
if(tama[i].flag == 0 ){
tama[i].x=chx-1;
tama[i].y=chy;
tama[i].flag=1;
}
}
stg[chx-1][chy]=3;
break;
}
stg[chx][chy]=2;
}
void init(){
chx= MAP-2;
chy=((MAP/2)+(MAP%2));
for(i=0;i<T_MAX;i++)tama[i].flag = 0;
for(i=0;i<MAP;i++){
for(j=0;j<MAP;j++){
if(i==0 || i==MAP-1 || j==0 || j==MAP-1)stg[i][j]=1;
else stg[i][j]=0;
}
}
stg[chx][chy]=2;
}
void display(){
system("cls");
for(i=0;i<MAP;i++){
for(j=0;j<MAP;j++){
if (stg[i][j]==0)printf(" ");
else if(stg[i][j]==1)printf("■");
else if(stg[i][j]==2)printf("凸");
else if(stg[i][j]==3)printf("||");
else printf("G");
}
printf("\n");
}
}
int move(int x,int y,int flag){
if(flag==1){
x--;
if(stg[x+1][y]==3)stg[x+1][y]=0;
stg[x][y]=3;
}
return x,y,flag;
}
int wall(int x,int y,int flag){
if(stg[x-1][y]==1){
flag=0;
stg[x][y]=0;
}
return 0;
}
int enemove(int x,int y,int flag){
if(stg[x+1][y]==1){
flag=0;
stg[x][y]=0;
}
if(flag==1){
x++;
if(stg[x-1][y]==4){
stg[x-1][y]=0;
}
stg[x][y]=4;
}
return x,y,flag;