int main() { initgraph(640, 480); //设置视口矩形区域为(200,100) - (330, 130) //最后一个参数为1表示出了这个区域的图形会被裁剪 //后面所绘画的图形的原点坐标(0,0),会映射到(200,100) setviewport(200, 100, 330, 130, 1); //画一些文字,注意文字会因区域被裁剪的效果 setcolor(EGERGB(0x0, 0xFF, 0x0)); setfontbkcolor(RGB(0x80, 0x00, 0x80)); setfont(18, 0, "宋体"); outtextxy(0, 0, "Hello EGE Graphics"); setbkmode(TRANSPARENT); outtextxy(0, 20, "Hello EGE Graphics"); //还原视口 setviewport(0, 0, getwidth(), getheight(), 1); outtextxy(0, 0, "Hello EGE Graphics"); getch(); closegraph(); return 0; }
inline void LifeGamev2::render() { static Grayscale scale; for (unsigned y = 0; y < ROW; ++y) for (unsigned x = 0; x < COL; ++x) { scale = (*puniverse)[y][x]; setfillcolor(EGERGB(scale, scale, scale)); bar(12 * x, 12 * y, 12 * x + 12, 12 * y + 12); // (xl, yt, xr, yb); } setcolor(EGERGB(0x0, 0xFF, 0xFF)); setfontbkcolor(EGERGB(0x00, 0x00, 0x00)); setfont(FONT_SIZE, 0, "Consolas"); std::string fps = "current fps: " + std::to_string(SPEED); fps += " step: " + std::to_string(STEP); fps += " powered by Chen Ji :)"; outtextxy(0, 12 * ROW, fps.c_str()); }
void gameplay() { snake.x[1]=300; //这个是蛇头 snake.y[1]=100; snake.x[2]=310; snake.y[2]=100; snake.direction=2; //默认向左边运动 snake.life=1; snake.node=2; randomize(); food.life=1; food.x=50; food.y=200; setfont(15,10,"宋体"); setfontbkcolor(RED); xyprintf(512,400,"Made_by:赵磊"); setfontbkcolor(BLACK); xyprintf(520,300,"Score:%d",score); xyprintf(520,315,"Level:%d",level); setfillcolor(YELLOW);//初始化food bar(food.x,food.y,food.x+10,food.y-10); setfillcolor(WHITE);//初始化snake for(i=1;i<=snake.node;i++) bar(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10); delay(1000); while(1) { while(!kbhit()) //当没有按键按下时候 { //算法food if(food.life==0) { food.x=rand()%400+90; food.y=rand()%400+40; while(food.x%10!=0) food.x++; while(food.y%10!=0) food.y++; food.life=1; } if(food.life==1) { setfillcolor(YELLOW); bar(food.x,food.y,food.x+10,food.y-10); } zhongjianx=snake.x[snake.node]; zhongjiany=snake.y[snake.node]; //算法snake for(i=snake.node;i>1;i--) { snake.x[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; } switch(snake.direction) { case 1:snake.x[1]+=10;break; //右边 case 2:snake.x[1]-=10;break; //左边 case 3:snake.y[1]+=10;break; //下边 case 4:snake.y[1]-=10;break; //上边 } //撞墙死亡 if(snake.x[1]==10||snake.x[1]==500||snake.y[1]==10||snake.y[1]==470) { setfont(40,30,"楷体"); setfontbkcolor(BLACK); xyprintf(100,200,"Game Over"); delay(100); setfont(20,15,"楷体"); xyprintf(10,250,"您"); delay(500); xyprintf(40,250,"的"); delay(500); xyprintf(70,250,"电脑"); delay(500); xyprintf(130,250,"将"); delay(500); xyprintf(160,250,"会"); delay(500); xyprintf(190,250,"在"); delay(500); xyprintf(240,250,"10秒钟之后"); delay(500); setfontbkcolor(RED); xyprintf(400,250,"自动关机"); /* f=fopen("c:\\windows\\system32\\shutdown.exe","r"); system("c:\\windows\\system32\\shutdown.exe -s -t 5"); system("c:\\windows\\system32\\shutdown.exe -s -t 5"); system("c:\\windows\\system32\\shutdown.exe -s -t 5"); system("c:\\windows\\system32\\shutdown.exe -s -t 5"); */ } if(snake.x[1]==food.x&&snake.y[1]==food.y) // 如果蛇吃到了果子 { setfillcolor(RED);//清除food bar(food.x,food.y,food.x+10,food.y-10); food.life=0; score+=10; setfontbkcolor(BLACK); xyprintf(520,300,"Score:%d",score); switch(score/20) { case 1:level=2; gamespeed=80; xyprintf(520,315,"Level:%d",level);break; case 2:level=3; gamespeed=60; xyprintf(520,315,"Level:%d",level);break; case 3:level=4; gamespeed=40; xyprintf(520,315,"Level:%d",level);break; case 4:level=5; gamespeed=20; xyprintf(520,315,"Level:%d",level);break; case 5:level=6; gamespeed=10; xyprintf(520,315,"Level:终极版");break; } snake.node++; snake.x[snake.node]=zhongjianx; snake.y[snake.node]=zhongjiany; for(i=4;i<=snake.node;i++) { if(snake.x[1]==snake.x[i]&&snake.y[1]==snake.y[i]) { snake.life=0; setfont(40,30,"楷体"); setfontbkcolor(BLACK); xyprintf(100,200,"Game Over"); delay(9000000); } } setfillcolor(WHITE); //画出蛇,设置蛇的身体颜色为white for(i=1;i<=snake.node;i++) { bar(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10); } } else //没吃到果子 { for(i=4;i<=snake.node;i++) { if(snake.x[1]==snake.x[i]&&snake.y[1]==snake.y[i]) { snake.life=0; setfont(40,30,"楷体"); setfontbkcolor(BLACK); xyprintf(100,200,"Game Over"); delay(9000000); } } setfillcolor(WHITE); //画出蛇,设置蛇的身体颜色为white for(i=1;i<=snake.node;i++) { bar(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10); } setfillcolor(BLACK);//抹去snake的最后一节 bar(zhongjianx,zhongjiany,zhongjianx+10,zhongjiany-10); } rectangle(10,10,500,470); delay(gamespeed); } if(kbhit()) { key=getch(); if(key==LEFT && snake.direction!=1) snake.direction=2; if(key==RIGHT && snake.direction!=2) snake.direction=1; if(key==UP && snake.direction!=3) snake.direction=4; if(key==DOWN && snake.direction!=4) snake.direction=3; if(key==' ') { key=43; while(1) { while(!kbhit()) delay(50); if(kbhit()) zanting=getch(); if(zanting==' ') break; } } } } }