制作贪吃蛇步骤

1.构造小蛇

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

#define High 20         //游戏画面尺寸
#define Width 30

int canvas[High][Width]={0};    //二维数组存储游戏画布中对应的元素
    //0为空格,-1为边框#,1为蛇头@,大于1的正数为蛇身*

void gotoxy(int x,int y)        //将光标移动到(x,y)位置
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}


void startup()                 //数据的初始化
{
	int i,j;
		//初始化边框
    for (i=0; i < High; i++)
    {
		canvas[i][0]=-1;
        canvas[i][Width-1]=-1;
	}
	for (j=0;j<Width;j++)
    {
		canvas[0][j] = -1;
        canvas[High-1][j] = -1;
	}

	//初始化蛇头位置
	canvas[High/2][Width/2] = 1;
    //初始化蛇身,画布中的元素值分别为2·3·4·5等
	for (i=1; i<=4; i++)
		canvas[High/2][Width/2-i] = i+1;

}

void show()                           //显示画面
{
	gotoxy(0,0);                      //光标移动到原点位置,以下重画清屏
    int i,j;
	for (i=0; i<High;i++)
    {
		for (j=0; j<Width; j++)
        {
			if(canvas[i][j]==0)
				printf(" ");             //输出空格
            else if (canvas[i][j]==-1)
			    printf("#");            //输出边框#
            else if (canvas[i][j]==1)
			    printf("@");            //输出蛇头@
            else if (canvas[i][j]>1)
			    printf("*");            //输出蛇身*
		}
		printf("\n");
	}
}

void updateWithoutInput()                //与用户输入无关的更新
{
}

void updateWithInput()                   //与用户输入有关的更新
{
}

int main()
{
	startup();                            //数据的初始化
	while(1)                              //游戏循环执行
	{
		show();                           //显示画面
		updateWithoutInput();             //与用户输入无关的更新
		updateWithInput();                //与用户输入有关的更新
	}
	return 0;
}

2.小蛇的移动

# include <stdio.h>
# include <stdlib.h>
# include <conio.h>
# include <windows.h>
 
# define High 20                                        //游戏画面尺寸
# define Width 30
 
//全局变量
int moveDirection;                                      //小蛇移动方向上下左右1234
int canvas[High][Width] = {0};                          //二维数组存储游戏画布中对应的元素
                 //0为空格,-1为边框#,1为蛇头@,大于1的正数为蛇身
void gotoxy(int x, int y)                               //将光标移动到(x, y)的位置
{
    HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(handle, pos);
}
 
//移动小蛇
//第一步扫描数组canvas所有元素,找到正数元素都加1
//找到最大元素(即蛇尾),把其变为0
//找到等于2的元素(即蛇头),根据输出的上下左右方向把对应的另一个像素值设为1(新蛇头)
void moveSnakeByDirection()
{
    int i, j;
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
                canvas[i][j]++;
 
    int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
    int max = 0;
 
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
            {
                if(max < canvas[i][j])
                {
                    max = canvas[i][j];
                    oldTail_i = i;
                    oldTail_j = j;
                }
                if(canvas[i][j] == 2)
                {
                    oldHead_i = i;
                    oldHead_j = j;
                }
            } 
    canvas[oldTail_i][oldTail_j] = 0;
 
    if(moveDirection == 1)                                //向上移动
        canvas[oldHead_i - 1][oldHead_j] = 1;
    if(moveDirection == 2)                                //向下
        canvas[oldHead_i + 1][oldHead_j] = 1;
    if(moveDirection == 3)                                //向左
        canvas[oldHead_i][oldHead_j - 1] = 1;
    if(moveDirection == 4)                                //向右
        canvas[oldHead_i][oldHead_j + 1] = 1;
}
 
