#include<graphics.h>
#include<stdio.h>
#include<stdlib.h>
#define screensize 1000
#define up 0
#define down 1
#define left 2
#define right 3
#define foodnumber 2
int snakelong = 5;
char score = 0;
int foodx = 0;
int foody = 0;
bool gameover = false;
int sd = down;
int x[64] = { 0,0,0 },y[64] = { 2,1,0 };
void drawxy(int x,int y)
{
int q = screensize / 20;
rectangle(x * q, y * q, x * q + q, y * q + q);
}
void drawxy_(int x, int y)
{
int q = screensize / 20;
fillrectangle(x * q, y * q, x * q + q, y * q + q);
}
void creatfood() {
foodx= rand() % 20;
foody = rand() % 20;
for (int i = 0; i < snakelong; i++)
{
while (foodx == x[i] && foody == x[i])
{
foodx = rand() % 20;
foody = rand() % 20;
}
}
}
void initscreen() {
for (int i = 0; i < 20; i++)
{
for (int j = 0; j < 20; j++) {
drawxy(i, j);
}
}
}
void drawsnake() {
setfillcolor(BLUE);
for (int i = 0; i < snakelong; i++)
{
drawxy_(x[i], y[i]);
}
setfillcolor(RED);
drawxy_(foodx, foody);
}
void drawscore() {
char score_[6] = { 48 + score / 100,48 + score % 100 / 10,48 + score % 10 ,0,0,0};
settextcolor(WHITE);
settextstyle(50, 0, "����");
setbkmode(TRANSPARENT);
outtextxy(1010, 0, "SCORE:");
outtextxy(1010, 50, score_);
}
void drawing() {
cleardevice();
BeginBatchDraw();
initscreen();
drawsnake();
drawscore();
EndBatchDraw();
}
void keyborad() {
if (GetAsyncKeyState(VK_UP))//�����¼�
{
//printf("UP has been pressed\n");
if (sd!=down)
{
sd = up;
}
}
if (GetAsyncKeyState(VK_DOWN))
{
//printf("DOWN has been pressed\n");
if (sd != up) {
sd = down;
}
}
if (GetAsyncKeyState(VK_LEFT))
{
//printf("LEFT has been pressed\n");
if (sd != right) {
sd = left;
}
}
if (GetAsyncKeyState(VK_RIGHT))
{
//printf("RIGHT has been pressed\n");
if (sd != left) {
sd = right;
}
}
}
void over() {
printf("gameover\n");
cleardevice();
IMAGE img;
loadimage(&img, "./cai.png",1200,1000);// ./��ʾ�� ǰ�ļ��У�../��ʾ��һ��Ŀ¼
putimage(0, 0, &img);
Sleep(3000);
closegraph();
HWND hmd = GetHWnd();
int isok = MessageBox(hmd, "���ȷ�Ͽ�ʼ�˳�", "GAME OVER", MB_OK);
if (isok == IDOK) {
}
}
void deadcheck() {
for (int i = 0; i < snakelong; i++)
{
switch (sd)
{
case up:
if (x[i] == x[0] && y[i] == (y[0] - 1)) {
gameover = true;
}
break;
case down:
if (x[i] == x[0] && y[i] == (y[0] + 1)) {
gameover = true;
}
break;
case left:
if (y[i] == y[0] && x[i] == (x[i] - 1)) {
gameover = true;
}
break;
case right:
if (y[i] == y[0] && x[i] == (x[i] + 1)) {
gameover = true;
}
break;
}
}
if (gameover==true)
{
over();
}
}
void snakerun() {
for (int i = snakelong+3; i>0; i--)
{
x[i ] = x[i-1];
y[i] = y[i-1];
}
switch (sd)
{
case up:y[0]--; break;
case down:y[0]++; break;
case left:x[0]--; break;
case right:x[0]++; break;
}
if (x[0]<0)
{
x[0] += 20;
}
if (x[0]>19)
{
x[0] -= 20;
}
if (y[0] < 0)
{
y[0] += 20;
}
if (y[0] > 19)
{
y[0] -= 20;
}
if (x[0]==foodx&&y[0]==foody)
{
snakelong+=3;
creatfood();
score++;
printf("%d\n",score);
}
}
void screenchange() {
HWND hnd = GetHWnd();
SetWindowText(hnd, "̰����");//�Ĵ�������
int isok = MessageBox(hnd, "���ȷ�Ͽ�ʼ��Ϸ", "̰���� by lyy", MB_OK);
}
int main() {
initgraph(screensize+200, screensize, NOMINIMIZE);
screenchange();
creatfood();
while (!gameover)
{
drawing();
Sleep(500);
keyborad();
deadcheck();
snakerun();
//if (gameover == true) break;
}
return 0;
}