赞
踩
#include<stdio.h>
#include<easyx.h>
int main()
{
initgraph(800, 600);//创建窗口
setbkcolor(RGB(162, 92, 10));//设置棕色
cleardevice();//涂满背景
//耳朵的阴影部分
setfillcolor(RGB(130,69,4));//设置颜色
solidcircle(200, 130, 90);
solidcircle(600, 130, 90);//两个圆形
//耳朵部分
//与阴影部分重合,留下下面一个类似半月牙型的阴影
setfillcolor(RGB(255,178,50));
solidcircle(200,120,90);
solidcircle(600,120,90);
//耳朵高光
//由于耳朵的阴影,本体,高光的圆形都是一样大的
//为了避免高光部分遮住阴影部分,需要对高光部分进行剪切
HRGN leftEarClip =CreateEllipticRgn(110, 30, 290, 210);
HRGN rightEarClip =CreateEllipticRgn(510, 30, 690, 210);//创建两个圆形区域
HRGN earsClip =CreateRectRgn(0, 0, 0, 0);//创建一个空目标
CombineRgn(earsClip, leftEarClip, rightEarClip, RGN_OR);//使用并集组合区域
setcliprgn(earsClip);//设置剪切区域
setfillcolor(RGB(243,154,2));
solidcircle(200,130,90);
solidcircle(600,130,90);//绘制圆形高光
//耳朵里面
//由于上面已经设置了剪切区域,所以不用担心会绘制到外面
setfillcolor(RGB(255,178,50));
solidcircle(200,210,90);
solidcircle(600,210,90);
//释放剪切区域
DeleteObject(leftEarClip);
DeleteObject(rightEarClip);
DeleteObject(earsClip);//释放前面的三个区域
setcliprgn(NULL);//将合并区域置空
//绘制头部
setfillcolor(RGB(255,178,50));
solidcircle(400,300,250);
//头顶高光
//由于绘制高光依然会超出头的位置,所以外面需要剪切
HRGN headClip = CreateEllipticRgn(150, 50, 650, 550);
setcliprgn(headClip);//设置剪切区域
setfillcolor(RGB(243,154, 2));
solidcircle(400, 320, 250);//绘制高光
DeleteObject(headClip);
setcliprgn(NULL);//释放剪切区域
//眼睛
setfillcolor(RGB(51,34,8));
solidcircle(275,300,25);
solidcircle(525,300,25);
//嘴部
//有两个椭圆组成
//棕色椭圆作为下部阴影
setfillcolor(RGB(130,69,4));
solidellipse(310,385,490,485);
setfillcolor(WHITE);
solidellipse(310,380,490,480);
//鼻子
setfillcolor(RGB(51,34,8));
solidcircle(400,420,15);
//胡须
//两条线段
setlinestyle(PS_SOLID, 5); //样式
setlinecolor(RGB(51,34,8)); //颜色
line(400,420, 370, 450);
line(400,420,430,450);//线段
getchar();
closegraph();
return 0;
}
ps:在win11里进行键盘交互时可能会出现控制台窗口在前按键盘才有反应,此时需要在命令框上点出属性,其中有一个下拉选项是:让windows决定。把这个选项改为控制台。
#include<stdio.h>
#include<easyx.h>
#include<conio.h>
#include <graphics.h>
int main()
{
//创建窗口
initgraph(800,600);
setorigin(400,300);
setaspectratio(1, -1);
setbkcolor(RGB(164,225,202));
cleardevice();
//小球移动
int x = 0, y = 0;//圆心坐标
int vx = 5, vy = 5;//小球移动速度
int r = 40;//小球大小
//挡板参数
int barLeft, barTop, barRight, barBottom;
barLeft = -150;
barRight = 150;
barTop = -280;
barBottom = -300;
while (1)
{
cleardevice();
solidcircle(x,y,r);
solidrectangle(barLeft, barTop, barRight, barBottom);//每一帧中绘制挡板
//通过键盘对挡板进行交互
if (_kbhit() != 0)//是否按下键盘
{
char c = _getch();//从键盘里获取字符
if (c == 'a')
{
if (barLeft > -400)
{
barLeft -= 20;
barRight -= 20;
}
}
else if (c == 'd')
{
if (barRight < 400)
{
barLeft += 20;
barRight += 20;
}
}
}
Sleep(40);//每绘制一帧暂停40毫秒
//碰到边缘反弹
if (y >= 300-r)
vy = -vy;
if (x <= -400+r || x >= 400-r)
vx = -vx;
if (barLeft <= x && x <= barRight && y <= barTop + r)//撞击挡板
vy = -vy;
x += vx;
y += vy;
if (y <= -300)//游戏结束,复位
{
x = 0, y = 0;
vx = 5, vy = 5;
barLeft = -150;
barRight =150 ;
barBottom = -280;
barTop = -300;
}
}
getchar();
closegraph();
return 0;
}
#include<stdio.h>
#include<easyx.h>
#include<Windows.h>
#include<conio.h>
#include<stdbool.h>
#include<time.h>
#define NODE_WIDTH 40 //每个网格的宽度
//4个方向
enum direction
{
eUp,
eDown,
eLeft,
eRight
};
//每个网格的坐标
typedef struct {
int x;
int y;
}node;
//绘制网格
void paintGrid()
{
for (int x = 0; x <= 800; x += NODE_WIDTH)
{
line(x, 0, x, 600);//纵向线段
}
for (int y = 0; y <= 600; y += NODE_WIDTH)
{
line(0, y, 800, y);//横向线段
}
}
//绘制蛇
void paintSnake(node*snake,int n)
{
int left, top, right, bottom;
for (int i = 0; i < n; i++)
{
left = snake[i].x * NODE_WIDTH;
top = snake[i].y * NODE_WIDTH;
right = (snake[i].x + 1) * NODE_WIDTH;
bottom = (snake[i].y + 1) * NODE_WIDTH;
solidrectangle(left, top, right, bottom);
}
}
//移动
node snakeMove(node* snake, int length, int direction)
{
node tail = snake[length - 1];//记录尾节点方便之后吃食物长大
for (int i = length - 1; i> 0; i--)//将前一个节点覆盖后一个节点
{
snake[i] = snake[i - 1];
}
node newHead;
newHead=snake[0];//储存新的节点
//新节点方向
if (direction == eUp)
{
newHead.y--;
}
else if (direction == eDown)
{
newHead.y++;
}
else if (direction == eLeft)
{
newHead.x--;
}
else
{
newHead.x++;
}
snake[0] = newHead;//更新头节点
return tail;//为吃掉食物长大做准备
}
//更改蛇移动方向
void changeDrection(enum direction*pD)
{
if (_kbhit() != 0)
{
char c = _getch();
switch(c)
{
case 'w':
if(*pD!=eDown)
*pD = eUp;
break;
case 's':
if(*pD!=eUp)
*pD = eDown;
break;
case 'a':
if(*pD!=eRight)
*pD = eLeft;
break;
case 'd':
if(*pD!=eLeft)
*pD = eRight;
break;
}
}
}
//随机生成食物
node creatFood(node* snake, int length)
{
node food;
while (1)
{
//首先随机创建食物
food.x = rand() % (800 / NODE_WIDTH);
food.y = rand() % (600 / NODE_WIDTH);
//判断生成的食物是否与蛇重叠
int i;
for(i=0;i<length;i++)
{
if (snake[i].x == food.x && snake[i].y == food.y)
{
break;
}
}
if (i < length)//如果重叠就再重新生成
continue;
else
break;
}
return food;
}
//绘制食物
void paintFood(node food)
{
int left, top, right,bottom;
left = food.x * NODE_WIDTH;
top = food.y * NODE_WIDTH;
right = (food.x + 1) * NODE_WIDTH;
bottom = (food.y + 1) * NODE_WIDTH;
setfillcolor(YELLOW);
solidrectangle(left, top, right, bottom);
setfillcolor(WHITE);
}
bool isGameOver(node* snake, int length)
{
//撞墙
if (snake[0].x < 0 || snake[0].x>800 / NODE_WIDTH)
return true;
if (snake[0].y < 0 || snake[0].y>600 / NODE_WIDTH)
return true;
//吃到自身
for (int i = 1; i < length; i++)
{
if (snake[0].x == snake[i].x && snake[0].y == snake[i].y)
return true;
}
return false;
}
int main()
{
//创建控制台
initgraph(800,600);
setbkcolor(RGB(164,225,202));
cleardevice();
//制定网格
paintGrid();
//绘制蛇的节点
node snake[100] = { {5,7},{4,7},{3,7},{2,7},{1,7} };//初始节点
int length = 5;//蛇的长度
enum direction d = eRight;//设置初始方向
//第一次生成食物
srand(unsigned int(time(NULL)));
node food = creatFood(snake,length);
//移动蛇节点
while (1)
{
cleardevice();//清空窗体
paintGrid();//绘制网格
paintSnake(snake, length);//绘制蛇节点
paintFood(food);//绘制食物
Sleep(500);
changeDrection(&d);//改变方向
node lastTail=snakeMove(snake, length, d);//移动蛇
//判断是否吃到食物
if (snake[0].x == food.x && snake[0].y == food.y)
{
if (length < 100)//我们规定蛇最长99
{
snake[length] = lastTail;
length++;//蛇长长
}
food = creatFood(snake, length);//创建新的食物
}
//游戏是否结束
if (isGameOver(snake,length))
break;
}
printf("游戏结束");
closegraph();
return 0;
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。