贊助商連結


這次發現網路上貌似沒有視窗環境下而且還用c++編寫的打磚塊

找到的都是一堆vb或著c#的....

所以硬著頭皮利用visual c++弄一個很普通的供人參考

就不多說甚麼,有問題歡迎發問我會儘量解決您的迷惑喔~

底下這是執行檔如不能執行可逕行自最底下下載程式碼自行編譯

給大家鑑賞一下球的類別 (很糟糕請嘴下留情呀!!)

using namespace System::Drawing;
using namespace System::Media;
#pragma once

namespace Breakit{

	static int CBallDiameter=20;
	static int CheckPointsBrick=20;
	static int HitScore;
	static int GameBrickCount;
	static int GameLife;
	static int GameTime;
	static int PropsBall=0;

public ref class ball
{
	static SoundPlayer ^PongBallHit;
	static bool PongBallHitOver;

public:
	int WindowRight,WindowDown;
    int x,y;
	int previousUp,previousDown,previousLeft,previousRight;
	int xspeed,yspeed;
	System::Random^ rand;

	ball()
	{
	xspeed=12,yspeed=-13;
	rand=gcnew System::Random;
	PongBallHit=gcnew SoundPlayer("sound\\PongBallHit.wav");
	PongBallHitOver=false;
	}

	public:

	void updatePoint()
	{
		previousUp=y,previousDown=y+CBallDiameter,previousLeft=x,previousRight=x+CBallDiameter;
	}

	void moving(System::Windows::Forms::PictureBox^ ball)
	{
		x+=xspeed; 
		y+=yspeed;
		ball->Location = Point(x,y);
		if(y<=5){ yspeed*=-1; PongBallHitOver=true;}
		if(x<=5){ xspeed*=-1; PongBallHitOver=true;}
		if(x>=WindowRight-CBallDiameter-5){ xspeed*=-1; PongBallHitOver=true;}
		if(PongBallHitOver){PongBallHit->Play();		PongBallHitOver=false;	}
	}

	bool lose_ball(System::Windows::Forms::PictureBox^ racket)
	{
		if( y>racket->Location.Y ){return true;} else return false; 
	}

	void collision_Racket(System::Windows::Forms::PictureBox^ racket)
	{
		if(y+CBallDiameter>racket->Location.Y &&x>=racket->Location.X&&x<=racket->Location.X+racket->Size.Width) 
		{
			if(x+CBallDiameter/2>=racket->Location.X+(racket->Width/2)){ if(xspeed<0)	xspeed=rand->Next(-15,-11);  if(xspeed>0)  xspeed=rand->Next(11,15); } //右半邊
			if(x+CBallDiameter/2<racket->Location.X+(racket->Width/2)){ if(xspeed<0)	xspeed=rand->Next(-15,-11);  if(xspeed>0)  xspeed=rand->Next(11,15); } //左半邊
			yspeed*=-1; 
		} 
	}

	bool collision_Brick(System::Windows::Forms::PictureBox^  brick) {

			 if(x+CBallDiameter<brick->Location.X){return false;}
			 if(x>brick->Location.X+brick->Width){return false;}
			 if(y>brick->Location.Y+brick->Height){return false;}
			 if(y+CBallDiameter<brick->Location.Y){return false;}
			 if(y<=brick->Location.Y+brick->Height && previousUp>brick->Location.Y+brick->Height) { yspeed*=-1; return true; }
			 if(x+CBallDiameter>=brick->Location.X && previousRight<brick->Location.X) { xspeed*=-1; return true;}
			 if(x<=brick->Location.X+brick->Width && previousRight>brick->Location.X+brick->Width) { xspeed*=-1; return true;}
			 if(y+CBallDiameter>=brick->Location.Y && previousDown<brick->Location.Y ){ yspeed*=-1; return true;}
			 return false;
			 }

	void new_ball(System::Windows::Forms::PictureBox^ ball,System::Windows::Forms::PictureBox^ racket)
	{
		System::Random^ rand=gcnew System::Random;
		x=racket->Location.X+racket->Width/2;  //在球拍中心新增
		y=racket->Location.Y-CBallDiameter;
		xspeed=rand->Next(-11,11);
		ball->Location=Point(x,y);
		ball->Visible=true;
		if(yspeed>0){yspeed*=-1;} //假如球是往下改成往上
	}
	


};
}

 

windows form視窗程式的打磚塊提供初學者參考windows form視窗程式的打磚塊提供初學者參考

windows form視窗程式的打磚塊提供初學者參考windows form視窗程式的打磚塊提供初學者參考windows form視窗程式的打磚塊提供初學者參考  

 

文章標籤
創作者介紹

宇若彎彎

周宇若 發表在 痞客邦 PIXNET 留言(4) 人氣()


留言列表 (4)

發表留言
  • 悄悄話
  • 悄悄話
  • 悄悄話
  • 悄悄話
找更多相關文章與討論