GDI图形设备接口是Windows API提供给开发者处理窗口程序的函数接口。
绘图句柄HDC:
HDC hdc=GetDC(HWND hWnd);绘制直线:
MoveToEx(HDC hdc,int x,int y,LPPOINT preP);
这样,我们那可以绘制敌人和大嘴的形象。
绘制三种敌人:
void Enermy::Draw(HDC& hdc)//绘制敌人 { HPEN pen = ::CreatePen(0, 0, color); HPEN oldPen = (HPEN)SelectObject(hdc, pen); Arc(hdc, point.x - DISTANCE, point.y + DISTANCE, point.x + DISTANCE, point.y + DISTANCE, point.x + DISTANCE, point.y, point.x - DISTANCE, point.y);//半圆形的头 int const LEGLENTH = (DISTANCE) / (LEGCOUNTS); //根据帧数来绘制身体和腿部 if (frame % 2 == 0) { MoveToEx(hdc, point.x - DISTANCE, point.y, NULL); LineTo(hdc, point.x - DISTANCE, point.y + DISTANCE - LEGLENTH); MoveToEx(hdc, point.x + DISTANCE, point.y, NULL); LineTo(hdc, point.x + DISTANCE, point.y + DISTANCE - LEGLENTH); for (int i = 0; i < LEGCOUNTS; i++) { Arc(hdc, point.x - DISTANCE + i * 2 * LEGLENTH, point.y + DISTANCE - 2 * LEGLENTH, point.x - DISTANCE + (i + 1)*2 * LEGLENTH, point.y + DISTANCE, point.x - DISTANCE + i * 2 * LEGLENTH, point.y+DISTANCE-LEGLENTH, point.x - DISTANCE + (i + 1) * 2 * LEGLENTH, point.y + DISTANCE - LEGLENTH ); } } else { MoveToEx(hdc, point.x - DISTANCE, point.y, NULL); LineTo(hdc, point.x - DISTANCE, point.y + DISTANCE); MoveToEx(hdc, point.x + DISTANCE, point.y, NULL); LineTo(hdc, point.x + DISTANCE, point.y + DISTANCE); //从左往右绘制腿部 MoveToEx(hdc, point.x - DISTANCE, point.y+DISTANCE, NULL); LineTo(hdc, point.x - DISTANCE+LEGLENTH, point.y + DISTANCE - LEGLENTH); for (int i = 0; i < LEGCOUNTS - 1; i++) { Arc(hdc, point.x - DISTANCE + (1 + i * 2) * LEGLENTH, point.y + DISTANCE - 2 * LEGLENTH, point.x - DISTANCE + (3 + i * 2) * LEGLENTH, point.y + DISTANCE, point.x - DISTANCE + (1 + i * 2) * LEGLENTH, point.y + DISTANCE - LEGLENTH, point.x - DISTANCE + (3 + i * 2) * LEGLENTH, point.y + DISTANCE - LEGLENTH ); } MoveToEx(hdc, point.x + DISTANCE, point.y + DISTANCE, NULL); LineTo(hdc, point.x + DISTANCE - LEGLENTH, point.y + DISTANCE - LEGLENTH); } //根据方向绘制眼睛 int R = DISTANCE / 5; switch (tw) { case UP: Ellipse(hdc, point.x - 2 * R, point.y - 2 * R, point.x, point.y); Ellipse(hdc, point.x, point.y - 2 * R, point.x + 2 * R, point.y); break; case DOWN: Ellipse(hdc, point.x - 2 * R, point.y, point.x, point.y + 2 * R); Ellipse(hdc, point.x, point.y, point.x + 2 * R, point.y + 2 * R); break; case LEFT: Ellipse(hdc, point.x - 3 * R, point.y - R, point.x - R, point.y + R); Ellipse(hdc, point.x - R, point.y - R, point.x + R, point.y + R); break; case RIGHT: Ellipse(hdc, point.x - R, point.y - R, point.x + R, point.y + R); Ellipse(hdc, point.x + R, point.y - R, point.x + 3 * R, point.y + R); break; } frame++;//准备绘制下一帧 SelectObject(hdc, oldPen); DeleteObject(pen); return; }
绘制大嘴:
void PacMan::Draw(HDC& memDC) { if (tw == OVER) { } else if (frame % 2 == 0)//第4帧与第2帧动画 { int x1 = 0, x2 = 0, y1 = 0, y2 = 0; int offsetX = DISTANCE / 2 + D_OFFSET; int offsetY = DISTANCE / 2 + D_OFFSET; switch (tw) { case UP: x1 = point.x - offsetX; x2 = point.x + offsetX; y2 = y1 = point.y - offsetY; break; case DOWN: x1 = point.x + offsetX; x2 = point.x - offsetX; y2 = y1 = point.y + offsetY; break; case LEFT: x1 = x2 = point.x - offsetX; y1 = point.y + offsetY; y1 = point.y - offsetY; break; case RIGHT: x1 = x2 = point.x + offsetX; y1 = point.y - offsetY; y1 = point.y + offsetY; break; } Arc(memDC, point.x - DISTANCE, point.y - DISTANCE, point.x + DISTANCE, point.y + DISTANCE, x1, y1, x2, y2); MoveToEx(memDC, x1,y1,NULL); LineTo(memDC, point.x, point.y); LineTo(memDC, x2,y2); } else if (frame % 3 == 0) { Ellipse(memDC, point.x - DISTANCE, point.y - DISTANCE, point.x + DISTANCE, point.y + DISTANCE); } else { int x1 = 0, x2 = 0, y1 = 0, y2 = 0; switch (tw) { case UP: x1 = point.x - DISTANCE; x2 = point.x + DISTANCE; y2 = y1 = point.y; break; case DOWN: x1 = point.x + DISTANCE; x2 = point.x - DISTANCE; y2 = y1 = point.y; break; case LEFT: x2 = x1 = point.x; y1 = point.y + DISTANCE; y2 = point.y - DISTANCE; break; case RIGHT: x2 = x1 = point.x; y1 = point.y - DISTANCE; y2 = point.y + DISTANCE; break; } Arc(memDC, point.x - DISTANCE, point.y - DISTANCE, point.x + DISTANCE, point.y + DISTANCE, x1, y1, x2, y2); MoveToEx(memDC, x1, y1, NULL); LineTo(memDC, point.x, point.y); LineTo(memDC, x2, y2); } frame++;//绘制下一帧 }
因为有3种敌人,所以我们可以给他们赋予颜色来识别:
//敌人颜色绘制 RedOne(int x, int y) :Enermy(x, y) { color = RGB(255, 0, 0); } BlueOne(int x, int y) : RedOne(x, y) { color = RGB(0, 0, 255); } YellowOne(int x, int y) : RedOne(x, y) { color = RGB(200, 200, 100); }
@ Mayuko