#1 ブロック崩し Breakout

ソースファイル一式 source code
01-Breakout.zip

MITライセンスです。


Breakout.cs
// #1 ブロック崩し Breakout 2017/11/20 T.Umezawa using System; using System.Collections.Generic; class Block { static System.Drawing.SolidBrush sSB = new System.Drawing.SolidBrush( System.Drawing.Color.Yellow ); int mX, mY; public Block( int x, int y ) { mX = x; mY = y; } public void draw( System.Drawing.Graphics g ) { g.FillRectangle( sSB, mX - 30, mY - 15, 60, 30 ); } public bool isCollision( int x, int y ) { return( Math.Abs( mX - x ) <= 35 && Math.Abs( mY - y ) <= 20 ); } public bool isReflectX( int x, int y ) { return( Math.Abs( mX - x ) > Math.Abs( mY - y ) * 2 ); } } class Breakout : System.Windows.Forms.Form { System.Timers.Timer mTimer = new System.Timers.Timer(); System.Drawing.SolidBrush mSB = new System.Drawing.SolidBrush( System.Drawing.Color.White ); System.Drawing.Font mFont = new System.Drawing.Font( "MS Gothic", 32 ); int mPlayerX = 480; int mBallX = 480, mBallY = 640; int mBallDX = -5, mBallDY = -5; List mLBlock = new List(); int mScore; protected override void OnLoad( EventArgs e ) { ClientSize = new System.Drawing.Size( 960, 720 ); Left = 62; Top = 20; BackColor = System.Drawing.Color.Black; DoubleBuffered = true; for( int y = 60; y < 200; y += 35 ){ for( int x = 60; x < 940; x += 65 ){ mLBlock.Add( new Block( x, y ) ); } } mTimer.Elapsed += new System.Timers.ElapsedEventHandler( onMyTimer ); mTimer.Interval = 10; mTimer.Start(); } protected override void OnPaint( System.Windows.Forms.PaintEventArgs e ) { System.Drawing.Graphics g = e.Graphics; foreach( Block b in mLBlock ){ b.draw( g ); } g.FillRectangle( mSB, mPlayerX - 50, 650, 100, 25 ); g.FillRectangle( mSB, mBallX - 5, mBallY - 5, 10, 10 ); g.DrawString( "SCORE " + mScore, mFont, mSB, 0, 0 ); if( mLBlock.Count == 0 ){ g.DrawString( "GAME CLEAR!", mFont, mSB, 330, 350 ); } if( mBallY > 720 ){ g.DrawString( "GAME OVER", mFont, mSB, 330, 350 ); } } protected override void OnMouseMove( System.Windows.Forms.MouseEventArgs e ) { mPlayerX = e.X; base.OnMouseMove( e ); } void onMyTimer( object sender, System.Timers.ElapsedEventArgs e ) { //Console.WriteLine( e.SignalTime ); if( mLBlock.Count == 0 ){ return; } mBallX += mBallDX; mBallY += mBallDY; if( mBallX <= 5 || mBallX >= 955 ){ mBallDX = -mBallDX; } if( mBallY <= 5 ){ mBallDY = -mBallDY; } int d = mBallX - mPlayerX; int m = Math.Abs( d ); if( mBallY >= 650 && mBallY <= 670 && m < 50 ){ mBallDX = d; mBallDY = m - 55; int s = Math.Max( 2, 200 / ( mScore + 20 ) ); mBallDX /= s; mBallDY /= s; } for( int i = mLBlock.Count - 1; i >= 0; i-- ){ if( !mLBlock[ i ].isCollision( mBallX, mBallY ) ){ continue; } if( mLBlock[ i ].isReflectX( mBallX, mBallY ) ){ mBallDX = -mBallDX; mBallX += mBallDX; }else{ mBallDY = -mBallDY; mBallY += mBallDY; } mLBlock.Remove( mLBlock[ i ] ); mScore++; } Invalidate(); } static void Main() { System.Windows.Forms.Application.Run( new Breakout() ); } }


[戻る]
管理人:T.Umezawa