用c语言写一个贪吃蛇游戏

时间: 2023-06-13 21:03:20 浏览: 55
好的,以下是一个简单的贪吃蛇游戏的C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windows.h> #define UP 72 #define DOWN 80 #define LEFT 75 #define RIGHT 77 int length = 1; int bend_no; int len; char key; void record(); void load(); int life; void Delay(long double); void Move(); int Score(); void Print(); void gotoxy(int x, int y); void Bend(); void Boarder(); struct coordinate { int x; int y; int direction; }; typedef struct coordinate coordinate; coordinate head, bend[500], food, body[30]; int main() { char key; Print(); system("cls"); load(); length = 1; head.x = 25; head.y = 20; head.direction = RIGHT; Boarder(); food.x = 30; food.y = 20; life = 3; bend[0] = head; Move(); return 0; } void Move() { int a, i; do { foodrand: food.x = rand() % 70; if (food.x <= 10) { food.x += 11; } food.y = rand() % 30; if (food.y <= 10) { food.y += 11; } } while (food.x == head.x && food.y == head.y); system("cls"); Boarder(); if (head.direction == RIGHT) { gotoxy(head.x, head.y); printf(">"); } else if (head.direction == LEFT) { gotoxy(head.x, head.y); printf("<"); } else if (head.direction == UP) { gotoxy(head.x, head.y); printf("^"); } else if (head.direction == DOWN) { gotoxy(head.x, head.y); printf("v"); } for (i = 0; i < length; i++) { body[i].x = bend[i].x; body[i].y = bend[i].y; } Bend(); gotoxy(food.x, food.y); printf("*"); if (head.x == food.x && head.y == food.y) { length++; life++; record(); goto foodrand; } for (i = 0; i < bend_no; i++) { gotoxy(bend[i].x, bend[i].y); printf("O"); } for (i = 0; i < length; i++) { gotoxy(body[i].x, body[i].y); printf("*"); } Delay(length); coordinate prev = head; gotoxy(head.x, head.y); printf(" "); if (head.direction == RIGHT) { head.x++; } else if (head.direction == LEFT) { head.x--; } else if (head.direction == UP) { head.y--; } else if (head.direction == DOWN) { head.y++; } if (head.x >= 70 || head.x <= 10 || head.y >= 30 || head.y <= 10) { life--; if (life >= 0) { head = prev; Bend(); } else { system("cls"); printf("\n\n\n\n\n\n\n\n\n\t\t\t\tGame Over!\n\n\n\n\n\n\n\n\n"); printf("\t\t\t\tPress any key to quit the game\n\n\n\n\n\n\n\n\n\n"); exit(0); } } } void Delay(long double k) { long double i; for (i = 0; i <= 10000000; i++) { } } void Bend() { int i, j, diff; for (i = bend_no; i >= 0 && length < 5; i--) { if (bend[i].x == bend[i - 1].x) { diff = bend[i].y - bend[i - 1].y; if (diff < 0) { for (j = bend[i].y; j <= bend[i - 1].y; j++) { bend[len].x = bend[i].x; bend[len].y = j; len++; if (len == length) { break; } } } else { for (j = bend[i].y; j >= bend[i - 1].y; j--) { bend[len].x = bend[i].x; bend[len].y = j; len++; if (len == length) { break; } } } } else if (bend[i].y == bend[i - 1].y) { diff = bend[i].x - bend[i - 1].x; if (diff < 0) { for (j = bend[i].x; j <= bend[i - 1].x; j++) { bend[len].x = j; bend[len].y = bend[i].y; len++; if (len == length) { break; } } } else { for (j = bend[i].x; j >= bend[i - 1].x; j--) { bend[len].x = j; bend[len].y = bend[i].y; len++; if (len == length) { break; } } } } } } void Print() { gotoxy(10, 12); printf("\t\t\t\t--------------------------"); gotoxy(10, 30); printf("\t\t\t\t--------------------------"); gotoxy(20, 14); printf("WELCOME TO THE GAME 'SNAKE'"); gotoxy(20, 16); printf("Use arrow keys to control the snake"); gotoxy(20, 18); printf("Eat the food to increase the length"); gotoxy(20, 20); printf("Press any key to start the game"); gotoxy(10, 40); printf("\t\t\t\t--------------------------"); gotoxy(10, 50); printf("\t\t\t\t--------------------------"); getch(); } void load() { int row, col, r, c; gotoxy(10, 12); printf("Loading..."); gotoxy(10, 14); for (r = 1; r <= 20; r++) { for (c = 1; c <= 10000000; c++) { } printf("%c", 177); } printf("\n"); } int Score() { int score; score = length - 1; return score; } void record() { int sc = Score(); char plname[20], nplname[20], cha, c; FILE* f; f = fopen("score.txt", "a+"); if (f == 0) { f = fopen("score.txt", "w+"); system("cls"); printf("Please wait while we configure your system files"); printf("\nPress any key to continue"); getch(); } system("cls"); gotoxy(10, 12); printf("Enter your name"); gotoxy(10, 14); scanf("%s", plname); fprintf(f, "Name: %s\nScore: %d\n", plname, sc); gotoxy(10, 15); printf("Do you want to see past records? (y/n)"); cha = getch(); if (cha == 'y') { system("cls"); rewind(f); printf("\t\tSCORES\n"); while (fscanf(f, "\n%s\n%d\n", nplname, &score) != EOF) { printf("\n%s\n%d", nplname, score); } getch(); } fclose(f); } void Boarder() { system("cls"); int i; gotoxy(food.x, food.y); printf("*"); gotoxy(10, 10); printf("SCORE: %d", Score()); gotoxy(70, 10); printf("Life: %d", life); for (i = 10; i < 71; i++) { gotoxy(i, 10); printf("-"); gotoxy(i, 30); printf("-"); } for (i = 10; i < 31; i++) { gotoxy(10, i); printf("|"); gotoxy(70, i); printf("|"); } } void gotoxy(int x, int y) { COORD coord; coord.X = x; coord.Y = y; SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } ``` 这是一个基于控制台的简单贪吃蛇游戏,游戏窗口大小为80x40,可以通过修改代码自定义窗口大小和其他游戏参数。