void startup()                                            //数据的初始化
{
    int i, j;
 
    //初始化边框
    for(i = 0; i < High; i++)
    {
        canvas[i][0] = -1;
        canvas[i][Width-1] = -1;
    }
    for(j = 0; j < Width; j++)
    {
        canvas[0][j] = -1;
        canvas[High-1][j] = -1;
    }
 
    //初始化蛇头位置
    canvas[High/2][Width/2] = 1;
    //初始化蛇身,画布中的元素值分别为2,3,4,5等
    for(i = 1; i <= 4; i++)
        canvas[High/2][Width/2 - i] = i + 1;
 
    //初始小蛇向右移动
    moveDirection = 4;
}
 
void show()                                               //显示画面
{
    gotoxy(0, 0);                                         //光标移动到原点位置,以下重画清屏
    int i, j;
    for(i = 0; i < High; i++)
    {
        for(j = 0; j < Width; j++)
        {
            if(canvas[i][j] == 0)
                printf(" ");                                //输出空格
            else if(canvas[i][j] == -1)
                printf("#");                                //输出边框#
            else if(canvas[i][j] == 1)
                printf("@");                                //输出蛇头@
            else if(canvas[i][j] > 1)
                printf("*");                                //输出蛇身*
        }
        printf("\n");
    }
    Sleep(100);        
}
 
void updateWithoutInput()                                 //与用户输入无关的更新
{
    moveSnakeByDirection();
}
 
void updateWithInput()                                    //与用户输入有关的更新
{
}
 
int main()
{
    startup();                                            //数据的初始化
    while(1)                                              //游戏循环执行
    {
        show();                                           //显示画面
        updateWithoutInput();                             //与用户输入无关的更新
        updateWithInput();                                //与用户输入有关的更新
    }
    return 0;
}

3.玩家控制小蛇的移动

void updateWithInput()        //与用户输入有关的更新
{
	char input;
	if(kbhit())               //判断是否有输入
	{
		input = getch();      //根据用户的不同输入来移动,不必输入回车
		if (input== 'a')
		{
			moveDirection = 3;         //位置左移
			moveSnakeByDirection();
		}
		else if (input == 'd')
		{
            moveDirection = 4;         //位置右移
			moveSnakeByDirection();
		}
        else if (input == 'w')
		{
            moveDirection = 1;         //位置上移
			moveSnakeByDirection();
		}
        else if (input == 's')
		{
            moveDirection = 2;         //位置下移
			moveSnakeByDirection();
		}
	}
}
 

4.判断游戏失败

void moveSnakeByDirection()
{
    int i, j;
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
                canvas[i][j]++;
 
    int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
    int max = 0;
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
            {
                if(max < canvas[i][j])
                {
                    max = canvas[i][j];
                    oldTail_i = i;
                    oldTail_j = j;
                }
                if(canvas[i][j] == 2)
                {
                    oldHead_i = i;
                    oldHead_j = j;
                }
            } 
    canvas[oldTail_i][oldTail_j] = 0;
    int newHead_i. newHead_j;
    if(moveDirection) == 1                              //向上移动
    {
        newHead_i = oldHead_i - 1;
        newHead_j = oldHead_j;
    }
    if(moveDirection) == 2                              //向下移动
    {
        newHead_i = oldHead_i + 1;
        newHead_j = oldHead_j;
    }
    if(moveDirection) == 3                              //向左移动
    {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j - 1;
    }
    if(moveDirection) == 4                              //向右移动
    {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j + 1;
    }
 
    //小蛇是否和自身撞或者和边框撞,游戏失败
    if(canvas[newHead_i][newHead_j] > 0 || canvas[newHead_i][newHead_j] == -1)
    {
        printf("游戏失败!\n");
        exit(0);
    }
    else
        canvas[newHead_i][newHead_j] = 1;
}

5吃食物增加长度以及完整版贪吃蛇代码

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>

#define High 20         //游戏画面尺寸
#define Width 30

//全局变量
int moveDirection;              //小蛇移动位置,上下左右分别用1.2.3.4表示
int food_x,food_y;
int canvas[High][Width]={0};    //二维数组存储游戏画布中对应的元素
    //0为空格,-1为边框#,1为蛇头@,大于1的正数为蛇身*

