win32 框架 射线输出

本文详细介绍了使用C++进行游戏开发中动态线条绘制与小球轨迹模拟的技术实现。通过实例代码展示了如何创建并更新线条结构,控制线条两端的动态变化,以及如何基于鼠标位置和按键状态来模拟小球的发射与反弹效果。同时,文章还涉及了游戏帧处理、输入响应及图形输出等关键环节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include "..\\Frame\\Frame.h"
#include "vector2.h"
#include"game_input.h"
#include<math.h>
#include<Windows.h>
#include<vector>
#include<time.h>
struct xian
{
	Vector2 a ;
	Vector2 b ;
	Vector2 c ;
	Vector2 d ;
	Vector2 dira ;
	Vector2 dird ;
	xian(Vector2 x,Vector2 y):a(x),d(y)
	{
		b.x = -10 ;
		b.y = -10 ;
		c.x = -10 ;
		c.y = -10 ;
	}
};

std::vector<xian> go_ing ;//
Vector2 ballPos(400,300);  //小球位置 ---------
Vector2 M_mouse;
Vector2 huax(0,0);
Vector2 dir1 ;
Vector2 dir2 ;
Vector2 t1(-10.0f,-10.0f);
Vector2 t2(-10.0f,-10.0f);
int count = 0 ;
bool fa = false ;
bool cun = false ;
void GameInit()
{
	GameFrame* frame = GameFrame::GetFrame();
	CGameAudio* audio = frame->GetAudio();
}

void GameRun()
{
	GameFrame* frame = GameFrame::GetFrame();
	CGameOutput* out = frame->GetOutput();
	CGameInput* in = frame->GetInput();
	CGameAudio* audio = frame->GetAudio();
	
	//画线----------------------------
	for(std::vector<xian>::iterator it = go_ing.begin();it != go_ing.end() ;it++)
	{
		(*it).a += (*it).dira ;
		(*it).d += (*it).dird ;
		//判断头点状态
		if((*it).a.x < 0 || (*it).a.x > 800)
		{
			(*it).dira.x = -(*it).dira.x ;
			(*it).c.x = (*it).b.x ;
			(*it).c.y = (*it).b.y ;
			(*it).a.x < 0 ? ((*it).b.x = 0 ): ((*it).b.x = 800);
			(*it).b.y = (*it).a.y ;
		}
		if((*it).a.y < 0 || (*it).a.y > 600)
		{
			(*it).dira.y = -(*it).dira.y ;
			(*it).c.x = (*it).b.x ;
			(*it).c.y = (*it).b.y ;
			(*it).b.x = (*it).a.x ;
			(*it).b.y = (*it).a.y ;
		}
		//判断尾点状态
		if((*it).d.x < 0 || (*it).d.x > 800)
		{
			(*it).dird.x = -(*it).dird.x ;
			if((*it).c.x > -1)
			{
				(*it).c.x = -10 ;
			}
			else
			{
				if((*it).b.x > -1)
					(*it).b.x = -10 ;
			}
		}
		if((*it).d.y < 0 || (*it).d.y > 600)
		{
			(*it).dird.y = -(*it).dird.y ;
			if((*it).c.x > -1)
			{
				(*it).c.x = -10 ;
			}
			else
			{
				if((*it).b.x > -1)
					(*it).b.x = -10 ;
			}
		}


		//画出每条线------------------------------
		if((*it).b.x > -1)
		{
			out->DrawLine((*it).b.x,(*it).b.y, (*it).a.x , (*it).a.y ,0xffda) ;
			if((*it).c.x > -1) 
			{
				out->DrawLine((*it).c.x,(*it).c.y, (*it).b.x, (*it).b.y ,0xff00) ;
				out->DrawLine((*it).d.x,(*it).d.y,(*it).c.x, (*it).c.y ,0xff00) ;
			}
			else
			{
				out->DrawLine((*it).d.x,(*it).d.y, (*it).b.x, (*it).b.y ,0xbb11ff) ;
			}
		}
		else
			out->DrawLine((*it).d.x,(*it).d.y, (*it).a.x,(*it).a.y ,0xff0000);
	}

	int px ,py ;
	in->GetCursor(&px,&py);
	Vector2 mousePos(px,py);
	Vector2 paoguan = mousePos - ballPos ;
	Vector2	biao = paoguan.Normalize()*10 ;
	if((in->GetKeyState(VK_LBUTTON) == _KS_CU)&&fa == false )
	{
		fa = true ;
		huax = ballPos ;
		dir1 =biao ;
		dir2 =biao ;
	
	}
	if(fa)
	{
		count ++ ;
		if(count < 20)
		{
			huax += dir1 ;
			if(huax.x < 0 || huax.x > 800)
			{
				dir1.x = -dir1.x ;
				t2.x = t1.x ;
				t2.y = t1.y ;
				t1.x = huax.x ;
				t1.y = huax.y ;
			}
			if(huax.y < 0 || huax.y > 600)
			{
				dir1.y = -dir1.y ;
				t2.x = t1.x ;
				t2.y = t1.y ;
				t1.x = huax.x ;
				t1.y = huax.y ;
			}
		}
		else
		{
			xian abc(huax,ballPos);
			abc.dira = dir1 ;
			abc.dird = dir2 ;
			abc.b = t1 ;
			abc.c = t2 ;
			go_ing.push_back(abc);
			count = 0 ;
			huax.x = ballPos.x ;
			huax.y = ballPos.y ;
			fa = false ;
			t1.x = -10 ;
			t2.x = -10 ;
		}
		if(t1.x > -1)
		{
			out->DrawLine(t1.x,t1.y, huax.x , huax.y ,0xbb11ff) ;
			if(t2.x > -1) 
			{
				out->DrawLine(t2.x,t2.y, t1.x, t1.y ,0xbb11ff) ;
				out->DrawLine(ballPos.x,ballPos.y,t2.x, t2.y ,0xbb11ff) ;
			}
			else
			{
				out->DrawLine(ballPos.x,ballPos.y, t1.x, t1.y ,0xbb11ff) ;
			}
		}
		else
 			out->DrawLine(ballPos.x,ballPos.y, huax.x,huax.y ,0xbb11ff);
	}
	else
		out->DrawLine(ballPos.x,ballPos.y,ballPos.x + 5*biao.x,ballPos.y + 5*biao.y ,0xbb11ff) ;


	
	//绘制发射点
	out->DrawEllipse(ballPos.x -20,ballPos.y-20 ,ballPos.x +20,ballPos.y+20 ,0xffff00,0xbb11ff);
	//移动发射点---------------------
	if(!fa)
	{
		if((GetAsyncKeyState('W') & 0x8000) && ballPos.y > 0)ballPos.y -= 5 ;
		else if((GetAsyncKeyState('S') & 0x8000) && ballPos.y < 600)ballPos.y += 5 ;
		else if((GetAsyncKeyState('A') & 0x8000) && ballPos.x > 0)ballPos.x -= 5 ;
		else if((GetAsyncKeyState('D') & 0x8000) && ballPos.x < 800)ballPos.x += 5 ;
	}

	//退出游戏
	if (in->GetKeyState(VK_ESCAPE) == _KS_CU)
		frame->GameExit();
}

void GameEnd()
{

}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值