实例介绍
【实例简介】
【实例截图】
【核心代码】#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#define U 1
#define D 2
#define L 3
#define R 4
struct termios tm_old;
typedef struct SNAKE{
int x;
int y;
struct SNAKE *next;
}snake;
snake *head,*food,*q;
int status;
int sleeptime;
int speed,score,add;
void Index();
void clear();
int Getch();
void Getch_2();
int kbhit(void);
void Initmap();
void Initmsgb();
void Initsnake();
void Createfood();
void Snakemove();
int biteself();
void Cantcrosswall();
void Startgame();
void Gamestate();
void Gameset();
void Exit();
void Pause();
void Pause_2();
void Endgame();
void Index(){
clear();
printf(" __________________________________________________________ \n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 1. 开始游戏 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 2. 游戏说明 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 3. 游戏设置 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 4. 退出游戏 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|__________________________________________________________|\n");
}
void clear(){
printf("\033[2J");
printf("\033[0;0H");
printf("\033[?25l");
}
int Getch()
{
struct termios tm;
tcgetattr(0,&tm_old);
cfmakeraw(&tm);
tcsetattr(0,0,&tm);
int ch = getchar();
tcsetattr(0,0,&tm_old);
return ch;
}
void Getch_2(){
int ch;
ch=Getch();
// printf("%d",ch);
if(ch==104||ch==72){
clear();
Index();
}
else{
printf("输入有误,请重新输入\n");
Getch_2();
}
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
void Initmap(){
int x,y;
printf("\033[2J");
printf("\033[1;1H");
for(y=1;y<24;y ){
printf("\033[%d;1H",y);
printf("■");
printf("\033[%d;61H",y);
printf("■");
}
for(x=1;x<61;x=x 2){
printf("\033[1;%dH",x);
printf("■");
printf("\033[24;%dH",x);
printf("■");
}
printf(" ■");
}
void Initmsgb(){
printf("\033[5;65H ");
printf("\033[5;65H当前速度:%d",speed);
printf("\033[7;65H ");
printf("\033[7;65H当前得分:%d",score);
printf("\033[17;62H【按H键返回主界面】");
printf("\033[19;62H 【按P键暂停游戏】");
printf("\033[21;64H");
}
void Initsnake(){
int i;
snake *tail;
tail = (snake*) malloc(sizeof(snake));
tail->x=17;
tail->y=5;
tail->next=NULL;
for(i=1;i<4;i ){
head =(snake*) malloc(sizeof(snake));
head->next=tail;
head->x=17 2*i;
head->y=5;
tail = head;
}
while(tail!=NULL){
printf("\033[%d;%dH■",tail->y,tail->x);
tail=tail->next;
}
status=R;
}
void Createfood(){
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
food_1->x=rand()%28*2 3;
food_1->y=rand()%21 2;
q=head;
while(q!=NULL){
if(q->x==food_1->x&&q->y==food_1->y){
free(food_1);
food_1=NULL;
Createfood();
return;
}
q=q->next;
}
printf("\033[%d;%dH●",food_1->y,food_1->x);
food=food_1;
}
void Snakemove(){
snake *nexthead;
nexthead = (snake*)malloc(sizeof(snake));
if(status==U)
{
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==D)
{
nexthead->x=head->x;
nexthead->y=head->y 1;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==L)
{
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==R)
{
nexthead->x=head->x 2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
}
int biteself(){
snake *self;
self=head->next;
while(self!=NULL){
if(self->x==head->x&&self->y==head->y)
return 1;
self=self->next;
}
return 0;
}
void Cantcrosswall(){
if(head->x<3||head->x>59||head->y<2||head->y>22)
Endgame();
}
void Startgame(){
sleeptime=240000;
speed=1;
score=0;
add=1;
clear();
Initmap();
Initmsgb();
Initsnake();
Createfood();
while(1){
Cantcrosswall();
if(kbhit()){
int ch;
ch = Getch();
if((ch==119||ch==87)&&status!=D)
status=U;
else if((ch==115||ch==83)&&status!=U)
status=D;
else if((ch==97||ch==65)&&status!=R)
status=L;
else if((ch==100||ch==68)&&status!=L)
status=R;
else if(ch==61){
sleeptime=sleeptime-50000;
add ;
speed ;
}
else if(ch==45){
sleeptime=sleeptime 50000;
add--;
speed--;
}
else if(ch==104||ch==72){
clear();
Index();
Pause_2();
return;
}
else if(ch==112||ch==80){
Pause();
}
if(biteself()==1){
Endgame();
}
}
Snakemove();
Initmsgb();
usleep(sleeptime);
}
}
【实例截图】
【核心代码】#include <termios.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#define U 1
#define D 2
#define L 3
#define R 4
struct termios tm_old;
typedef struct SNAKE{
int x;
int y;
struct SNAKE *next;
}snake;
snake *head,*food,*q;
int status;
int sleeptime;
int speed,score,add;
void Index();
void clear();
int Getch();
void Getch_2();
int kbhit(void);
void Initmap();
void Initmsgb();
void Initsnake();
void Createfood();
void Snakemove();
int biteself();
void Cantcrosswall();
void Startgame();
void Gamestate();
void Gameset();
void Exit();
void Pause();
void Pause_2();
void Endgame();
void Index(){
clear();
printf(" __________________________________________________________ \n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 1. 开始游戏 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 2. 游戏说明 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 3. 游戏设置 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| 4. 退出游戏 |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("| |\n");
printf("|__________________________________________________________|\n");
}
void clear(){
printf("\033[2J");
printf("\033[0;0H");
printf("\033[?25l");
}
int Getch()
{
struct termios tm;
tcgetattr(0,&tm_old);
cfmakeraw(&tm);
tcsetattr(0,0,&tm);
int ch = getchar();
tcsetattr(0,0,&tm_old);
return ch;
}
void Getch_2(){
int ch;
ch=Getch();
// printf("%d",ch);
if(ch==104||ch==72){
clear();
Index();
}
else{
printf("输入有误,请重新输入\n");
Getch_2();
}
}
int kbhit(void)
{
struct termios oldt, newt;
int ch;
int oldf;
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
newt.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
oldf = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, oldf | O_NONBLOCK);
ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
fcntl(STDIN_FILENO, F_SETFL, oldf);
if(ch != EOF)
{
ungetc(ch, stdin);
return 1;
}
return 0;
}
void Initmap(){
int x,y;
printf("\033[2J");
printf("\033[1;1H");
for(y=1;y<24;y ){
printf("\033[%d;1H",y);
printf("■");
printf("\033[%d;61H",y);
printf("■");
}
for(x=1;x<61;x=x 2){
printf("\033[1;%dH",x);
printf("■");
printf("\033[24;%dH",x);
printf("■");
}
printf(" ■");
}
void Initmsgb(){
printf("\033[5;65H ");
printf("\033[5;65H当前速度:%d",speed);
printf("\033[7;65H ");
printf("\033[7;65H当前得分:%d",score);
printf("\033[17;62H【按H键返回主界面】");
printf("\033[19;62H 【按P键暂停游戏】");
printf("\033[21;64H");
}
void Initsnake(){
int i;
snake *tail;
tail = (snake*) malloc(sizeof(snake));
tail->x=17;
tail->y=5;
tail->next=NULL;
for(i=1;i<4;i ){
head =(snake*) malloc(sizeof(snake));
head->next=tail;
head->x=17 2*i;
head->y=5;
tail = head;
}
while(tail!=NULL){
printf("\033[%d;%dH■",tail->y,tail->x);
tail=tail->next;
}
status=R;
}
void Createfood(){
snake *food_1;
srand((unsigned)time(NULL));
food_1=(snake*)malloc(sizeof(snake));
food_1->x=rand()%28*2 3;
food_1->y=rand()%21 2;
q=head;
while(q!=NULL){
if(q->x==food_1->x&&q->y==food_1->y){
free(food_1);
food_1=NULL;
Createfood();
return;
}
q=q->next;
}
printf("\033[%d;%dH●",food_1->y,food_1->x);
food=food_1;
}
void Snakemove(){
snake *nexthead;
nexthead = (snake*)malloc(sizeof(snake));
if(status==U)
{
nexthead->x=head->x;
nexthead->y=head->y-1;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==D)
{
nexthead->x=head->x;
nexthead->y=head->y 1;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==L)
{
nexthead->x=head->x-2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
if(status==R)
{
nexthead->x=head->x 2;
nexthead->y=head->y;
if(nexthead->x==food->x&&nexthead->y==food->y){
nexthead->next=head;
head = nexthead;
q=head;
while(q!=NULL){
printf("\033[%d;%dH■",q->y,q->x);
q=q->next;
}
score = score add;
Createfood();
}
else{
nexthead->next=head;
head=nexthead;
q=head;
while(q->next->next!=NULL){
printf("\033[%d;%dH■",q->y, q->x);
q = q->next;
}
printf("\033[%d;%dH ",q->next->y,q->next->x);
free(q->next);
q->next = NULL;
}
}
}
int biteself(){
snake *self;
self=head->next;
while(self!=NULL){
if(self->x==head->x&&self->y==head->y)
return 1;
self=self->next;
}
return 0;
}
void Cantcrosswall(){
if(head->x<3||head->x>59||head->y<2||head->y>22)
Endgame();
}
void Startgame(){
sleeptime=240000;
speed=1;
score=0;
add=1;
clear();
Initmap();
Initmsgb();
Initsnake();
Createfood();
while(1){
Cantcrosswall();
if(kbhit()){
int ch;
ch = Getch();
if((ch==119||ch==87)&&status!=D)
status=U;
else if((ch==115||ch==83)&&status!=U)
status=D;
else if((ch==97||ch==65)&&status!=R)
status=L;
else if((ch==100||ch==68)&&status!=L)
status=R;
else if(ch==61){
sleeptime=sleeptime-50000;
add ;
speed ;
}
else if(ch==45){
sleeptime=sleeptime 50000;
add--;
speed--;
}
else if(ch==104||ch==72){
clear();
Index();
Pause_2();
return;
}
else if(ch==112||ch==80){
Pause();
}
if(biteself()==1){
Endgame();
}
}
Snakemove();
Initmsgb();
usleep(sleeptime);
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论