パックマンのゲームプログラム

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
とある高専の生徒

パックマンのゲームプログラム

#1

投稿記事 by とある高専の生徒 » 7年前

パックマンのプログラムを作りたいのですが、壁を表示するところからうまくいきません。
どうすればいいですか。
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdlib.h> // system()
#include <windows.h> // Sleep()
#include <conio.h> // kbhit()

#define SIZE 15 // 一辺の長さ
#define EMPTY 0 // 何もない場所は0
#define WALL 1 // 壁
#define FOOD 2 // えさ(ドット)
#define PACMAN 3// 主人公

int main()
{
int wait_time = 300;
int x, y;
int food_count = 0;//エサの個数
int cx, cy;//パックマン
int key;
int point = 0;
int object;
// 最も重要なデータ 1 は壁、えさは2、空白は 0
// field[y][x]となるので注意!
int field[SIZE][SIZE] =
{
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },
{ 1,2,2,2,2,2,2,2,2,2,2,2,2,2,1 },
{ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 },

// 自分で配列を完成させよう!
};

//まずはゲーム開始前の処理
// パックマンを初期位置に配置
cx = 4, cy = 4; //好きな場所に配置する!

//エサの個数を数える
for (y = 0; y < SIZE; y++)
{
for (x = 0; x < SIZE; x++)
{
if (field[y][x] == FOOD)
food_count++;
}
}

while (1)
{
// まずはPACMANの動き
// キーが押されていたら、キーコードを取得する
// 4なら左、6なら右、8なら上、2なら下
if (kbhit()) // キーが入力されていれば真
{
key = getch();//一文字入力、エコーバックなし
switch (key)
{
case '4':
cx--;
break;
case '6':
cx++;
break;
case '2':
cy++;
break;
case '8':
cy--;
break;
}
cx = (cx + SIZE) % SIZE; //ワープ

switch (field[cy][cx])
{
case WALL://壁
//動けないので cx, cy を元に戻す
break;
case FOOD:
//得点加算
break;
}



}
// ここまでPACMANの動き

// 画面表示
for (x = 0; x < SIZE; x++) //x方向
{
for (y = 0; y < SIZE; y++) //x方向
{
if (field[y][x] == 1)
printf("■");
else if (field[y][x] == 2)
printf("*");
else if (field[y][x] == 0)
printf("");
}
printf("\n");
}
printf("\n");
}
// すべての場所で field[y][x] の値を元に表示
// shooting のコードを参考にしよう!(コピー可)

printf("\n");
printf(" (cx,cy) = (%d, %d)\n", cx, cy);
printf(" point: %d\n", point);
if (food_count == 0)
{
printf("GAME CLEAR\n");

}
Sleep(wait_time);// 1秒間(wait_timeミリ秒)何もしない
system("cls");// 画面消去
return 0;
}

“C言語何でも質問掲示板” へ戻る