专栏文章

easyx贪吃蛇

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mipyoikv
此快照首次捕获于
2025/12/03 20:06
3 个月前
此快照最后确认于
2025/12/03 20:06
3 个月前
查看原文
CPP
#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()){
			if(!(start.first+fx_s[fx[start.first][start.second]]>=1&&start.first+fx_s[fx[start.first][start.second]]<=30&&start.second+fx_s[fx[start.first][start.second]+4]>=1&&start.second+fx_s[fx[start.first][start.second]+4]<=30&&(mp[start.first+fx_s[fx[start.first][start.second]]][start.second+fx_s[fx[start.first][start.second]+4]]==0||mp[start.first+fx_s[fx[start.first][start.second]]][start.second+fx_s[fx[start.first][start.second]+4]]==2))){
				EndBatchDraw();
				cleardevice();
				settextstyle(100,0,"楷体");
				outtextxy(350-textwidth("GAME OVER")/2,350-textheight("GAME OVER")/2,"GAME OVER"); 
				Sleep(3000);
				return 0;
			}
			mp[start.first+fx_s[fx[start.first][start.second]]][start.second+fx_s[fx[start.first][start.second]+4]]=1;
			fx[start.first+fx_s[fx[start.first][start.second]]][start.second+fx_s[fx[start.first][start.second]+4]]=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==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];
			}
		}
		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 

评论

0 条评论,欢迎与作者交流。

正在加载评论...