esayx:
贪吃蛇:
#include "iostream"
#include "cmath"
#include "conio.h"
#include "easyx.h"
#include "time.h"
#define NODE_WIDTH 40
using namespace std;
typedef struct {int x;int y;
}node; enum direction //蛇的方向,上下左右
{eUP,eDown,eLeft,eRight
};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, right, top, 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 == 0) {newhead.y--;}else if (direction == 1) {newhead.y++;}else if (direction == 2) {newhead.x--;}else if (direction == 3) {newhead.x++;}snake[0] = newhead;return tail;
}void changeDirection(enum direction* pD) {if (_kbhit()!=0) {char c = _getch();if (c == 'w') {if (*pD != eDown)*pD = eUP;}else if (c == 's') {if (*pD != eUP)*pD = eDown;}else if (c == 'a') {if (*pD != eRight)*pD = eLeft;}else if (c == 'd') {if (*pD != eLeft)*pD = eRight;}}
}node createFood(node* snake, int length) {node food;int i;while (1) {food.x = rand() % (800 / NODE_WIDTH); //[0,19]food.y = rand() % (600 / NODE_WIDTH); //[0,14]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, right, top, 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[i].x == snake[0].x && snake[i].y == snake[0].y)return true;}return false;
}void reset(node* snake, int* plength, enum direction* d) {snake[0] = { 5,7 };snake[1] = { 4,7 };snake[2] = { 3,7 };snake[3] = { 2,7 };snake[4] = { 1,7 };*plength = 5;*d = eRight;
}
int main() {enum direction d = eRight;node snake[100] = { {5,7},{4,7},{3,7},{2,7},{1,7} };int length = 5;initgraph(800, 600);setbkcolor(RGB(164, 225, 202));cleardevice();srand(unsigned int(time(NULL))); //食物随机node food = createFood(snake, length);while(1){cleardevice();paintGrid();paintsnake(snake, length);paintfood(food);Sleep(150);changeDirection(&d);node lastTail=snakeMove(snake, length, d);if (snake[0].x == food.x && snake[0].y == food.y) {if (length < 100) {snake[length] = lastTail;length++;}food = createFood(snake, length);}if (isGameOver(snake, length) == true){reset(snake, &length, &d);food = createFood(snake, length);}}getchar();closegraph();}
图像处理:
#include "iostream"
#include "cmath"
#include "conio.h"
#include "easyx.h"
#include "time.h"
#include "stdlib.h"
using namespace std;void putransparentImage(int x, int y, const IMAGE* mask, const IMAGE *img) {putimage(x, y, mask, SRCAND);putimage(x, y, img, SRCPAINT);//绘制透明图片的封装函数
}
int main() {//IMAGE对象:保存图片的各种信息//loadimage( //加载一个图片文件,将图片信息填充至IMAGE对象中//IMAGE* pDstImg, 待填充的IMAGE对象指针//LPCTSTR pImgFile, 图片的路径 // int nWidth=0, 图片的拉伸宽度,如果参数为0,使用原图宽度// int nHeight=0, 图片的拉伸高度,如果参数为0,使用原图高度// bool bResize=false 是否调整IMAGE的大小以适应图片 // );//putimage( //将image对象中的图片显示在窗体上//int dstX, //绘制位置的xy坐标//int dstY,//IMAGE *pSrcImg, //要绘制的IMAGE对象指针//DWORD dwRop =SRCCOPY //三元光栅操作码// );//三元光栅常用://SRCAND:目标图像=目标图像and源图像//SRCPAINT:目标图像=目标图像or源图像//默认情况下会用SRCCOPY:目标图像=源图像initgraph(1200, 480);setbkcolor(WHITE);cleardevice();IMAGE img;IMAGE img2;IMAGE img3;loadimage(&img, "./background.jpg");loadimage(&img2, "./mask.jpg");loadimage(&img3, "./bear.jpg");putimage(0, 0, &img);putimage(530, 180, &img2, SRCAND);putimage(530, 180, &img3, SRCPAINT);getchar();closegraph();//三元光栅操作码:剪影图片and绿色背景or黑背景熊主体=熊主题与背景 //(通过这样的方式就可以绘制出透明图片了)//剪影图片称为掩码
}
按键处理:
#include "iostream"
#include "cmath"
#include "conio.h"
#include "easyx.h"
#include "time.h"
#include "stdlib.h"
int main() {//EX_MOUSE; 鼠标消息//EX_KEY; 按键消息//EX_CHAR; 字符消息//EX_WINDOW; 窗口消息//ExMessage函数,创建消息容器//ExMessage msg;//如果两种及以上都想要:msg=getmessage(EX_MOUSE|EX_KEY);// ExMessage函数内部://鼠标消息交互://bool ctrl :1 ctrl是否按下//bool shift : 1 shift是否按下//bool lbutton :1 鼠标左键是否按下//bool mbutton :1 鼠标中键是否按下//bool rbutton :1 鼠标右键是否按下//short x; 鼠标的x轴坐标//short y; 鼠标的y轴坐标//short wheel; 鼠标滚轮滚动值,为120的倍数//键盘消息交互://BYTE vkcode; //按键的虚拟键码//BYTE scancode; //按键的扫描码//bool extended :1 //按键是否为扩充位//bool prevdown :1 //按键的前一个状态是否按下//字符消息的数据// TCHAR ch;//窗口消息的数据//WPARAM wParam//LPARAM lParam//鼠标:/*WM_MOUSEMOVE EX_MOUSE 鼠标移动消息。WM_MOUSEWHEEL 鼠标滚轮拨动消息。WM_LBUTTONDOWN 左键按下消息。WM_LBUTTONUP 左键弹起消息。WM_LBUTTONDBLCLK 左键双击消息。WM_MBUTTONDOWN 中键按下消息。WM_MBUTTONUP 中键弹起消息。WM_MBUTTONDBLCLK 中键双击消息。WM_RBUTTONDOWN 右键按下消息。WM_RBUTTONUP 右键弹起消息。WM_RBUTTONDBLCLK 右键双击消息。WM_KEYDOWN EX_KEY 按键按下消息WM_KEYUP 按键弹起消息。WM_CHAR EX_CHAR 字符消息。WM_ACTIVATE EX_WINDOW 窗口激活状态改变消息。WM_MOVE 窗口移动消息。WM_SIZE 窗口大小改变消息。*///键盘://WM_KEYDOWN; 按键按下消息//WM_KEYUP; 按键弹起消息initgraph(800, 600, EX_DBLCLKS); //EX_DBLCLKS:支持双击setbkcolor(WHITE);cleardevice();ExMessage msg;while (1) {msg = getmessage(EX_MOUSE);switch (msg.message){case WM_MOUSEMOVE:setfillcolor(BLACK);solidcircle(msg.x, msg.y, 2);break;case WM_LBUTTONDOWN:setfillcolor(RED);if (msg.ctrl == true)solidrectangle(msg.x - 10, msg.y - 10, msg.x + 10, msg.y + 10);else solidcircle(msg.x, msg.y, 10);break;case WM_RBUTTONDOWN:setfillcolor(BLUE);if (msg.ctrl == true)solidrectangle(msg.x - 10, msg.y - 10, msg.x + 10, msg.y + 10);else solidcircle(msg.x, msg.y, 10);break;}}}