當前位置:商標查詢大全網 - 網遊競技 - 求經典小遊戲(五子棋 貪吃蛇 俄羅斯方塊等)c++ 源代碼。最好能有軟件設計過程 我想學思路

求經典小遊戲(五子棋 貪吃蛇 俄羅斯方塊等)c++ 源代碼。最好能有軟件設計過程 我想學思路

這有壹個最簡單的貪吃蛇的控制過程。

壹般對於此類的遊戲,都分為控制算法,顯示算法,判定算法等幾個大部分。

供參考:

#include <stdio.h>

#include <windows.h>

#include <stdlib.h>

#include <string.h>

#include <conio.h>

#include <time.h> //使用當前時間做種子;

enum dir{up,down,left,right}; //枚舉類型enum dir;

//圍墻;

void InitFence();

void OutputF();

char game[20][20];

//畫框框;

void InitFence(){

int i,j;

for(i=0; i<20; i++)

for(j=0; j<20; j++){

if(i==0||i==19||j==0||j==19)

game[i][j]= '*';

else game[i][j]= ' ';

}

}

//顯示框框;

void OutputF(){

int i,j;

for(i=0; i<20; i++){

for(j=0; j<20; j++)

printf("%c ",game[i][j]);

printf("\n");

}

}

//蛇結點;

struct SnakeNode{

int x,y;

struct SnakeNode *prior,*next;

}*head=NULL, *tail =NULL;

void add_head(int x,int y);

int get_x(struct SnakeNode *p);

int get_y(struct SnakeNode *p);

void delete_tail();

//插入頭結點;

void add_head(int x,int y){

struct SnakeNode *q= (struct SnakeNode *)malloc(sizeof(struct SnakeNode));

q->x =x; q->y =y;

q->next =head;

q->prior =NULL;

if(head) head->prior =q;

head =q;

if(!tail) tail =head;

game[x][y]= '*'; //f對象可以在定義Fence類時定義; 且Fence類在SnakeNode類前定義;

}

int get_x(struct SnakeNode *p){

return p->x;

}

int get_y(struct SnakeNode *p){

return p->y;

}

//刪除尾結點;

void delete_tail(){

struct SnakeNode *p =tail;

game[get_x(tail)][get_y(tail)]= ' ';//把尾結點的坐標表示的'*'置為空格;

if(tail==head)

tail= head= NULL;

else{

tail= tail->prior;

tail->next= NULL;

}

free(p);

}

//move移動;

struct move{

enum dir point; //枚舉變量point: 控制方向;

int food_x;

int food_y;

};

void moving(struct move *m);

void change_point(char,struct move *m); //改變方向;

void get_food(struct move *m);

void get_food(struct move *m){

srand((unsigned int) time(NULL)); //做種子(程序運行時間);

m->food_x= rand()%18+1;

m->food_y= rand()%18+1;

game[m->food_x][m->food_y]= '*';

}

void moving(struct move *m){

int a,b;

a= get_x(head); //取得頭結點橫坐標

b= get_y(head); //頭結點縱坐標

switch(m->point){

case up: --a; break;

case down: ++a; break;

case left: --b; break;

case right: ++b; break;

}

if(a==19||b==19||a==0||b==0){ //判斷是否撞墻;

printf("game over!!!\n");

exit(0);

}

if(a==m->food_x && b==m->food_y){ //吃food;

add_head(a,b);

get_food(m);

}

else{

add_head(a,b); //插入頭結點;

delete_tail(); //刪除尾結點;

}

}

void change_point(char keydown,struct move *m){

switch(keydown){

// case 'w': m->point= up; break;

// case 's': m->point= down; break;

// case 'a': m->point= left; break;

// case 'd': m->point= right; break;

case 72: m->point= up; break;

case 80: m->point= down; break;

case 75: m->point= left; break;

case 77: m->point= right; break;

}

}

//main();

int main(){

struct move m;

printf("Using 'w,s,a,d'to control direction!!!\n\n\n");

InitFence();

add_head(4,3);

add_head(4,4);

add_head(4,5);

get_food(&m);

OutputF();

while (1){

char keydown= getch(); //getch()返回鍵盤上讀取的字符;包含頭文件<conio.h>

change_point(keydown,&m);

while(!kbhit()){ //判斷有沒有按鍵落下;

system("cls"); //清屏函數;

moving(&m);

OutputF();

Sleep(200);

}

}

return 0;

}