貪吃蛇c原始碼
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<graphics.h>
#include<conio.h>
#include<time.h>
#include<mmsystem.h>
#pragma comment(lib,"winmm.lib")
struct Point
{
int x,y;
};
struct Snake {
Point xy[100];
int position;
int num;
};
struct food {
Point fdxy;
int flag;
int grade;
};
enum position { up, down, left, right };
Snake snake{};
food _food{};
Snake initsnake()
{
snake.xy[2].x = 0;
snake.xy[2].y = 0;
snake.xy[1].x = 10;
snake.xy[1].y = 0;
snake.xy[0].x = 20;
snake.xy[0].y = 0;
snake.position = right;
snake.num = 3;
return snake;
}
void drawSnake()
{
for (int i = 0; i < snake.num; i++) {
setlinecolor(BLACK);
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(snake.xy[i].x, snake.xy[i].y, snake.xy[i].x + 10, snake.xy[i].y + 10);
}
}
void Snakemove() {
for (int i = snake.num - 1; i > 0; i--) {
snake.xy[i].x = snake.xy[i - 1].x;
snake.xy[i].y = snake.xy[i - 1].y;
}
switch (snake.position) {
case up:
snake.xy[0].y -= 10;
break;
case down:
snake.xy[0].y += 10;
break;
case left:
snake.xy[0].x -= 10;
break;
case right:
snake.xy[0].x += 10;
break;
}
}
void keydown() {
char userkey = _getch();
switch (userkey) {
case'w':
case'W':
case 72:
if (snake.position != down) {
snake.position = up;
}
break;
case's':
case'S':
case 80:
if (snake.position != up) {
snake.position = down;
}
break;
case'a':
case'A':
case 75:
if (snake.position != right) {
snake.position = left;
}
break;
case'd':
case'D':
case 77:
if (snake.position != left) {
snake.position = right;
}
break;
}
}
void initfood() {
_food.fdxy.x = rand() % 80 * 10;
_food.fdxy.y = rand() % 60 * 10;
_food.flag = 1;
for (int i = 0; i < snake.num; i++)
{
if ((snake.xy[i].x == _food.fdxy.x) && (snake.xy[i].y == _food.fdxy.y) )
{
_food.fdxy.x = rand() % 80 * 10;
_food.fdxy.y = rand() % 60 * 10;
}
}
}
void drawfood() {
setlinecolor(BLACK);
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255));
fillrectangle(_food.fdxy.x, _food.fdxy.y, _food.fdxy.x + 10, _food.fdxy.y + 10);
}
void eatfood() {
if ((snake.xy[0].x == _food.fdxy.x) && (snake.xy[0].y == _food.fdxy.y)) {
_food.flag = 0;
snake.num++;
_food.grade += 10;
}
}
void showgrade() {
char grade[20] = "";
sprintf(grade, "得分:%d", _food.grade);
settextcolor(LIGHTGREEN);
settextstyle(25, 0, "楷體");
outtextxy(650, 50,grade);
}
HWND hwnd = NULL;
int snakedie() {
if (snake.xy[0].x > 800 || snake.xy[0].x < 0 || snake.xy[0].y>600 || snake.xy[0].y < 0) {
mciSendString("close 1.mp3 ", 0, 0, 0);
MessageBox(hwnd, "撞牆了,遊戲結束", "GameOver", MB_OK);
return 1;
}
for (int i = 1; i < snake.num; i++) {
if (snake.xy[0].x == snake.xy[i].x && snake.xy[0].y == snake.xy[i].y) {
mciSendString("close 1.mp3 ", 0, 0, 0);
MessageBox(hwnd, "自殺了,遊戲結束", "GameOver", MB_OK);
return 1;
}
}
return 0;
}
int main()
{
srand((unsigned int)time(NULL));
initgraph(800,600);
setbkcolor(RGB(248, 219, 86));
cleardevice();
mciSendString("open 1.mp3 ", 0, 0, 0);
mciSendString("play 1.mp3 ", 0, 0, 0);
initsnake();
drawSnake();
while (1) {
cleardevice();
Snakemove();
drawSnake();
if (_food.flag == 0) {
initfood();
}
if (_kbhit()) {
keydown();
}
if (snakedie()) {
break;
}
drawfood();
eatfood();
showgrade();
Sleep(60);
}
int a=_getch();
void closegraph();
return 0;
}