for (a = a1; a < a2; a += PI / 360)
{
for (int r = 0; r < LIGHT_R; r++)
{
x = (int)(_x + r * cos(a));
y = (int)(_y + r * sin(a));
if (x <= 0 || x >= WIDTH || y <= 0 || y >= HEIGHT - 1)
break;
if (bufImage[y * WIDTH + x])//真 白色
break;
bufScreen[y * WIDTH + x] += 0x202000;//淡黄色
}
}
我在一个地方学的这个if语句,直接就可以判断这个点是否为白色,但是我调试以后得到的结果是,不管这个点是什么颜色,他的布尔值都是true,也就是说if语句下的break都会运行,这有什么办法解决呢?
DWORD* bufImage;
void GameInit()
{
loadimage(&image, L"mama.png.png",700,700);
bufScreen = GetImageBuffer();
bufImage = GetImageBuffer(&image);
之前的操作也就是定义这个指针,然后读取图像信息
备注:我使用的VS2019,easyx.h库,C++
这是源码:
#include<stdio.h>
#include<easyx.h>
#include<math.h>
#pragma comment(lib,"winmm.lib")
#define PI 3.1415926
#define WIDTH 700
#define HEIGHT 700
#define LIGHT_A (PI/3)
#define LIGHT_R 120
const POINT exitpt = { 660,680 };
IMAGE image;
POINT player;
POINT mouse;
bool isBegin;
DWORD* bufScreen;
DWORD* bufImage;
void GameInit()
{
loadimage(&image, L"mama.png.png",700,700);
bufScreen = GetImageBuffer();
bufImage = GetImageBuffer(&image);
player.x = 22;
player.y = 30;
/*player.x = 68;
player.y = 68;*/
mouse.x = 100;
mouse.y = 100;
isBegin = false;
}
void LithtDarw(int _x,int _y,double _a)
{
int x, y;
double a;
double a1 = _a - LIGHT_A / 2;
double a2 = _a + LIGHT_A / 2;
for (a = a1; a < a2; a += PI / 360)
{
for (int r = 0; r < LIGHT_R; r++)
{
x = (int)(_x + r * cos(a));
y = (int)(_y + r * sin(a));
if (x <= 0 || x >= WIDTH || y <= 0 || y >= HEIGHT - 1)
break;
if (bufImage[y * WIDTH + x])//真 白色
break;
bufScreen[y * WIDTH + x] += 0x202000;//淡黄色
}
}
}
void GameDraw()//绘制界面
{
BeginBatchDraw();
cleardevice();
if(!isBegin)//关灯
putimage(0, 0, &image);
outtextxy(exitpt.x, exitpt.y, L"EXIT");
solidcircle(player.x, player.y, 5);
if (isBegin)
{
double dx, dy, a;
dx = mouse.x - player.x;
dy = mouse.y - player.y;
if (dx == 0 && dy == 0)
a = 0;
else if (dx != 0 && dy != 0)
a = atan(dy / dx);
else if (dx == 0)
a = dy > 0 ? PI / 2 : PI * 3 / 2;
else
a = 0;
if (dx < 0)a += PI;
if (a < 0)a += PI * 2;
LithtDarw(player.x, player.y, a);
}
EndBatchDraw();
}
void UpdatePlayer()
{
if (GetAsyncKeyState(VK_LEFT))
{
int i = (player.y - 5)*WIDTH+(player.x - 6);//左上角的点
int j;
for (j = 0; j < 10; j++, i += WIDTH)//左边一列
{
if (bufImage[i])//是否为白色
break;
}
if (j == 10)
player.x--;
}
if (GetAsyncKeyState(VK_RIGHT))
{/*
int i = (player.x - 5) * WIDTH + (player.x - 6);
int j;
for (j = 0; j < 10; j++, i += WIDTH)
if (bufImage[i])
break;
if (j == 10)*/
player.x++;
}
if (GetAsyncKeyState(VK_UP))
{
/*int i = (player.y - 5) * HEIGHT + (player.y - 6);
int j;
for (j = 0; j < 10; j++, i += HEIGHT)
if (bufImage[i])
break;
if (j == 10)*/
player.y--;
}
if (GetAsyncKeyState(VK_DOWN))
{
/*int i = (player.y - 5) * HEIGHT + (player.y - 6);
int j;
for (j = 0; j < 10; j++, i += HEIGHT)
if (bufImage[i])
break;
if (j == 10)*/
player.y++;
}
ExMessage m;
while (peekmessage(&m, EX_MOUSE, PM_NOREMOVE))
{
m = getmessage(EX_MOUSE);
m.message == WM_MOUSEMOVE;
mouse.x = m.x;
mouse.y = m.y;
}
}
int main()
{
initgraph(WIDTH, HEIGHT);
GameInit();
int time[7] = { 1000,50,500,50,50,50,50 };
for (int i = 0; i < 7; i++)
{
if (i % 2 == 0)
GameDraw();
else
cleardevice();
Sleep(time[i]);
}
for (int i = 255; i >= 0; i -= 5)
{
for (int y = 0; y < HEIGHT; y++)
{
for (int x = 0; x < WIDTH; x++)
{
if (bufScreen[y * WIDTH + x] > 0)//不是黑色
{
bufScreen[y * WIDTH + x] -= 0x050505;
}
}
}
FlushBatchDraw();
Sleep(20);
}
PlaySound(L"lau.wav", NULL, SND_ASYNC | SND_FILENAME);
isBegin = true;
while (1)
{
GameDraw();
UpdatePlayer();
Sleep(20);//速度
}
GameDraw();
system("pause");
return 0;
}
效果就是一个迷宫游戏,开始闪烁几次后会关灯,玩家有一个手电筒,抵达终点