void gotoxy(int x,int y)        //将光标移动到(x,y)位置
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X=x;
	pos.Y=y;
	SetConsoleCursorPosition(handle,pos);
}
//移动小蛇
//第一步扫描数组canvas所有元素,找到正数元素都加1
//找到最大元素(即蛇尾),把其变为0
//找到等于2的元素(即蛇头),根据输出的上下左右方向把对应的另一个像素值设为1(新蛇头)
void moveSnakeByDirection()
{
    int i, j;
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
                canvas[i][j]++;
 
    int oldTail_i, oldTail_j, oldHead_i, oldHead_j;
    int max = 0;
 
    for(i = 1; i < High - 1; i++)
        for(j = 1; j < Width - 1; j++)
            if(canvas[i][j] > 0)
            {
                if(max < canvas[i][j])
                {
                    max = canvas[i][j];
                    oldTail_i = i;
                    oldTail_j = j;
                }
                if(canvas[i][j] == 2)
                {
                    oldHead_i = i;
                    oldHead_j = j;
                }
            } 
    int newHead_i, newHead_j;

    if(moveDirection == 1)                              //向上移动
    {
        newHead_i = oldHead_i - 1;
        newHead_j = oldHead_j;
    }
    if(moveDirection == 2)                              //向下移动
    {
        newHead_i = oldHead_i + 1;
        newHead_j = oldHead_j;
    }
    if(moveDirection == 3)                              //向左移动
    {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j - 1;
    }
    if(moveDirection == 4)                              //向右移动
    {
        newHead_i = oldHead_i;
        newHead_j = oldHead_j + 1;
    }

	//如果新蛇头吃到食物
	if(canvas[newHead_i][newHead_j] == -2)
	{
		canvas[food_x][food_y] = 0;
		//产生一个新的食物
		food_x = rand()%(High-5) + 2;
        food_y = rand()%(Width-5) + 2;
		canvas[food_x][food_y] = -2;

		//原来的旧蛇尾留着,长度自动加1
	}
	else                     //否则,原来的旧蛇尾减掉,保持长度不变
		canvas[oldTail_i][oldTail_j] = 0;
	//小蛇是否和自身撞或者和边框撞,游戏失败
    if(canvas[newHead_i][newHead_j] > 0 || canvas[newHead_i][newHead_j] == -1)
    {
        printf("游戏失败!\n");
		Sleep(2000);
		system("pause");
        exit(0);
    }
    else
        canvas[newHead_i][newHead_j] = 1;
}

void startup()                 //数据的初始化
{
	int i,j;

	//初始化边框
    for (i=0; i < High; i++)
    {
		canvas[i][0]=-1;
        canvas[i][Width-1]=-1;
	}
	for (j=0;j < Width; j++)
    {
		canvas[0][j] = -1;
        canvas[High-1][j] = -1;
	}

	//初始化蛇头位置
	canvas[High/2][Width/2] = 1;
    //初始化蛇身,画布中的元素值分别为2·3·4·5等
	for (i=1; i<=4; i++)
		canvas[High/2][Width/2-i] = i+1;
    //初始小蛇向右移动
    moveDirection = 4;

	food_x = rand()%(High-5) + 2;
    food_y = rand()%(Width-5) + 2;
	canvas[food_x][food_y] = -2;
}

void show()                           //显示画面
{
	gotoxy(0,0);                      //光标移动到原点位置,以下重画清屏
    int i,j;
	for (i=0; i<High;i++)
    {
		for (j=0; j<Width; j++)
        {
			if(canvas[i][j]==0)
				printf(" ");             //输出空格
            else if (canvas[i][j] == -1)
			    printf("#");            //输出边框#
            else if (canvas[i][j] == 1)
			    printf("@");            //输出蛇头@
            else if (canvas[i][j] > 1)
			    printf("*");            //输出蛇身*
            else if (canvas[i][j] == -2)
			    printf("F");            //输出食物F
		}
		printf("\n");
	}
	Sleep(100);
}

