自制程序3:贪吃蛇

这篇博客详细介绍了如何利用C++编程语言实现经典游戏——贪吃蛇,包括游戏规则和操作说明,主要涉及键盘事件处理和游戏逻辑实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

游戏规则开始后会介绍,按wsad前进

#include <iostream>
#include <termios.h>
#include <cstring>
#include <iomanip>
#include <list>
#include <unistd.h>
#define clear() cout << "\033c" << flush
using namespace std;

struct node
{
    int x, y;
};
int num[15][15];
int x, y;
node head, food;
bool Food = false;
list<node> snake;
char ch = 0;
int score = 0;
static struct termios initial_settings, new_settings;
static int peek_character = -1;
void init_keyboard();
void close_keyboard();
bool kbhit();
char readch();
void changeNum();
void drawBoard();
void move();
int scanKeyboard();
void init_snake();
void fullFood();
bool getFood();
void longer();
bool gameOver();
void intro();
int getch(); // 不回显函数

//主函数
int main()
{
    init_snake();  //初始化蛇
    init_keyboard();  //初始化键盘
    x = 0, y = 0;     //初始化移动方向
    
    intro();          //介绍游戏规则
    char a = getch();
    fullFood();       //填充食物
    drawBoard();      //绘制游戏界面
    while(ch != 'n') {
        usleep(150000);
        if(kbhit())  //如果按下按键
        {
             ch = readch(); //读取按键值
        }
        int nx = x, ny = y;
        if (ch == 'w') nx = -1,ny = 0;  //预设移动方向
        if (ch == 's') nx = 1, ny = 0;
        if (ch == 'a') nx = 0, ny = -1;
        if (ch == 'd') nx = 0, ny
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值