PDA

View Full Version : سوال: بازي پازل



farzaneh_22
سه شنبه 15 مرداد 1387, 14:05 عصر
سلام
مي خوام با سي شارپ بازي پازل بسازم اما نميدونم چطوري بايد قطعه هاي پازلو رو فرم اجرا حركت بدم
يعني كدشو چطوري بنويسم توي چه رويدادي بنويسم كه عكسا قابل حركت دادن باشن

asefy2008
سه شنبه 15 مرداد 1387, 14:31 عصر
سلام
using System;
using System.Collections;
using System.Drawing;
using System.Windows.Forms;
// allows 2 players to play chess
public partial class ChessGame : Form
{
private ArrayList chessTile = new ArrayList(); // for tile images
private ArrayList chessPieces = new ArrayList(); // for chess pieces
private int selectedIndex = -1; // index for selected piece
private int[ , ] board = new int[ 8, 8 ]; // board array
private const int TILESIZE = 75; // chess tile size in pixels
// default constructor
public ChessGame()
{
// Required for Windows Form Designer support
InitializeComponent();
} // end constructor
// load tile bitmaps and reset game
private void ChessGame_Load( object sender, EventArgs e )
{
// load chess board tiles
chessTile.Add( Bitmap.FromFile( @"images\lightTile1.png" ) );
chessTile.Add( Bitmap.FromFile( @"images\lightTile2.png" ) );
chessTile.Add( Bitmap.FromFile( @"images\darkTile1.png" ) );
chessTile.Add( Bitmap.FromFile( @"images\darkTile2.png" ) );
ResetBoard(); // initialize board
Invalidate(); // refresh form
} // end method ChessGame_Load
// initialize pieces to start and rebuild board
private void ResetBoard()
{
int current = -1;
ChessPiece piece;
Random random = new Random();
bool light = false;
int type;
chessPieces.Clear(); // ensure empty arraylist
// load whitepieces image
Bitmap whitePieces =
( Bitmap ) Image.FromFile( @"images\whitePieces.png" );
// load blackpieces image
Bitmap blackPieces =
( Bitmap ) Image.FromFile( @"images\blackPieces.png" );
// set whitepieces to be drawn first
Bitmap selected = whitePieces;
// traverse board rows in outer loop
for ( int row = 0; row <= board.GetUpperBound( 0 ); row++ )
{
// if at bottom rows, set to black pieces images
if ( row > 5 )
selected = blackPieces;
// traverse board columns in inner loop
for ( int column = 0;
column <= board.GetUpperBound( 1 ); column++ )
{
// if first or last row, organize pieces
if ( row == 0 || row == 7 )
{
switch ( column )
{
case 0:
case 7: // set current piece to rook
current = ( int ) ChessPiece.Types.ROOK;
break;
case 1:
case 6: // set current piece to knight
current = ( int ) ChessPiece.Types.KNIGHT;
break;
case 2:
case 5: // set current piece to bishop
current = ( int ) ChessPiece.Types.BISHOP;
break;
case 3: // set current piece to king
current = ( int ) ChessPiece.Types.KING;
break;
case 4: // set current piece to queen
current = ( int ) ChessPiece.Types.QUEEN;
break;
} // end switch
// create current piece at start position
piece = new ChessPiece( current,
column * TILESIZE, row * TILESIZE, selected );
chessPieces.Add( piece ); // add piece to arraylist
} // end if
// if second or seventh row, organize pawns
if ( row == 1 || row == 6 )
{
piece = new ChessPiece(
( int ) ChessPiece.Types.PAWN,
column * TILESIZE, row * TILESIZE, selected );
chessPieces.Add( piece ); // add piece to arraylist
} // end if
type = random.Next( 0, 2 ); // determine board piece type
if ( light ) // set light tile
{
board[ row, column ] = type;
light = false;
}
else // set dark tile
{
board[ row, column ] = type + 2;
light = true;
}
} // end for loop for columns
light = !light; // account for new row tile color switch
} // end for loop for rows
} // end method ResetBoard
// display board in form OnPaint event
private void ChessGame_Paint( object sender, PaintEventArgs e )
{
Graphics graphicsObject = e.Graphics; // obtain graphics object
graphicsObject.TranslateTransform( 0, 24 ); // adjust origin
for ( int row = 0; row <= board.GetUpperBound( 0 ); row++ )
{
for ( int column = 0;
column <= board.GetUpperBound( 1 ); column++)
{
// draw image specified in board array
graphicsObject.DrawImage(
( Image ) chessTile[ board[ row, column ] ],
new Point( TILESIZE * column, ( TILESIZE * row ) ) );
} // end for loop for columns
} // end for loop for rows
} // end method ChessGame_Paint
// return index of piece that intersects point
// optionally exclude a value
private int CheckBounds( Point point, int exclude )
{
Rectangle rectangle; // current bounding rectangle
for ( int i = 0; i < chessPieces.Count; i++ )
{
// get piece rectangle
rectangle = GetPiece( i ).GetBounds();
// check if rectangle contains point
if ( rectangle.Contains( point ) && i != exclude )
return i;
} // end for
return -1;
} // end method CheckBounds
// handle pieceBox paint event
private void pieceBox_Paint(
object sender, System.Windows.Forms.PaintEventArgs e )
{
// draw all pieces
for ( int i = 0; i < chessPieces.Count; i++ )
GetPiece( i ).Draw( e.Graphics );
} // end method pieceBox_Paint
// handle pieceBox MouseDown event
private void pieceBox_MouseDown(
object sender, System.Windows.Forms.MouseEventArgs e )
{
// determine selected piece
selectedIndex = CheckBounds( new Point( e.X, e.Y ), -1 );
} // end method pieceBox_MouseDown
// if piece is selected, move it
private void pieceBox_MouseMove(
object sender, System.Windows.Forms.MouseEventArgs e )
{
if ( selectedIndex > -1 )
{
Rectangle region = new Rectangle(
e.X - TILESIZE * 2, e.Y - TILESIZE * 2,
TILESIZE * 4, TILESIZE * 4 );
// set piece center to mouse
GetPiece( selectedIndex ).SetLocation(
e.X - TILESIZE / 2, e.Y - TILESIZE / 2 );
pieceBox.Invalidate( region ); // refresh region
} // end if
} // end method pieceBox_MouseMove
// on mouse up deselect piece and remove taken piece
private void pieceBox_MouseUp( object sender, MouseEventArgs e )
{
int remove = -1;
// if chess piece was selected
if ( selectedIndex > -1 )
{
Point current = new Point( e.X, e.Y );
Point newPoint = new Point(
current.X - ( current.X % TILESIZE ),
current.Y - ( current.Y % TILESIZE ) );
// check bounds with point, exclude selected piece
remove = CheckBounds( current, selectedIndex );
// snap piece into center of closest square
GetPiece( selectedIndex ).SetLocation( newPoint.X, newPoint.Y );
selectedIndex = -1; // deselect piece
// remove taken piece
if ( remove > -1 )
chessPieces.RemoveAt( remove );
} // end if
pieceBox.Invalidate(); // ensure artifact removal
} // end method pieceBox_MouseUp
// helper function to convert
// ArrayList object to ChessPiece
private ChessPiece GetPiece( int i )
{
return ( ChessPiece ) chessPieces[ i ];
} // end method GetPiece
// handle NewGame menu option click
private void newGameItem_Click(
object sender, System.EventArgs e )
{
ResetBoard(); // reinitialize board
Invalidate(); // refresh form
} // end method newGameItem_Click
} // end class ChessGame
این یک برنامه شطرنج هست از کتاب deitelاگر به قسمت های pieceBox_MouseUpوpieceBox_MouseMoveوpieceBox_Mou seDownنگاه کنید ایده خوبی هست