训练C语言基础和调试能力的小游戏,这里所有物体(蛇身、墙体、食物)均由符号代替未作修饰。
实现的主要功能:
1.方向键控制蛇的移动方向。
2.食物随机出现。
3.实时计分(还未写计分系统)。
————————————————————————————————————————————
项目代码:
#include<curses.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
#define UP 1
#define DOWN -1
#define LEFT 2
#define RIGHT -2
struct Snake{
int hang;
int lie;
struct Snake *next;
};
int key;
int dir;
int score = -1;
struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;
void* initFood()
{
int x = rand()%20;
int y = rand()%30;
while(x==0 || y==0 || x==20 || y==30){
x = rand()%20;
y = rand()%30;
}
food.hang = x;
food.lie = y;
score+=1;
noecho();
}
void initCury()
{
initscr();
keypad(stdscr,1);
noecho();
}
int hasNode(int x,int y)
{
struct Snake *p;
p = head;
while(p != NULL){
if(p->hang == x && p->lie == y){
return 1;
}
p = p->next;
}
return 0;
}
int hasFood(int x,int y)
{
if(food.hang == x && food.lie == y){
return 1;
}
return 0;
}
void mapGame()
{
int i,j;
move(0,0);
for(i=0;i<=19;i++){
if(i == 0){
for(j=0;j<=30;j++){
printw("#");
}
printw("\n");
}// 0
if(i>=0 || i<=19){
for(j=0;j<=30;j++){
if(j == 0 || j == 30){
printw("#");
}
else if(hasNode(i,j)){
printw("@");
}
else if(hasFood(i,j)){
printw("$");
}
else{
printw(" ");
}
}
printw("\n");
}
if(i == 19){
for(j=0;j<=30;j++){
printw("#");
}
}
}
printw("\nScore: %d$\n",score);
printw("food.x:%d, food.y:%d\n",food.hang,food.lie);
printw("\n\n<By: Tao Xie, Feb22,2023>\n");
noecho();
}
void addBody()
{
struct Snake *newBody = (struct Snake*)malloc(sizeof(struct Snake));
newBody->hang = tail->hang+1;
newBody->lie = tail->lie;
newBody->next = NULL;
switch(dir){
case UP:
newBody->hang = tail->hang-1;
newBody->lie = tail->lie;
break;
case DOWN:
newBody->hang = tail->hang+1;
newBody->lie = tail->lie;
break;
case LEFT:
newBody->hang = tail->hang;
newBody->lie = tail->lie-1;
break;
case RIGHT:
newBody->hang = tail->hang;
newBody->lie = tail->lie+1;
break;
}
tail->next = newBody;
tail = newBody;
}
void delBody()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
void initSnake()
{
struct Snake *p;
dir = DOWN;
while(head != NULL){
p = head;
head = head->next;
free(p);
}
initFood();
head = (struct Snake*)malloc(sizeof(struct Snake));
head->hang = 0;
head->lie = 15;
head->next = NULL;
tail = head;
addBody();
addBody();
}
int snakeKilled()
{
struct Snake *p;
p = head;
if(tail->hang<0||tail->lie==0||tail->hang==20||tail->lie==30){
return 1;
}
while(p->next!=NULL){
if(p->hang==tail->hang && p->lie==tail->lie){
return 1;
}
p = p->next;
}
return 0;
}
void moveBody()
{
addBody();
if(hasFood(tail->hang,tail->lie)){
initFood();
}
else{
delBody();
}
if(snakeKilled()){
score=-1;
initSnake();
}
}
void* refreshPage()
{
while(1){
moveBody();
mapGame();
refresh();
usleep(150000);
}
}
void turn(int direction)
{
if(abs(dir) != abs(direction)){
dir = direction;
}
}
void* contKey()
{
while(1){
key = getch();
switch(key){
case KEY_UP:
printw("key:UP\n");
turn(UP);
break;
case KEY_DOWN:
printw("key:DOWN\n");
turn(DOWN);
break;
case KEY_LEFT:
printw("key:LEFT\n");
turn(LEFT);
break;
case KEY_RIGHT:
printw("key:RIGHT\n");
turn(RIGHT);
break;
}
}
noecho();
}
void main()
{
pthread_t t1;
pthread_t t2;
pthread_t t3;
initCury();
initSnake();
mapGame();
pthread_create(&t1,NULL,refreshPage,NULL);
pthread_create(&t2,NULL,contKey,NULL);
pthread_create(&t3,NULL,initFood,NULL);
while(1);
getch();
endwin();
}