easyx贪吃蛇2.0
cyx1234
·
·
个人记录
#include<bits/stdc++.h>
#include<easyx.h>
#include<graphics.h>
#define int long long
using namespace std;
int mp[32][32],fx[32][32],cd=3;
int fx_s[9]={0,-1,1,0,0,0,0,-1,1};
pair<int,int> start,end;
bool time();
signed main(){
//fx:1w2s3a4d
srand(time(NULL));
mp[1][1]=mp[2][1]=mp[3][1]=1;
start.first=3;
start.second=1;
end.first=1;
end.second=1;
fx[1][1]=fx[3][1]=fx[2][1]=2;
initgraph(700,700,1);
bool flog=0;
while(1){
int ax,ay;
if(flog==0){
ax=rand()%30+1,ay=rand()%30+1;
while(mp[ax][ay]==1){
ax=rand()%30+1;
ay=rand()%30+1;
}
mp[ax][ay]=2;
flog=1;
}
setlinestyle(PS_SOLID,1);
setlinecolor(WHITE);
BeginBatchDraw();
for(int i=0;i<=30;i++){
line(50,i*20+50,650,i*20+50);
}
for(int i=0;i<=30;i++){
line(i*20+50,50,i*20+50,650);
}
for(int i=1;i<=30;i++){
for(int j=1;j<=30;j++){
if(mp[i][j]==1){
setfillcolor(RGB(243,114,120));
solidrectangle(50+j*20-20+2,50+i*20-20+2,50+j*20+20-20-2,50+i*20+20-20-2);
}
if(mp[i][j]==2){
setfillcolor(RGB(151,187,240));
solidrectangle(50+j*20-20+2,50+i*20-20+2,50+j*20+20-20-2,50+i*20+20-20-2);
}
}
}
ExMessage msg={0};
if(GetAsyncKeyState(VK_UP)||GetAsyncKeyState('W')&&mp[start.first+fx_s[1]][start.second+fx_s[1+4]]!=1){
fx[start.first][start.second]=1;
}
if(GetAsyncKeyState(VK_DOWN)||GetAsyncKeyState('S')&&mp[start.first+fx_s[2]][start.second+fx_s[2+4]]!=1){
fx[start.first][start.second]=2;
}
if(GetAsyncKeyState(VK_LEFT)||GetAsyncKeyState('A')&&mp[start.first+fx_s[3]][start.second+fx_s[3+4]]!=1){
fx[start.first][start.second]=3;
}
if(GetAsyncKeyState(VK_RIGHT)||GetAsyncKeyState('D')&&mp[start.first+fx_s[4]][start.second+fx_s[4+4]]!=1){
fx[start.first][start.second]=4;
}
if(time()){
int ywx,ywy;
ywx=start.first+fx_s[fx[start.first][start.second]];
ywy= start.second+fx_s[fx[start.first][start.second]+4];
if(ywx<1){
ywx=30;
}
if(ywx>30){
ywx=1;
}
if(ywy<1){
ywy=30;
}
if(ywy>30){
ywy=1;
}
if(mp[ywx][ywy]==1){
EndBatchDraw();
cleardevice();
settextstyle(100,0,"楷体");
outtextxy(350-textwidth("GAME OVER")/2,350-textheight("GAME OVER")/2,"GAME OVER");
Sleep(3000);
return 0;
}
ywx=start.first+fx_s[fx[start.first][start.second]];
ywy=start.second+fx_s[fx[start.first][start.second]+4];
if(ywx<1){
ywx=30;
}
if(ywx>30){
ywx=1;
}
if(ywy<1){
ywy=30;
}
if(ywy>30){
ywy=1;
}
fx[ywx][ywy]=fx[start.first][start.second];
int x=start.first,y=start.second;
start.first=start.first+fx_s[fx[x][y]];
start.second=start.second+fx_s[fx[x][y]+4];
if(start.first<1){
start.first=30;
}
if(start.first>30){
start.first=1;
}
if(start.second<1){
start.second=30;
}
if(start.second>30){
start.second=1;
}
mp[start.first][start.second]=1;
if(start.first==ax&&start.second==ay){
flog=0;
}else{
mp[end.first][end.second]=0;
x=end.first,y=end.second;
end.first=end.first+fx_s[fx[x][y]];
end.second=end.second+fx_s[fx[x][y]+4];
if(end.first<1){
end.first=30;
}
if(end.first>30){
end.first=1;
}
if(end.second<1){
end.second=30;
}
if(end.second>30){
end.second=1;
}
}
}
EndBatchDraw();
cleardevice();
}
}
bool time(){
static int time_js=clock();
int begin=clock();
if(begin-time_js>=200){
time_js=begin;
return true;
}
return false;
}
//原创:cyx1234