C语言基础项目:200 行代码实现贪吃蛇,思路+源码详解

思路分析:

(1)使用该函数首先应在开头包含头文件stdlib.h

#include<stdlib.h>(C++建议使用#include<cstdlib>,下同)

(2)在标准的C库中函数rand()可以生成0~RAND_MAX之间的一个随机数,其中RAND_MAX是stdlib.h 中定义的一个整数,它与系统有关。

(3)rand()函数没有输入参数,直接通过表达式rand()来引用;例如可以用下面的语句来打印两个随机数:

printf(“Random numbers are: %i %i\n”,rand(),rand());

(4)因为rand()函数是按指定的顺序来产生整数,因此每次执行上面的语句都打印相同的两个值,所以说C语言的随机并不是真正意义上的随机,有时候也叫伪随机数。

(5)为了使程序在每次执行时都能生成一个新序列的随机值,我们通常通过为随机数生成器提供一粒新的随机种子。函数srand()(来自stdlib.h)可以为随机数生成器播散种子。只要种子不同rand()函数就会产生不同的随机数序列。srand()称为随机数生成器的初始化器。

修改部分及bug:

1.速度值反show函数及操作中的bug

2.源代码注释

3.新增最高纪录变量

源码展示:(CSDN:Shawn Hou)

#include <windows.h>
#include <stdlib.h>
#include <conio.h>
#include <time.h>
#include <cstring>
#include <cstdio>
#include <iostream>
#define  N 25
using namespace std;
int gameover;//游戏失败的值
int x1, y1; // 随机生成食物的坐标
int x,y;
int record=0;  //当前用户最高纪录
long start;

