简单的贪吃蛇代码,可上机运行

article/2025/9/30 10:42:24

 贪吃蛇无敌版,可穿墙,英文输入法小写字母wasd操作。

#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<time.h>
#include<conio.h>#define up 'w'
#define down 's'
#define left 'a'
#define right 'd'
#define space 'q'
#define slow 'e'void gotoxy(int x, int y);
int ClickControl();
void moveobject();
void food();
int color(int c);
void border();
void wall();
//srand((unsigned)time(0));int j,i,k,click,length=5;
int _time=100000000;
typedef struct Snake
{int x;int y;struct Snake *next;
}snake;
snake s={15,15};
snake *head;
snake ss[100];main()
{int c;ss[0]=s;snake temp[2];for(i=1;i<length;i++){ss[i].x=ss[0].x-2*i;ss[i].y=ss[0].y;}head=ss;while(1){wall();food();temp[0]=ss[0];ClickControl();moveobject();border();for(i=1;i<length;i++){                                 //交换temp[0]和ss[i]的值temp[1]=ss[i];ss[i]=temp[0];temp[0]=temp[1];}srand((unsigned)time(0));color(2);for(i=0;i<length;i++){if(i==0){gotoxy(ss[i].x,ss[i].y);printf("¤");
//                printf(" 0");}else{gotoxy(ss[i].x,ss[i].y);c=rand()%13+1;//			printf("");printf("⊙");}}gotoxy(68,3);printf("你的得分是:%d",length*100-500);
//		   system("cls");for(i=0;i<_time;i++);for(i=0;i<length;i++){gotoxy(ss[i].x,ss[i].y);printf("  ");}
//		_time=100000000;}
}void gotoxy(int x, int y)
{COORD pos;HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);pos.X = x;pos.Y = y;SetConsoleCursorPosition(hOutput, pos);CONSOLE_CURSOR_INFO cursor;cursor.bVisible = FALSE;cursor.dwSize = sizeof(cursor);SetConsoleCursorInfo(hOutput, &cursor);
}void moveobject()
{int x,y;x=ss[0].x;y=ss[0].y;switch (click){case up:y -= 1;break;case down:y += 1;break;case left:x -= 2;break;case right:x += 2;break;case space:_time=4000000;break;case slow:_time=100000000;break;default:break;}ss[0].x=x;ss[0].y=y;
}int ClickControl()
{char c;while (1){if (_kbhit() == 0) return 0;if (_kbhit()){click = _getch();}moveobject();}return 1;
}void food()
{int static foodx,foody,h_food=0;srand((unsigned)time(0));if(!h_food){foodx=rand()%29*2+3;foody=rand()%25+1;gotoxy(foodx,foody);
//    	printf("■");h_food=1;}gotoxy(foodx,foody);color(4);printf("■");for(i=0;i<length;i++)if(ss[i].x==foodx&&ss[i].y==foody){length+=1;h_food=0;}
}int color(int c)
{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), c);        //更改文字颜色return 0;
}void border()
{if(head->x<=1)head->x=61;else if(head->x>=61)head->x=1;if(head->y<=0)head->y=28;else if(head->y>=28)head->y=0;
}void wall()
{color(5);gotoxy(0,0);for(i=0;i<=61;i+=2){printf("■");}gotoxy(0,28);for(i=0;i<=61;i+=2){printf("■");}for(i=0;i<=28;i++){gotoxy(0,i);printf("■");}for(i=0;i<=28;i++){gotoxy(62,i);printf("■");}
}

运行结果如下:

 


http://chatgpt.dhexx.cn/article/l0iNuOaP.shtml

相关文章

cmd贪吃蛇(cmd贪吃蛇怎么做)

贪吃蛇代码-贪吃蛇的围墙代码怎么&#xff1f;贪吃蛇的围墙代码怎么写 哈哈……避邪[哈哈] 贪吃蛇在哪下载啊 我的工享里有 在dos环境下c语言编程编一个贪吃蛇游戏 程序设计及说明 该类规定游戏的范围大小。 Snake 用该类生成一个实例蛇 snake 该类用于实现对蛇的操作控制&…

C++实现cmd界面简单贪吃蛇游戏

贪吃蛇的玩法我想应该大家都是耳熟能详了。但是这游戏虽然简单&#xff0c;但是编写的难度对一个刚刚学完c,准备考研的苦逼大学生来说却是一件非常艰难的事情。 date:10月3日&#xff0c;国庆节的头3天&#xff0c;大家在外玩耍我却苦逼的在这里写代码痛苦ing&#xff0c;不知…

手敲最基础C语言代码----“贪吃蛇”

C语言创作游戏----第二弹----贪吃蛇&#xff08;无限吃&#xff09; 主函数系列&#xff1a; 创建引入头文件----方便查看代码&#xff01;&#xff01; #include<stdio.h> #include<Windows.h> #include<stdlib.h> #include<conio.h> #include<tim…

创建链表和遍历链表算法演示

#include <stdio.h> #include <malloc.h> #include <string.h> #include <stdlib.h>typedef struct Node {int data; //数据域struct Node * pNext; //指针域}Node, *pNode;//函数声明 pNode create_list(); void traverse_list(pNode pHead); int…

C++ 创建链表