相关推荐

最新推荐

recommend-type

Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip

Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip本资源中的源码都是经过本地编译过可运行的,评审分达到95分以上。资源项目的难度比较适中,内容都是经过助教老师审定过的能够满足学习、使用需求,如果有需要的话可以放心下载使用。 Java毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zipJava毕业设计-基于Springboot+Vue旅游网站设计-源码+数据库+使用文档+演示视频(高分项目).zip
recommend-type

Music-app-master.zip

Music-app-master
recommend-type

基于springboot的权限管理系统.zip

基于springboot的java毕业&课程设计
recommend-type

外东洪路中段.m4a

外东洪路中段.m4a
recommend-type

基于matlab+Simulink模拟的微电网系统包括包括电源、电力电子设备等+源码+开发文档(毕业设计&课程设计&项目开发)

基于matlab+Simulink模拟的微电网系统包括包括电源、电力电子设备等+源码+开发文档,适合毕业设计、课程设计、项目开发。项目源码已经过严格测试,可以放心参考并在此基础上延申使用~ 项目简介: 这是一个完整的微电网模型,包括电源、电力电子设备、使用MatLab和Simulink的负载和电源模型。该模型基于费萨尔·穆罕默德的硕士论文《微网格建模与仿真》。 什么是微电网 模拟的微电网使用一组电源和负载在与任何集中式电网(宏电网)断开连接的情况下工作,并自主运行,为其局部区域提供电力。该仿真对微电网在稳态下进行建模,以分析其对输入变化的瞬态响应。 此模拟的目的 对系统进行全年模拟,测量负载、产量、电压和频率。 给出简化规划和资源评估阶段的方法。
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

SQL怎么实现 数据透视表

SQL可以通过使用聚合函数和GROUP BY子句来实现数据透视表。 例如,假设有一个销售记录表,其中包含产品名称、销售日期、销售数量和销售额等信息。要创建一个按照产品名称、销售日期和销售额进行汇总的数据透视表,可以使用以下SQL语句: ``` SELECT ProductName, SaleDate, SUM(SaleQuantity) AS TotalQuantity, SUM(SaleAmount) AS TotalAmount FROM Sales GROUP BY ProductName, SaleDate; ``` 该语句将Sales表按照ProductName和SaleDat
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。