日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

C#游戏实例:弹砖块游戏

發布時間:2023/12/20 C# 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C#游戏实例:弹砖块游戏 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

游戲的資源和設計參考了C#Windows游戲設計書中的案例,沒有太復雜的算法,適合入門者學習,就直接貼上資源文件和相關代碼了。

游戲資源:

在項目的bin/Debug文件夾下新建兩個文件夾GamePictures和GameSounds,分別保存項目的圖片和聲音資源,如下圖:



具體代碼:


主窗體類FrmBlock.cs

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices;namespace ShotBlock {public partial class FrmBlock : Form{Bitmap bmpBackground;Bitmap bmpPaddle;Bitmap bmpBlock;Bitmap bmpBall;static readonly Rectangle GamePaddleRect = new Rectangle(39, 1, 93, 23); //GamePaddleRect對應擋板在位圖Break,bmp中的位置和大小static readonly Rectangle GameBlockRect = new Rectangle(136, 1, 62, 27);static readonly Rectangle GameBallRect = new Rectangle(1, 1, 36, 36);static Rectangle PaddleRect;Point centerOfPaddle;int GameLevel = 1;//游戲關卡數int GameLives = 4;//玩家有5條命BlockClass[,] Blocks = new BlockClass[12, 14];[DllImport("winmm")]public static extern bool PlaySound(string szSound, int hMod, int i);bool isGameOver = false;bool bSounding = false;private void LoadPictures(){bmpBackground = LoadBitmap.LoadBmp("Background");Bitmap bmpBreak = LoadBitmap.LoadBmp("Break");bmpPaddle = bmpBreak.Clone(GamePaddleRect, bmpBreak.PixelFormat);bmpBlock = bmpBreak.Clone(GameBlockRect, bmpBreak.PixelFormat);bmpBall = bmpBreak.Clone(GameBallRect, bmpBreak.PixelFormat);}private void DrawGame(Graphics graphic){//畫背景圖graphic.DrawImage(bmpBackground, new Rectangle(0, 0, this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1));//顯示擋板graphic.DrawImage(bmpPaddle, new Rectangle(centerOfPaddle.X - 93 / 2, centerOfPaddle.Y, 93, 23));for(int row=0;row<12;row++)for (int col = 0; col < 14; col++){if (Blocks[row, col].IsBlock){graphic.DrawImage(bmpBlock, Blocks[row, col].BlockRect);}}graphic.DrawImage(bmpBall, BallClass.BallRect);graphic.DrawString("Level " + GameLevel.ToString(), new Font("Arial Black", 24), new SolidBrush(Color.White), new Point(5, 5));graphic.DrawString("Lives " + (GameLives + 1).ToString(), new Font("Arial Black", 24), new SolidBrush(Color.White), new Point(710, 5));if (isGameOver)graphic.DrawString("Game Over", new Font("Arial Black", 48), new SolidBrush(Color.White), new Point(240, 2800));}private void RandomBlockMap(){for(int row=0;row<12;row++)for (int col = 0; col < 14; col++){Blocks[row, col] = new BlockClass();if (GetRandom.GetRandomInt() < GameLevel) //GameLevel越大,是磚塊的可能性越大,難度越高{Blocks[row, col].IsBlock = true;Blocks[row, col].BlockRect = new Rectangle(62 * col, row * 27 + 60, 62, 27);}}}protected override void OnPaint(PaintEventArgs e){Bitmap bufferBmp = new Bitmap(this.ClientRectangle.Width - 1, this.ClientRectangle.Height - 1);Graphics g = Graphics.FromImage(bufferBmp);this.DrawGame(g);e.Graphics.DrawImage(bufferBmp, 0, 0);g.Dispose();base.OnPaint(e);}public FrmBlock(){InitializeComponent();}private void FrmBlock_Load(object sender, EventArgs e){this.LoadPictures();this.RandomBlockMap();centerOfPaddle.X = this.ClientRectangle.Width / 2;centerOfPaddle.Y = this.ClientRectangle.Height - 30;BallClass.BallRect = new Rectangle(centerOfPaddle.X - 36 / 2, centerOfPaddle.Y - 33, 36, 36);//BallClass.BallDirection = new Vector2(1.0f, -1.0f); this.TimerRefreshScreen.Start();}private void FrmBlock_MouseMove(object sender, MouseEventArgs e){if (e.X <= 93 / 2)centerOfPaddle.X = 93 / 2;else if (e.X >= this.ClientRectangle.Width - 93 / 2)centerOfPaddle.X = this.ClientRectangle.Width - 93 / 2;elsecenterOfPaddle.X = e.X;}private void TimerRefreshScreen_Tick(object sender, EventArgs e){BallClass.BallRect.X += (int)(BallClass.BallDirection.X * 4.0f);//8.0f彈球速度BallClass.BallRect.Y += (int)(BallClass.BallDirection.Y * 3.0f);//4.0fif (IsHit()&&!bSounding ){Play("Hit");bSounding = true;this.TimerAfterSound.Start();}if (IsKill()){Play("Kill");bSounding = true;this.TimerAfterSound.Start();}if (IsVictory()){Play("Victory");this.TimerRefreshScreen.Stop();this.TimerWait.Start();}this.IsGameOver();if (isGameOver){this.TimerRefreshScreen.Stop();}this.Invalidate();}private bool IsHit(){//判斷彈球是否和邊框及擋板碰撞//和邊框碰撞PaddleRect = new Rectangle(centerOfPaddle.X - 93 / 2, centerOfPaddle.Y, 93, 23);if (BallClass.BallRect.X <= 3) //左邊框{BallClass.BallDirection.X = -BallClass.BallDirection.X;return true;}if (BallClass.BallRect.X >= this.ClientRectangle.Width - 39) //右邊框{BallClass.BallDirection.X = -BallClass.BallDirection.X;return true;}if (BallClass.BallRect.Y <= 3) //上邊框{BallClass.BallDirection.Y = -BallClass.BallDirection.Y;return true;}//和擋板碰撞if (BallClass.BallRect.IntersectsWith(PaddleRect)){BallClass.BallDirection.Y = -BallClass.BallDirection.Y;return true;}return false;}private bool IsKill(){//和磚塊碰撞for(int row=0;row<12;row++)for(int col=0;col<14;col++){if (Blocks[row, col].IsBlock && Blocks[row, col].BlockRect.IntersectsWith(BallClass.BallRect)){//和磚塊的左右側面相撞if (Math.Abs(Blocks[row, col].BlockRect.X - BallClass.BallRect.X) < 6 || Math.Abs(Blocks[row, col].BlockRect.X - BallClass.BallRect.X + 93 - 36) < 6)BallClass.BallDirection.X = -BallClass.BallDirection.X;//和磚塊的上下面相撞elseBallClass.BallDirection.Y = -BallClass.BallDirection.Y;Blocks[row, col].IsBlock = false;return true;}}return false;}private void Play(string waveName){PlaySound(Application.StartupPath + "\\GameSounds\\" + waveName + ".wav", 0, 1);}private bool IsLost(){if (BallClass.BallRect.Y > this.ClientRectangle.Height - 18){BallClass.BallRect.Y = this.ClientRectangle.Height - GetRandom.GetRandomInt(35, 60);BallClass.BallDirection = new Vector2(1.0f, -1.0f);GameLives--;return true;}return false;}private bool IsVictory(){for(int row=0;row<12;row ++)for (int col = 0; col < 14; col++){if (Blocks[row, col].IsBlock)return false;}return true;}private void TimerWait_Tick(object sender, EventArgs e){this.TimerWait.Stop();GameLevel++;RandomBlockMap();BallClass.BallRect.X = this.ClientRectangle.Width / 2 - 18;BallClass.BallRect.Y = this.ClientRectangle.Height - GetRandom.GetRandomInt(35, 60);BallClass.BallDirection = new Vector2(1.0f, -1.0f);this.TimerRefreshScreen.Start();}private void IsGameOver(){if (GameLives < 0)isGameOver = true;}private void TimerAfterSound_Tick(object sender, EventArgs e){bSounding = false;this.TimerAfterSound.Stop();} } }



BallClass.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text;using System.Drawing ;namespace ShotBlock {class BallClass{public static Rectangle BallRect;public static Vector2 BallDirection;} }

BlockClass.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text;using System.Drawing ;namespace ShotBlock {class BlockClass{public Rectangle BlockRect;public bool IsBlock = false;} }

GetRandom.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ShotBlock {class GetRandom{//顯示磚塊public static Random globalRandomGenerator = GenerateNewRandomGenerator();public static Random GenerateNewRandomGenerator(){globalRandomGenerator = new Random((int)DateTime.Now.Ticks);return globalRandomGenerator;}public static int GetRandomInt(){//隨機產生0~9的一個整數return globalRandomGenerator.Next(10);}public static int GetRandomInt(int max){return globalRandomGenerator.Next(max);}public static int GetRandomInt(int min,int max){return globalRandomGenerator.Next(max - min) + min;}public static float GetRandomFloat(float min, float max){return (float)globalRandomGenerator.NextDouble() * (max - min) + min;}} }

LoadBitmap.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text; //添加一下兩個引用 using System.Drawing; //因為要使用Bitmap using System.Windows.Forms ; //因為要使用Application.StartupPathnamespace ShotBlock {class LoadBitmap{public static Bitmap LoadBmp(string bmpFileName){return new Bitmap(Application.StartupPath + "\\GamePictures\\" + bmpFileName + ".bmp");//Application.StartupPath是程序的可執行文件的路徑,也就是Debug文件下的ShotBlock.exe所在的路徑,即:...\bin\Debug}} }

Vector2.cs

using System; using System.Collections.Generic; using System.Linq; using System.Text;namespace ShotBlock {class Vector2{private float x, y;public Vector2(){ }public Vector2(float setX, float setY){x = setX;y = setY;}public float X{get { return x; }set { x = value; }}public float Y{get { return y; }set { y = value; }}//運算符+的重載public static Vector2 operator +(Vector2 v1, Vector2 v2){return new Vector2(v1.X + v2.X, v1.Y + v2.Y);}//運算符-的重載public static Vector2 operator -(Vector2 v1, Vector2 v2){return new Vector2(v1.X - v2.X, v1.Y - v2.Y);}//運算符*的重載(兩個向量的數量積)public static float operator *(Vector2 v1,Vector2 v2){return v1.X * v2.X + v1.Y * v2.Y;}//數量乘以向量public static Vector2 operator *(float f,Vector2 v){return new Vector2(f * v.X, f * v.Y);}//數量和向量相乘時可以不要求二者的次序public static Vector2 operator *(Vector2 v,float f){return new Vector2(f * v.X, f * v.Y);}//實例化Vector2的一個實例后,計算這個具體向量的模(長度)public float Moudle(){return (float)(Math.Sqrt(x * x + y * y));}} }

運行結果如圖:




總結

以上是生活随笔為你收集整理的C#游戏实例:弹砖块游戏的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。