C#游戏实例:弹砖块游戏
生活随笔
收集整理的這篇文章主要介紹了
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
GetRandom.cs
LoadBitmap.cs
Vector2.cs
運行結果如圖:
總結
以上是生活随笔為你收集整理的C#游戏实例:弹砖块游戏的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 开源3D播放器bino
- 下一篇: HTML连载77-3D播放器