c++贪吃蛇游戏

VC里大多都是很难想TC一样简单的进行画图。所以这里用了一个graphics图形库,仿TC的用法也基本一样。代码参考了TC上经典的贪吃蛇。

下载 graphics图形库

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164

#include <conio.h>
#include <iostream.h>
#include <time.h>
#include <cstdlib>
#include <stdio.h >

int i,temp=-1;

struct
{
int x[400];
int y[400];
int node;
int direction;
int life;
int score;
}snake;

struct Food
{
int x;
int y;
int empty;
}food;

void showScore()
{
char str[20];
if(snake.score!=temp)
{
setfillstyle(SOLID_FILL,0);
bar(80,485,130,500);
setcolor(WHITE);
sprintf(str,"score: %d",snake.score);
outtextxy(55,485,str);
temp=snake.score;
}
}

void gamepaly()
{
srand((unsigned)time(NULL));
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
snake.x[0]=100; snake.y[0]=100;
snake.node=6;
snake.direction=1;
snake.life=1;
snake.score=0;
for(i=1;i<snake.node;i++)
{
snake.x[i]=snake.x[i-1]-10;
snake.y[i]=snake.y[i-1];
}
for(i=0;i<snake.node;i++)
rectangle(snake.x[i], snake.y[i], snake.x[i]-10, snake.y[i]-10);
food.empty=1;
while(true)
{
while(!kbhit())
{
showScore();
if(food.empty==1)
{
food.x=rand()@0;
food.y=rand()50;
while(food.x<45)
food.x+=10;
while(food.x>585)
food.x-=10;
while(food.y<35)
food.y+=10;
while(food.y>445)
food.y-=10;
while(food.x!=0)
food.x++;
while(food.y!=0)
food.y++;
food.empty=0;
}
if(food.empty==0)
{
setcolor(GREEN);
rectangle(food.x,food.y,food.x-10,food.y-10);
}
for(i=1;i<snake.node;i++)
{
if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0])
{
snake.life=0;
MessageBox(NULL,"Your snake is dead","oop",MB_OK|MB_SYSTEMMODAL);
break;
}
}
if((snake.x[0]<40)||(snake.x[0]>610)||(snake.y[0]<40)||(snake.y[0]>470))
snake.life=0;
if (snake.life==0)
break;
if(snake.x[0]==food.x&&snake.y[0]==food.y)
{
setcolor(0);
rectangle(food.x,food.y,food.x-10,food.y-10);
snake.x[snake.node]=-10;snake.y[snake.node]=-10;
snake.node++;
food.empty=1;
snake.score++;
}
setcolor(0);
rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]-10,snake.y[snake.node-1]-10);
setcolor(RED);
for(i=snake.node-1;i>0;i--)
{
snake.x[i]=snake.x[i-1];
snake.y[i]=snake.y[i-1];
}
switch(snake.direction)
{
case 1: snake.x[0]+=10;break;
case 2: snake.x[0]-=10;break;
case 3: snake.y[0]-=10;break;
case 4: snake.y[0]+=10;break;
}
for(i=0;i<snake.node;i++)
rectangle(snake.x[i],snake.y[i],snake.x[i]-10,snake.y[i]-10);
Sleep(300);
}
if (snake.life==0)
break;
char direction;
direction=getch();
switch((int)direction)
{
case 100: if(snake.direction!=2) snake.direction=1;break;
case 97: if(snake.direction!=1) snake.direction=2;break;
case 119: if(snake.direction!=4) snake.direction=3;break;
case 115: if(snake.direction!=3) snake.direction=4;break;
}
if ((int)direction==113)
break;
if ((int)direction==112)
{
system("pause");
}
}
}


void main()
{
initgraph(640, 550);
setcolor(WHITE);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
for(int i=30;i<=600;i+=10)
{
bar(i,20,i+10,30);
bar(i,450,i+10,460);
}
for(i=20;i<=450;i+=10)
{
bar(20,i,30,i+10);
bar(600,i,610,i+10);
}
gamepaly();
}