大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺
贪吃蛇无敌版,可穿墙,英文输入法小写字母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("■");
}
}
运行结果如下:
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/197104.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...