|
5啊哈币
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <windows.h>
//To-do list:
// 1. Develop the function that player key pressed respond result:done
// 2. Develop the core code of the game result:done
// 2.1 Solve the growth of the snake problem; result:done
// 2.2 Solve the the snake hit the wall or eat itself,quit the game problme result:done
// 3. Develop the user interface for the game
// 3.1 Entrance interface result:done
// 3.2 Introduction interface result:done
// 3.3 Exit interface result:done
// 3.4 Pause interface
// 4. Free the map variable when the game is over. result:done;
// 5. Study the Sleep function to pause the runing thread.
struct {
int Pause;
int GameStarted;
int GameOver;
int Difficulty;
int ScreenWidth, ScreenHeight;
int **map;
}game;
struct{
int sLong;
char head[4];
int *CoordX;
int *CoordY;
int moveX;
int moveY;
int direction;
}snake;
struct{
int fCoordX;
int fCoordY;
int reput;
}food;
struct{
int keypressed;
int choice;
int quit;
int menuBack;
int leave;
}player;
void GameEnvironmentSetting();
void SnakeInfoSetting();
void FoodInfoSetting();
void PlayerInfoSetting();
void LoadMainEntrance();
void MStartGame();
void SetDifficulty();
void MExitGame();
void GReStartGame();
void LoadPauseStatusEntrance();
int main(){
GameEnvironmentSetting();
SnakeInfoSetting();
FoodInfoSetting();
MStartGame();
// while(!player.leave){
// LoadMainEntrance();
// GainPlayerInput();
// };
system("pause");
return 0;
}
void GainPlayerInput(){
void (*menuArray[])() ={MStartGame, SetDifficulty, MExitGame};
char menu[3][20] = {"StartGame","SetDifficulty","ExitGame"};
PlayerMakesChoice(menu, menuArray);
}
void PlayerMakesChoice(char menu[][20],void(*menuArray[3])()){
int i,count, menuCount;
char point[4] = "--->";
menuCount = sizeof(menu);
count = 0;
while(!player.menuBack){
for(i=0;i<menuCount;i++){
if (i==count) printf("%s", point);
else printf(" ");
printf("%-5s\n", menu[i]);
}
player.keypressed = getch();
if(player.keypressed == 0xe0) player.keypressed = getch();
switch(player.keypressed){
case 72: if(count>0) count-=1; break;
case 80: if(count<menuCount-2) count+=1; break;
case 13: player.choice = 1;
}
if(player.choice){
Sleep(100);
menuArray[count]();
player.choice = 0;
}
}
}
void LoadMainEntrance(){
printf("\n");
printf(" @@ @@ \n");
printf(" @@ @@ @@ \n");
printf(" @@@@ @@ @@ \n");
printf(" @@ @@ @@@@ @@@@@@@@ @@@@@@@@@@@@@@@ \n");
printf(" @@ @@@@@@@@@@ @@@@@@ @@ @@ @@ \n");
printf(" @@@@ @@ @@ @@ @@@@ \n");
printf(" @@ @@ @@ @@@@@@@@@@@@@@ \n");
printf(" @@@@@@@@@@@@@@@@@@ @@ @@ @@ \n");
printf(" @@ @@ @@ @@ @@ \n");
printf(" @@ @@ @@ @@ @@ @@ \n");
printf(" @@ @@ @@ @@@@@@@@ @@ \n");
printf(" @@ @@ @@ @@ @@ @@ \n");
printf(" @@ @@@@ @@ @@\n");
printf(" @@ @@@@ @@ @@\n");
printf(" @@@@ @@ @@@@@@@@@@@@@@ \n\n\n\n");
printf("Use the Arrow key to Control The Direction\n");
}
void SetDifficulty(){
printf("Press Up or Down Arrow to choose Difficulty\n");
printf("Press Esc to go back\n");
int back = 0;
int LastDifficulty = game.Difficulty;
int adjust = 0.5;
while(!back){
player.keypressed = getch();
if(player.keypressed == 0xe0) player.keypressed = getch();
switch(player.keypressed){
case 72: game.Difficulty=5 ; if(LastDifficulty+adjust <5) game.Difficulty = LastDifficulty+adjust; break;
case 80: game.Difficulty=0.5; if(LastDifficulty-adjust >0.5) game.Difficulty=LastDifficulty-adjust; break;
case 27: back = 1; break;
}
}
}
void LoadPauseStatusEntrance(){
void (*menuArray[3]) ={GReStartGame, SetDifficulty, MExitGame};
char menu[3][20] = {"StartGame","SetDifficulty","ExitGame"};
PlayerMakesChoice(menu, menuArray);
}
void GReStartGame(){
snake.sLong = 3;
food.reput = 1;
GNewSnakeBirth(snake.sLong, 2 + rand() % (game.ScreenWidth-4), 2 + rand() % (game.ScreenHeight-4));
GPutFood();
MStartGame();
}
void GameEnvironmentSetting(){
int row,col;
game.GameStarted = 1;
game.GameOver = 0;
game.ScreenWidth = 20;
game.ScreenHeight = 20;
game.Difficulty = 1.5;
system("mode con cols=61 lines=35");
system("color f0");
game.map = (int **)malloc(sizeof(int)*game.ScreenWidth);
for(row=0;row<game.ScreenWidth;row++){
game.map[row]=(int*)malloc(sizeof(int)*game.ScreenHeight);
};
GInitiateMap();
}
void GInitiateMap(){
int row, col;
for(row=0;row<game.ScreenHeight;row++){
for(col=0;col<game.ScreenWidth;col++){
game.map[row][col]=0;
if(row==0 || row==game.ScreenHeight-1 || col==0 || col==game.ScreenWidth-1){
game.map[row][col]=1;
}
}
}
}
void SnakeInfoSetting(){
int i;
snake.sLong = 3;
char temp[4] = {'^','V','<','>'};
memcpy(snake.head,temp, 4*sizeof(char));
srand((unsigned)time(NULL));
snake.CoordX = (int*)malloc(sizeof(int)*game.ScreenWidth);
snake.CoordY = (int*)malloc(sizeof(int)*game.ScreenHeight);
snake.direction = rand() % 3;
switch(snake.direction){
case 0: snake.moveX = 1; snake.moveY = 0; break; //create snake right
case 1: snake.moveX =-1; snake.moveY = 0; break; //create snake up
case 2: snake.moveX = 0; snake.moveY = 1; break; //create snake left
case 3: snake.moveX = 0; snake.moveY =-1; break; //create snake down
}
GNewSnakeBirth(snake.sLong, 2 + rand() % (game.ScreenWidth-4), 2 + rand() % (game.ScreenHeight-4));
}
void GNewSnakeBirth(int sLong, int headx,int heady){
int i;
for(i=0;i<snake.sLong;i++){
if(i==0){
snake.CoordX[i] = headx;
snake.CoordY[i] = heady;
game.map[snake.CoordX[i]][snake.CoordY[i]] = 2;
continue;
}
snake.CoordX[i] = snake.CoordX[i-1] + snake.moveX;
snake.CoordY[i] = snake.CoordY[i-1] + snake.moveY;
game.map[snake.CoordX[i]][snake.CoordY[i]] = 3;
}
}
void FoodInfoSetting(){
food.reput = 1;
GPutFood();
}
void PlayerInfoSetting(){
player.choice = 0;
player.quit = 0;
player.leave = 0;
player.menuBack = 0;
player.keypressed = NULL;
}
void MStartGame(){
game.GameStarted = 1;
while(game.GameStarted){
GDraw();
player.keypressed = getch();
if(player.keypressed == 0xe0) player.keypressed = getch();
GMoveSnake();
GPutFood();
Sleep(50);
fflush(stdin);
if(player.keypressed == 27) LoadPauseStatusEntrance();
}
}
void GDraw(){
int row, col;
for(row=0; row<game.ScreenHeight;row++){
for(col=0; col<game.ScreenWidth;col++){
switch(game.map[row][col]){
case 1: printf("#"); break;
case 2: printf("%c",snake.head[snake.direction]); break;
case 3: printf("*"); break;
case 4: printf("f"); break;
default: printf(" "); break;
}
if(col==game.ScreenWidth-1) printf("\n");
}
}
}
void GMoveSnake(){
int i, changeDirect, sLong, headx, heady;
int move[4][2] = {
{-1, 0}, //MOVE UP
{ 1, 0}, //MOVE DOWN
{ 0,-1}, //MOVE LEFT
{ 0, 1} //MOVE RIGHT
};
switch(player.keypressed){
//ESC ascii is 27
case 72: changeDirect = 0; break; //Up arrow ascii is 72
case 80: changeDirect = 1; break; //Down arrow ascii is 80
case 75: changeDirect = 2; break; //Left arrow ascii is 75
case 77: changeDirect = 3; break; //Right arrow ascii is 77
}
sLong = snake.sLong;
headx = snake.CoordX[0];
heady = snake.CoordY[0];
if (game.map[headx+move[changeDirect][0]][heady+move[changeDirect][1]]!=3)
{
snake.direction = changeDirect;
}
if(headx+move[snake.direction][0] == food.fCoordX && heady+move[snake.direction][1] == food.fCoordY){
snake.sLong++;
game.map[food.fCoordX][food.fCoordY] = 0;
food.reput = 1;
}
if(game.map[headx+move[snake.direction][0]][heady+move[snake.direction][1]]==1){
game.GameOver = 1;
return;
}
headx = headx+move[snake.direction][0];
heady = heady+move[snake.direction][1];
for(i=snake.sLong-1;i>=0;i--){
if(i==snake.sLong-1){
game.map[snake.CoordX[i]][snake.CoordY[i]] = 0;
}
if(i==0){
snake.CoordX[i] = headx;
snake.CoordY[i] = heady;
game.map[snake.CoordX[i]][snake.CoordY[i]] = 2;
break;
}
snake.CoordX[i] = snake.CoordX[i-1];
snake.CoordY[i] = snake.CoordY[i-1];
game.map[snake.CoordX[i]][snake.CoordY[i]] = 3;
}
}
void GPutFood(){
srand((unsigned)time(NULL));
while(food.reput){
food.fCoordX= 2 + rand() % (game.ScreenWidth-1);
food.fCoordY= 2 + rand() % (game.ScreenHeight-1);
if (game.map[food.fCoordX][food.fCoordY] == 0){
game.map[food.fCoordX][food.fCoordY] = 4;
food.reput = 0;
}
}
}
void MExitGame(){
GClearGameVariable();
player.leave = 1;
player.menuBack = 1;
return;
}
void GClearGameVariable(){
int i,row;
if(snake.CoordX == NULL && snake.CoordY == NULL & game.map == NULL) return;
for(i=0;i<snake.sLong;i++){
if(snake.CoordX!=NULL) free(snake.CoordX);
if(snake.CoordX!=NULL) free(snake.CoordY);
}
for(row=0;row<game.ScreenWidth;row++){
if(game.map[row]!=NULL) free(game.map[row]);
}
if(game.map!=NULL) free(game.map);
game.map = snake.CoordX = snake.CoordY = NULL;
}这是别人的,我不知道哪里错了??
|
最佳答案
查看完整内容
在啊哈C上试了一下貌似没问题??????????(编辑时间长达一小时的评论。。。)
|