//下面定义贪吃蛇的坐标类
class snake_position
{
public:
    int x,y;
    snake_position(){};
    void initialize(int &);//坐标初始化
};
snake_position position[(N-2)*(N-2)+1]; //定义贪吃蛇坐标类数组,有(N-2)*(N-2)个坐标
void snake_position::initialize(int &j)
{
        x = 1;
        y = j;
}
//下面定义贪吃蛇的棋盘图
class snake_map
{
private:
    char s[N][N];//定义贪吃蛇棋盘,包括墙壁。
    int grade, length;
    int gamespeed; //前进时间间隔
    char direction; // 初始情况下,向右运动
    int head,tail;//头和尾
    int score;//分数
    bool gameauto;
public:
    snake_map(int h=4,int t=1,int l=4,char d=77,int s=0):length(l),direction(d),head(h),tail(t),score(s){}
    void initialize();   //初始化函数
    void show_game();
    int updata_game();
    void setpoint();
    void getgrade();
    void display();
};
//定义初始化函数,将贪吃蛇的棋盘图进行初始化
void snake_map::initialize()
{
    int i,j;
    for(i=1;i<=3;i++)
        s[1][i] = '*';
    s[1][4] = '#';
    for(i=1;i<=N-2;i++)
        for(j=1;j<=N-2;j++)
 
  • 10
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
用windows api 做的贪吃蛇 #include #include"resource.h" #include"Node.h" #include #include TCHAR szAppname[] = TEXT("Snack_eat"); #define SIDE (x_Client/80) #define x_Client 800 #define y_Client 800 #define X_MAX 800-20-SIDE //点x的范围 #define Y_MAX 800-60-SIDE //点y的范围 #define TIME_ID 1 #define SECOND 100 #define NUM_POINT 10 //点的总个数 #define ADD_SCORE 10 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow) { HWND hwnd; //窗口句柄 MSG msg; //消息 WNDCLASS wndclass; //窗口类 HACCEL hAccel;//加速键句柄 wndclass.style = CS_HREDRAW | CS_VREDRAW; //窗口的水平和垂直尺寸被改变时,窗口被重绘 wndclass.lpfnWndProc = WndProc; //窗口过程为WndProc函数 wndclass.cbClsExtra = 0; //预留额外空间 wndclass.cbWndExtra = 0; //预留额外空间 wndclass.hInstance = hInstance; //应用程序的实例句柄,WinMain的第一个参数 wndclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); //设置图标 wndclass.hCursor = LoadCursor(NULL, IDC_ARROW); //载入预定义的鼠标指针 wndclass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); //设置画刷 wndclass.lpszMenuName = szAppname; //设置菜单 wndclass.lpszClassName = szAppname; //设置窗口类的名字 if (!RegisterClass(&wndclass))//注册窗口类 { MessageBox(NULL, TEXT("这个程序需要windows NT!"), szAppname, MB_ICONERROR); return 0; } hwnd = CreateWindow(szAppname, TEXT("Snack_eat"),//CreateWindow函数调用时,WndProc将受到WM_CREATE WS_OVERLAPPEDWINDOW&~WS_THICKFRAME& ~WS_MAXIMIZEBOX,//普通的层叠窗口&禁止改变大小&禁止最大化 CW_USEDEFAULT, //初始x坐标(默认) CW_USEDEFAULT, //初始y坐标 x_Client, //初始x方向尺寸 770 y_Client, //初始y方向尺寸 750 NULL, //父窗口句柄 NULL, //窗口菜单句柄 hInstance, //程序实例句柄 WinMain函数中第二个参数 NULL); //创建参数 ShowWindow(hwnd, iCmdShow);//显示窗口,iCmdShow是WinMain的第四个参数,决定窗口在屏幕中的初始化显示形式,例:SW_SHOWNORMAL表示正常显示 UpdateWindow(hwnd);//使窗口客户区重绘,通过向WndProc发送一条WM_PAINT消息而完成的 hAccel = LoadAccelerators(hInstance, szAppname);//加载加速键 while (GetMessage(&msg, NULL, 0, 0)) { if (!TranslateAccelerator(hwnd, hAccel, &msg)) { TranslateMessage(&msg); DispatchMessage(&msg); } }/* while (GetMessage(&msg, NULL, 0, 0))//GetMessage函数从消息队列中得到消息,填充msg。如果msg.message等于WM_QUIT,返回0,否则返回非0 { TranslateMessage(&msg);//将msg返回给windows已进某些键盘消息的转换 DispatchMessage(&msg);//将msg再次返回给windows }*/ return msg.wParam;//msg.wParam是PostQuitMessage函数的参数值,通常是0 } ...
以下是一个简单的贪吃蛇游戏的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define WIDTH 20 #define HEIGHT 20 int score; int gameover; int speed; int x, y; int fruit_x, fruit_y; int tail_x[100], tail_y[100]; int ntail; enum direction { STOP = 0, LEFT, RIGHT, UP, DOWN }; enum direction dir; void Setup() { gameover = 0; dir = STOP; x = WIDTH / 2; y = HEIGHT / 2; fruit_x = rand() % WIDTH; fruit_y = rand() % HEIGHT; score = 0; speed = 100; ntail = 0; } void Draw() { system("cls"); for (int i = 0; i < HEIGHT + 2; i++) { for (int j = 0; j < WIDTH + 2; j++) { if (i == 0 || i == HEIGHT + 1) printf("#"); else if (j == 0 || j == WIDTH + 1) printf("#"); else if (i == y && j == x) printf("O"); else if (i == fruit_y && j == fruit_x) printf("F"); else { int print_tail = 0; for (int k = 0; k < ntail; k++) { if (tail_x[k] == j && tail_y[k] == i) { printf("o"); print_tail = 1; } } if (!print_tail) printf(" "); } } printf("\n"); } printf("Score: %d\n", score); printf("Speed: %d\n", speed); } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameover = 1; break; } } } void Logic() { int prev_x = tail_x[0]; int prev_y = tail_y[0]; int prev2_x, prev2_y; tail_x[0] = x; tail_y[0] = y; for (int i = 1; i < ntail; i++) { prev2_x = tail_x[i]; prev2_y = tail_y[i]; tail_x[i] = prev_x; tail_y[i] = prev_y; prev_x = prev2_x; prev_y = prev2_y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; } if (x < 1 || x > WIDTH || y < 1 || y > HEIGHT) gameover = 1; for (int i = 0; i < ntail; i++) { if (tail_x[i] == x && tail_y[i] == y) gameover = 1; } if (x == fruit_x && y == fruit_y) { score += 10; fruit_x = rand() % WIDTH; fruit_y = rand() % HEIGHT; ntail++; speed -= 5; } } int main() { Setup(); while (!gameover) { Draw(); Input(); Logic(); Sleep(speed); } printf("Game Over!\n"); return 0; } ``` 该代码基于控制台,在 Windows 系统下编译运。游戏规则与经典贪吃蛇游戏相同,玩家通过方向键控制蛇的移动,吃到食物加分并增加蛇的长度,碰到边界或自身游戏结束。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值