本文旨在解决两个问题&#xff1a; 1、如何写一个创建链表函数 2、为什么对于单个节点必须要new&#xff0c;而不能使用& 1、如何写一个创建链表函数 代码如下 ListNode* createListNode(vector<int> input) {ListNode dummy ListNode(-1);ListNode* pre &d…

单链表创建

单链表的创建与操作 链表作为基本的数据结构&#xff0c;学习好链表的创建与操作是数据结构入门的基础。 &#xff08;小白make for myself&#xff09; 单链表的创建 typedef struct Node {int data;struct Node* next; }Node;//结构体创建&#xff0c;也可以使用*Node取址…

动态链表的创建

#include <stdio.h> //List结构样式 typedef struct node { int data; struct node *next; }Node; //创建head的空链 Node *createList() { Node *head (Node *)malloc(sizeof(Node)); if(NULL head) exit(-1); head->next NULL; return head; } Node *insertList(…

C++创建一个链表

这个是在参加面试的时候遇到的题目&#xff0c;说句实话&#xff0c;我当时不懂。 后面查了资料&#xff0c;里面写的比较仔细就不多说了。 #include <iostream> using namespace std; struct node {int data;node* next;node(int data, node* next NULL) {this->d…

如何在Python中创建与使用链表(单链表)

如何在Python中创建与使用链表&#xff08;单链表&#xff09; 最近用Python语言在Leetcode中刷题&#xff0c;接触到不少关于链表的题&#xff0c;学校目前还没有开设数据结构的课程&#xff08;开设的话应该也是以C/C语言写的&#xff09;。 因为不太了解链表使用方式&#…

循环链表的创建

循环链表的创建以及基本操作 上篇我们讲了运用头插法和尾插法创建单链表的方法&#xff0c;和两种方法的比较。 接着我们学习循环链表的创建。 只要学会了单链表的创建&#xff0c;循环链表的创建就变得很简单。 循环链表创建 单链表的结构&#xff1a; 循环链表&#xff1a…

单链表的创建

单链表类型定义 单链表是由一串结点组成的&#xff0c;其中每个结点都包含指向下一个结点的指针&#xff0c;最后一个结点的指针为空&#xff1b; 假设结点只包括一个整数和指向下一结点的指针 typedef struct node{int data;struct node *next; }LNode,*LinkList; //LNode为…

创建双向链表(详解)

双向链表操作 在学习了单链表之后&#xff0c;就顺带学习了双链表的操作。 什么是双链表&#xff1f; 双链表顾名思义&#xff0c;就是链表由单向的链变成了双向链。 使用这种数据结构&#xff0c;我们可以不再拘束于单链表的单向创建于遍历等操作&#xff0c;大大减少了在使…

如何构建一个简单链表

如何构建一个简单链表 一、 含构造函数和默认实参的结构体 typedef struct node {int data;struct node* next;node(int data 0, struct node* next NULL): data(data), next(next) {}} node; 二、 创建一个一定长度的链表 (一) 错误样例&#xff1a; int n 3;node* head …

C++:创建链表的过程详解

创建链表的过程详解 本人是一名刚开始学习算法的小白&#xff0c;今天遇到了一些关于链表的创建问题&#xff0c;查了一些资料&#xff0c;我把它们整理了一下&#xff0c;希望大家多多指教。 整体的代码&#xff1a; #include<iostream> using namespace std;struct …

链表的创建与使用

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 目录 文章目录 前言 一、链表是什么&#xff1f; 二、链表的创建与基本操作 1.链表的创建 3.链表的头插 4.链表的尾插 5.链表的销毁 6.链表的查找 7.链表的删除…

链表的创建

目录 一&#xff1a;链表的定义 二&#xff1a;链表的改进 链表的实现可以为后面JAVA的类集框架服务。 链表是一种最简单的数据结构&#xff0c;其主要目的是依靠引用关系实现多个数据的保存。 一&#xff1a;链表的定义 定义一个Node类&#xff0c;保存的数据是String型&a…

C语言之创建链表

自己琢磨着思考了一下书上的单链表的创建案例&#xff0c;记录一下自己的理解 代码如下&#xff1a; #include<stdio.h> #include<stdlib.h>struct Student{char cName[20];int age;struct Student* pNext; }; /*节点数量*/ int iCount0;/*创建链表的函数 返回头…

如何创建链表?

链表&#xff1a; 链表的组成其实很简单&#xff0c;就是由很多结点组成的。 一个结点包含数据域和指针域&#xff0c;数据域用来存放数据&#xff0c;指针域负责指向其他结点&#xff0c;起到链接的作用。创建链表&#xff1a; 其实创建一个链表也很简单&#xff0c;在我看来…

用CodeBlocks写SFML程序

vs2019 写sfml程序简直杀鸡用牛刀&#xff0c;vs2019占用资源太大了。 所以我想到了用Dev-C&#xff0c;然而我不会配置&#xff0c;卑鄙的CSDN相关资料查阅需要VIP&#xff0c;然而VIP太贵了。 SFML官方教程是用Code::Blocks&#xff0c;于是去下一个。 setup安装........ …

[笔记]使用SFML来生成分形图片

前言 最近在上《优秀科普纪录片》时&#xff0c;看了一部有关 分形 的纪录片&#xff0c;在观看的过程中&#xff0c;想着自己也来生成一些分形图片&#xff0c;正好偶然了解到了SFML这个简单的图形库&#xff0c;所以天时地利人和&#xff0c;正好查一些资料来学习一下。 以…