void updateWithoutInput()                                 //与用户输入无关的更新
{
    moveSnakeByDirection();
}
 
void updateWithInput()                                    //与用户输入有关的更新
{
	char input;
	if(kbhit())               //判断是否有输入
	{
		input = getch();      //根据用户的不同输入来移动,不必输入回车
		if (input== 'a')
		{
			moveDirection = 3;         //位置左移
			moveSnakeByDirection();
		}
		else if (input == 'd')
		{
            moveDirection = 4;         //位置右移
			moveSnakeByDirection();
		}
        else if (input == 'w')
		{
            moveDirection = 1;         //位置上移
			moveSnakeByDirection();
		}
        else if (input == 's')
		{
            moveDirection = 2;         //位置下移
			moveSnakeByDirection();
		}
	}
}

int main()
{
    CONSOLE_CURSOR_INFO cci;
    cci.bVisible = FALSE;
    cci.dwSize = sizeof(cci);
    SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cci);
    startup();                                            //数据的初始化
    while(1)                                              //游戏循环执行
    {
        show();                                           //显示画面
        updateWithoutInput();                             //与用户输入无关的更新
        updateWithInput();                                //与用户输入有关的更新
    }
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,制作贪吃蛇游戏可以分为以下几个步骤: 1. 导入必要的库 ``` import pygame import random ``` 2. 初始化pygame ``` pygame.init() ``` 3. 定义游戏窗口大小和蛇的初始位置 ``` WINDOW_WIDTH = 600 WINDOW_HEIGHT = 600 SNAKE_SIZE = 20 SNAKE_POS = [(WINDOW_WIDTH/2, WINDOW_HEIGHT/2)] ``` 4. 创建游戏窗口 ``` screen = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT)) pygame.display.set_caption('贪吃蛇') ``` 5. 定义蛇的移动方向 ``` direction = 'right' ``` 6. 定义食物的初始位置 ``` food_pos = (random.randint(0, (WINDOW_WIDTH-SNAKE_SIZE)//SNAKE_SIZE) * SNAKE_SIZE, random.randint(0, (WINDOW_HEIGHT-SNAKE_SIZE)//SNAKE_SIZE) * SNAKE_SIZE) ``` 7. 定义游戏循环 ``` while True: # 处理事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: direction = 'left' elif event.key == pygame.K_RIGHT: direction = 'right' elif event.key == pygame.K_UP: direction = 'up' elif event.key == pygame.K_DOWN: direction = 'down' # 移动蛇 if direction == 'left': SNAKE_POS[0] = (SNAKE_POS[0][0]-SNAKE_SIZE, SNAKE_POS[0][1]) elif direction == 'right': SNAKE_POS[0] = (SNAKE_POS[0][0]+SNAKE_SIZE, SNAKE_POS[0][1]) elif direction == 'up': SNAKE_POS[0] = (SNAKE_POS[0][0], SNAKE_POS[0][1]-SNAKE_SIZE) elif direction == 'down': SNAKE_POS[0] = (SNAKE_POS[0][0], SNAKE_POS[0][1]+SNAKE_SIZE) # 判断是否吃到食物 if SNAKE_POS[0] == food_pos: food_pos = (random.randint(0, (WINDOW_WIDTH-SNAKE_SIZE)//SNAKE_SIZE) * SNAKE_SIZE, random.randint(0, (WINDOW_HEIGHT-SNAKE_SIZE)//SNAKE_SIZE) * SNAKE_SIZE) SNAKE_POS.append(SNAKE_POS[-1]) # 绘制游戏界面 screen.fill((255, 255, 255)) pygame.draw.rect(screen, (0, 255, 0), (food_pos[0], food_pos[1], SNAKE_SIZE, SNAKE_SIZE)) for pos in SNAKE_POS: pygame.draw.rect(screen, (255, 0, 0), (pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE)) pygame.display.update() ``` 这就是一个简单的贪吃蛇游戏制作过程,可以根据需求进行修改和完善。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值