当前位置:网站首页>C language greedy snake
C language greedy snake
2022-07-07 23:40:00 【Bald happiness you don't understand】
Novice self study C, Writing games for the first time , Very excited , Make a note of
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#include <conio.h>
// auxiliary
#define mapheight 25
#define mapwidth 60
#define shejieshu 50
// Auxiliary function , Cursor movement
void gotoxy(int x, int y)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
COORD coord;
coord.X = x;
coord.Y = y;
SetConsoleCursorPosition(handle, coord);
}
struct // food
{
int x;
int y;
}food;
// The snake
struct
{
int x[shejieshu];
int y[shejieshu];
int len;
int v;
}she;
int fangxiang = 'A';
int chuchang = 0;
int grade = 0;
void map()
{
srand((unsigned int)time(NULL)); // Random function seed
for (int i = 0; i <= mapheight; i++)
{
gotoxy(0, i);
printf("□");
gotoxy(mapwidth, i);
printf("□");
}
for (int j = 0; j<= mapwidth; j+=2)
{
gotoxy(j, 0);
printf("□");
gotoxy(j, mapheight);
printf("□");
}
she.len = 3;
she.v ;
she.x[0] = mapwidth / 2;
she.y[0] = mapheight / 2;
gotoxy(she.x[0], she.y[0]);
printf("■");
for (int k = 1; k < she.len; k++)
{
she.x[k] = she.x[k - 1] + 2;
she.y[k] = she.y[k - 1];
gotoxy(she.x[k], she.y[k]);
printf("■");
}
while (1)
{
food.x = rand() % (mapwidth - 4) + 2;
food.y = rand() % (mapheight - 2) + 1;
if (food.x % 2 == 0)
{
break;
}
}
gotoxy(food.x, food.y);
printf("■");
}
void makefood()
{
if (she.x[0] == food.x&&she.y[0] == food.y)
{
srand((unsigned int)time(NULL));
while (1)
{
int flag = 1;
while (1)
{
food.x = rand() % (mapwidth - 4) + 2;
food.y = rand() % (mapheight - 2) + 1;
if (food.x % 2 == 0)
{
break;
}
}
int k;
for ( k = 0; k < she.len; k++);
{
if (she.y[k] == food.y&&she.x[k] == food.x||food.x % 2 != 0)
{
flag = 1;
break;
}
if (flag == 0)
{
break;
}
}
gotoxy(food.x, food.y);
printf("■");
she.len++;
//chuchang = 1;
grade = grade + 1;
break;
}
}
}
void anjian()
{
int chaoxiang;
if (_kbhit()) // Get keyboard information
{
fflush(stdin);
chaoxiang = _getch();
if (fangxiang != 'A'&&chaoxiang == 'D')
{
fangxiang = chaoxiang;
}
if (fangxiang != 'W'&&chaoxiang == 'S')
{
fangxiang = chaoxiang;
}
if (fangxiang != 'S'&&chaoxiang == 'W')
{
fangxiang = chaoxiang;
}
if (fangxiang != 'D'&&chaoxiang == 'A')
{
fangxiang = chaoxiang;
}
}
if (!chuchang)
{
gotoxy(she.x[she.len - 1], she.y[she.len - 1]);
printf(" ");
}
for (int i = she.len - 1; i > 0; i--)
{
she.x[i] = she.x[i - 1];
she.y[i] = she.y[i - 1];
}
switch (fangxiang)
{
case 'W':she.y[0]--; break;
case 'S':she.y[0]++; break;
case 'A':she.x[0]-= 2; break;
case 'D':she.x[0]+= 2; break;
}
gotoxy(she.x[0], she.y[0]);
printf("■");
chuchang = 0;
gotoxy(mapwidth + 2, 0);
}
int shezhangtai()
{
if (she.y[0] == 0 || she.y[0] == mapheight || she.x[0] == 0 || she.x[0] == mapwidth)
{
return 0;
}
for (int k = 1; k < she.len; k++)
{
if (she.x[0] == she.x[k] && she.y[k] == she.y[0])
return 0;
}
return 1;
}
int main()
{
int c ;
while (1)
{
printf(" Please select the speed ,1 For the ordinary ,2 Advanced ,3 For difficulties ,4 For gods \n");
scanf_s("%d", &c);
switch (c)
{
case 1:she.v = 500; break;
case 2:she.v = 300; break;
case 3:she.v = 100; break;
case 4:she.v = 50; break;
default:printf(" Please enter 1,2,3 A number in \n");
}
if (c == 1 || c == 2 || c == 3 || c == 4)
{
break;
}
}
printf(" Please open capitalization ");
system("pause");
system("cls");
map();
while (1)
{
gotoxy(mapwidth + 2, 0);
printf(" score :%d", grade);
makefood();
Sleep(she.v);
anjian();
if (!shezhangtai())
{
break;
}
}
gotoxy(mapwidth , mapheight +1);
printf("GAME OVER\n");
system("pause");
return 0;
}
边栏推荐
- Map operation execution process
- Dataguard 主备清理归档设置
- Flash encryption process and implementation of esp32
- Interface
- Display the server hard disk image to the browser through Servlet
- postgis学习
- One of the anti climbing methods
- Take you hand in hand to build Eureka server with idea
- 95.(cesium篇)cesium动态单体化-3D建筑物(楼栋)
- Possible SQL for Oracle table lookup information
猜你喜欢
![[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware](/img/bc/8241a339cca9b7af475169dba39c10.jpg)
[STM32 + esp-12s connect Tencent cloud IOT development platform 1] creation of cloud platform and burning of at firmware
![Balanced binary tree [AVL tree] - insert, delete](/img/1f/cd38b7c6f00f2b3e85d4560181a9d2.png)
Balanced binary tree [AVL tree] - insert, delete

C inheritance and interface design polymorphism

【实验分享】通过Console口登录到Cisco设备

Open source hardware small project: anxinco esp-c3f control ws2812

2022注册测绘师备考开始 还在不知所措?手把手教你怎么考?

The file format and extension of XLS do not match

Get started with mongodb

Pycharm essential plug-in, change the background (self use, continuous update) | CSDN creation punch in

MySQL Architecture
随机推荐
SAP 内存参数调优过程
企业应用需求导向开发之人力部门,员工考勤记录和实发工资业务程序案例
P2141 [noip2014 popularization group] abacus mental arithmetic test
数据库面试题+解析
How to change the formula picture in the paper directly into the formula in word
解析token的网址
[stm32+esp8266 connects to Tencent cloud IOT development platform 3] stm32+esp8266-01s dynamically registers devices on Tencent cloud (at instruction mode) -- with source code
SAP HR reward and punishment information export
MySQL Architecture
ASP. Net open web page
Ora-01741 and ora-01704
Arbre binaire équilibré [Arbre AVL] - Insérer et supprimer
C simple question 2
Anti climbing means cracking the second
Extended tree (I) - graphic analysis and C language implementation
SAP HR social work experience 0023
B_QuRT_User_Guide(36)
C # exchange number, judge to pass the exam
平衡二叉樹【AVL樹】——插入、删除
StringUtils工具类