想用c++编写一个贪吃蛇,怎么编啊?
3个回答
展开全部
#include "stdio.h"
#include "string.h"
#include "windows.h"
#include "stdlib.h"
#include "time.h"
#define null 0
void wall(void);
void printpoint(int x,int y);
void gotoxy(int x,int y);
void playgame(void);
void printsnake(void);
void printfood(void);
void printscore(void);
void move(struct snake);
struct snake
{
int x,y;
struct snake *next;
struct snake *front;
};struct snake snake[100],snakebody,*head,*p,*m;
void main()
{
printscore();
wall();
playgame();
getchar();
}
void wall(void)
{
char pwall;
int hang;
int lie;
int blank;
pwall=43;
for(lie=1;lie<80;lie++)
{
printf("%c",pwall);
}
printf("\n");
for(hang=1;hang<20;hang++)
{
printf("%c",pwall);
for(blank=1;blank<78;blank++)
{
printf(" ");
}
printf("%c\n",pwall);
}
for(lie=1;lie<80;lie++)
{
printf("%c",pwall);
}
printf("\n");
printf("上:w 下:s 左:a 右:d");
}
void printpoint(int x,int y)
{
HANDLE hout;
COORD coord;
coord.X=x;
coord.Y=y;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,coord);
printf("*"); // 在第x行,第y列开始显示字符串
}
void gotoxy(int x,int y)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
void playgame(void)
{
printfood();
printsnake();
}
void printsnake(void)
{
int bodynum,judgebodynum,step;
snake[bodynum];
snake[0].x=10;
snake[0].y=10;
bodynum=3;
for(judgebodynum=1;judgebodynum<=bodynum;judgebodynum++)
{
snake[judgebodynum].x=snake[judgebodynum-1].x+1;
snake[judgebodynum].y=snake[judgebodynum-1].y;
snake[judgebodynum-1].next=&snake[judgebodynum];
snake[bodynum-1].next=null;
}
snake[0].front=null;
p=&snake[0];
do
{
printpoint(p->x,p->y);
p=p->next;
}
while(p!=null);
p=&snake[0];
do
{
printpoint(p->x,p->y);
p=p->next;
(p->x)++;
}
while(p!=null);
}
void printfood(void)
{
struct food
{
int x,y;
};
struct food food1;
srand((int)time(0));
do
{
food1.y=rand()%20;
}
while(food1.y<2);
do
{
food1.x=rand()%80;
}
while(food1.x<1);
printpoint(food1.x,food1.y);
}
void printscore(void)
{
int score;
score=0;
printf("score:%d\n",score);
}
void move(struct snake)
{
int step;
snake[0].x=10;
snake[0].y=10;
for(step=0;step<10;step++)
{
snake[0].x=snake[0].x+1;
m=&snake[0];
do
{
}
while(m->next=null);
}
#include "string.h"
#include "windows.h"
#include "stdlib.h"
#include "time.h"
#define null 0
void wall(void);
void printpoint(int x,int y);
void gotoxy(int x,int y);
void playgame(void);
void printsnake(void);
void printfood(void);
void printscore(void);
void move(struct snake);
struct snake
{
int x,y;
struct snake *next;
struct snake *front;
};struct snake snake[100],snakebody,*head,*p,*m;
void main()
{
printscore();
wall();
playgame();
getchar();
}
void wall(void)
{
char pwall;
int hang;
int lie;
int blank;
pwall=43;
for(lie=1;lie<80;lie++)
{
printf("%c",pwall);
}
printf("\n");
for(hang=1;hang<20;hang++)
{
printf("%c",pwall);
for(blank=1;blank<78;blank++)
{
printf(" ");
}
printf("%c\n",pwall);
}
for(lie=1;lie<80;lie++)
{
printf("%c",pwall);
}
printf("\n");
printf("上:w 下:s 左:a 右:d");
}
void printpoint(int x,int y)
{
HANDLE hout;
COORD coord;
coord.X=x;
coord.Y=y;
hout=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hout,coord);
printf("*"); // 在第x行,第y列开始显示字符串
}
void gotoxy(int x,int y)
{
CONSOLE_SCREEN_BUFFER_INFO csbiInfo;
HANDLE hConsoleOut;
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hConsoleOut,&csbiInfo);
csbiInfo.dwCursorPosition.X = x;
csbiInfo.dwCursorPosition.Y = y;
SetConsoleCursorPosition(hConsoleOut,csbiInfo.dwCursorPosition);
}
void playgame(void)
{
printfood();
printsnake();
}
void printsnake(void)
{
int bodynum,judgebodynum,step;
snake[bodynum];
snake[0].x=10;
snake[0].y=10;
bodynum=3;
for(judgebodynum=1;judgebodynum<=bodynum;judgebodynum++)
{
snake[judgebodynum].x=snake[judgebodynum-1].x+1;
snake[judgebodynum].y=snake[judgebodynum-1].y;
snake[judgebodynum-1].next=&snake[judgebodynum];
snake[bodynum-1].next=null;
}
snake[0].front=null;
p=&snake[0];
do
{
printpoint(p->x,p->y);
p=p->next;
}
while(p!=null);
p=&snake[0];
do
{
printpoint(p->x,p->y);
p=p->next;
(p->x)++;
}
while(p!=null);
}
void printfood(void)
{
struct food
{
int x,y;
};
struct food food1;
srand((int)time(0));
do
{
food1.y=rand()%20;
}
while(food1.y<2);
do
{
food1.x=rand()%80;
}
while(food1.x<1);
printpoint(food1.x,food1.y);
}
void printscore(void)
{
int score;
score=0;
printf("score:%d\n",score);
}
void move(struct snake)
{
int step;
snake[0].x=10;
snake[0].y=10;
for(step=0;step<10;step++)
{
snake[0].x=snake[0].x+1;
m=&snake[0];
do
{
}
while(m->next=null);
}
追问
不要复制粘贴好不!这些在网上我也能搜索到的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询