提示:本文仅是对个人学习经历的一个记录
前言
作为一个小白,关于我的学习经历,我想在此做一个记录。
本文关于用C语言实现贪吃蛇小游戏。
PS:目前作者在学习C语言的内容,所以代码是基于C语言部分知识编写的。
一、ncurses是什么?
ncurses(new curses)是一个程序库,它提供了API,可以允许程序员编写独立于终端的基于文本的用户界面(英语:Text-based_user_interface)。它是一个虚拟终端中的“类GUI”应用软件工具箱。它还优化了屏幕刷新方法,以减少使用远程shell时遇到的延迟。
二、实现步骤
1.代码如下
#include <curses.h>
#include <stdlib.h>
/*
author: nopd
data: 2023.07.21
funDescription: Snake game */
#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;
struct Snake *head = NULL;
struct Snake *tail = NULL;
struct Snake food;
void initFood()
{
int x = rand()%20;
int y = rand()%20;
food.hang = x;
food.lie = y;
}
int hasSnakeNode(int i,int j)
{
struct Snake *p;
p = head;
while( p != NULL){
if(p->hang == i && p->lie == j){
return 1;
}
p = p->next;
}
return 0;
}
int hasFood(int i,int j)
{
if(food.hang == i && food.lie == j){
return 1;
}
return 0;
}
void initNcurse()
{
initscr();
keypad(stdscr,1);
noecho();
}
void gameMap()
{
int hang;
int lie;
move(0,0);
for( hang=0;hang<20;hang++){
if( hang==0){
for( lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
}
if( hang>=0 && hang<=19){
for( lie=0;lie<=20;lie++){
if( lie==0 || lie==20){
printw("|");
}else if(hasSnakeNode(hang,lie)){
printw("[]");
}else if(hasFood(hang,lie)){
printw("##");
}else{
printw(" ");
}
}
printw("\n");
}
if( hang==19){
for( lie=0;lie<20;lie++){
printw("--");
}
printw("\n");
printw(" ----------By nopd!----------\n");
printw(" ----key = %d,food(%d,%d) ---\n",key,food.hang,food.lie);
}
}
}
void addNode()
{
struct Snake *new = (struct Snake *)malloc(sizeof(struct Snake));
new->next = NULL;
switch(dir){
case UP:
new->hang = tail->hang-1;
new->lie = tail->lie;
break;
case DOWN:
new->hang = tail->hang+1;
new->lie = tail->lie;
break;
case LEFT:
new->hang = tail->hang;
new->lie = tail->lie-1;
break;
case RIGHT:
new->hang = tail->hang;
new->lie = tail->lie+1;
break;
}
tail->next = new;
tail = new;
}
void deleNode()
{
struct Snake *p;
p = head;
head = head->next;
free(p);
}
void initSnake()
{
struct Snake *p;
dir = RIGHT;
while( head != NULL){
p = head;
head = head->next;
free(p);
}
initFood();
head = (struct Snake *)malloc(sizeof(struct Snake));
head->hang = 1;
head->lie = 1;
head->next = NULL;
tail = head;
addNode();
addNode();
}
int snakeDie()
{
struct Snake *p;
p = head;
if( tail->hang<0 || tail->lie==0 || tail->hang==20 || tail->lie==20){
return 1;
}
while( p->next!=NULL){
if( p->hang ==tail->hang && p->lie ==tail->lie){
return 1;
}
p=p->next;
}
return 0;
}
void moveSnake()
{
addNode();
if( hasFood(tail->hang,tail->lie)){
initFood();
}else{
deleNode();
}
if( snakeDie()){
initSnake();
}
gameMap();
}
void refreshMap()
{
while(1){
moveSnake();
refresh();
usleep(100000);
}
}
void turn(int data)
{
if( abs(dir) != abs(data)){
dir = data;
}
}
void changeDir()
{
while(1){
key = getch();
switch(key){
case KEY_DOWN:
turn(DOWN);
break;
case KEY_UP:
turn(UP);
break;
case KEY_LEFT:
turn(LEFT);
break;
case KEY_RIGHT:
turn(RIGHT);
break;
}
}
}
int main()
{
pthread_t t1;
pthread_t t2;
initNcurse();
initSnake();
gameMap();
pthread_create( &t1,NULL,refreshMap,NULL);
pthread_create( &t2,NULL,changeDir,NULL);
while(1);
getch();
endwin();
return 0;
}
2.运行结果
C语言贪吃蛇小程序运行结果
总结
以上就是今天要讲的内容,本文简单介绍了基于C语言的知识实现贪吃蛇小游戏。以后我会对其继续修改,让它能够实现更多的功能,流程更合理,结构更简单。如有错漏,望批评指正。让我们共同进步吧!!