我是新手,最近在做一个五子棋游戏.
我的黑棋子和白棋子是在一张图片上的,
于是我用:
Image image=new Image(@"C:\My Documents\...\1.bmp");
//绘黑棋子.
g.DrawImage(image,new RectAngle(x,y,width,height),new RectAngle(0,24,24,24),GraphicsUnit.Pixel);
//绘白棋子.
g.DrawImage(image,new RectAngle(x,y,width,height),new RectAgnle(24,24,24,24),GraphicsUnit.Pixel);可是,问题出现了.
那就是绘在指定位置的是矩形,怎样才能将指定位置的圆形绘到你指定的位置??
还有就是,这时的五子棋游戏如果运行,最小化,再恢复窗口,窗口上的棋子就全都看不见了..
5555555555,我很着急,不知道哪位能给我解答一下??
先谢谢了.
(最后再问一个,怎么播放WAV音频文件?)

解决方案 »

  1.   

    这里有一份资料,带源码。
    http://www.cnblogs.com/esshs/archive/2005/04/01/129824.html
      

  2.   

    smilnet(笨笨) 回贴不看贴.....hehe
    留名关注
      

  3.   

    最小化,再恢复窗口,窗口上的棋子就全都看不见了..
    ----------------------------------你是不是把绘制窗口背景的代码写在paint里面了..改个地方试试..
      

  4.   

    将你上面的代码写到下面这个覆写方法中protected override void OnPaint( PaintEventArgs e)
    {
        write your codes
         ......
         ......}
      

  5.   

    我没发现你说的那个问题,而且最好不要把代码放到OnPaint里面系统和你的眼睛都会受不了的
    我绘图的代码如下:
    Graphics g = Graphics.FromImage( imgBackGround ); //imgBackGround == 背景图片
    Image img = Image.FromFile( @"*********\1.bmp" );
    g.DrawImage( img, ............ );
      

  6.   

    我用的是:
            Graphics g=this.CreateGraphics();
            ......
    问题可能出在这里吧,我再试一下.还有这俩个问题怎么没高手给我解答一下啊??
    /那就是绘在指定位置的是矩形,怎样才能将指定位置的圆形绘到你指定的位置??
    /最后再问一个,怎么播放WAV音频文件?)
      

  7.   

    [DllImport("winmm.dll")]
     public static extern long PlaySound(String lpszName, long hModule, long dwFlags);
      

  8.   

    用Bitmap.MakeTransparent()方法
    比如图片是红色背景,中间一个白色的圆Bitmap bmp = new Bitma(filePath);
    bmp.MakeTransparent(Color.Red);
    e.Graphics.DrawImage(bmp,30,30,50,50);画出来的就只有一个白色的圆另外,两个棋子的图片最好缓存下来,不要每次Paint的时候都LoadFile
    播放声音:定义
    [DllImport("winmm.dll", EntryPoint="PlaySound")]
    public static extern int PlaySound (
    string lpszName,
    int hModule,
    int dwFlags
    );
    int SND_ASYNC = 0x1;
    int SND_FILENAME = 0x20000;
    int SND_LOOP = 0x8;使用
    PlaySound("sound1.wav",0,SND_ASYNC|SND_FILENAME|SND_LOOP);
      

  9.   

    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;namespace FiveChess
    {
    /// <summary>
    ///
    ///
    /// Form1 的摘要说明。
    /// </summary>
    public class Five : System.Windows.Forms.Form
    {
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;
    //定义棋盘变量..
    private int[,] chessTable=new int[225,3];//[k,0]存储k点状态,[k,1]存储k点x坐标,[k,2]存储k点y坐标..
    private int nextTurn;//转换下一步绘棋的颜色..
    private const int bTurn=1;
    private const int wTurn=2;
    private Stack chessIndex;
    private int[] counts=new int[225];
    private int count=-1;
    Bitmap chessbmp=new Bitmap(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\Five\WindowsApplication3\11.bmp");
    Cursor myCursor=new Cursor(@"C:\Documents and Settings\Administrator\My Documents\Visual Studio Projects\Five\WindowsApplication3\cur133.cur");
    public Five()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    this.HelpButton=false;
    this.MaximizeBox=false;
    this.Cursor=myCursor; chessIndex=new Stack();
    nextTurn=bTurn;
    for(int j=0;j<15;j++)
    for(int i=0;i<15;i++)
    {
    int k=i+15*j;
    chessTable[k,0]=0;
    chessTable[k,1]=50+44*i;
    chessTable[k,2]=50+44*j;
    } }
    protected override void OnPaint(PaintEventArgs e)
    {
    Graphics g=e.Graphics;
    Pen myPen=new Pen(Color.Blue,3);
    //绘制棋盘..
    for(int i=0;i<15;i++)
    {
    g.DrawLine(myPen,50,50+44*i,666,50+44*i);
    g.DrawLine(myPen,50+44*i,50,50+44*i,666);
    } Pen yourPen=new Pen(Color.Blue,2);
    g.DrawRectangle(yourPen,chessTable[48,1]-5,chessTable[48,2]-5,10,10);
    g.DrawRectangle(yourPen,chessTable[56,1]-5,chessTable[56,2]-5,10,10);
    g.DrawRectangle(yourPen,chessTable[168,1]-5,chessTable[168,2]-5,10,10);
    g.DrawRectangle(yourPen,chessTable[176,1]-5,chessTable[176,2]-5,10,10);
    g.DrawRectangle(yourPen,chessTable[112,1]-5,chessTable[112,2]-5,10,10);
    myPen.Dispose();
    yourPen.Dispose();
    g.Dispose();
    } protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
    {
    switch(e.Button)
    {
    case MouseButtons.Left:
    OnLButtonDown(new Point(e.X,e.Y));
    break;
    case MouseButtons.Right:
    if(e.Clicks==2)
    {
    OnReSetGame();
    break;
    }
    OnRButtonDown(new Point(e.X,e.Y));
    break;
    }
    base.OnMouseDown(e); } private void OnLButtonDown(Point p)
    {
    if(p.X>20&&p.X<706&&p.Y>20&&p.Y<706)
    {
    Graphics g=this.CreateGraphics(); int x=(p.X-25)/44;
    int y=(p.Y-25)/44;
    int i=x+15*y;
    Point pp=new Point(50+44*x,50+44*y);
    if(chessTable[i,0]==0)
    {
    if(nextTurn==bTurn)
    {

    DrawBlack(g,i);

    chessIndex.Push(bTurn);
    chessIndex.Push(pp);
    }
    else
    {
    DrawWhite(g,i);

    chessIndex.Push(wTurn);
    chessIndex.Push(pp);
    }
    g.Dispose();
    CheckGameResult(pp,nextTurn);
    }
    }
    } private void CheckGameResult(Point pp,int nextTurn)
    {
    int thisTurn=(nextTurn==bTurn)?wTurn:bTurn;
    int x=(pp.X-50)/44;
    int y=(pp.Y-50)/44;
    int left=x;
    int right=x;
    int top=y;
    int bottom=y;
    int lefttop=0;
    int leftbottom=0;
    int righttop=0;
    int rightbottom=0;
    int judgenumber=0;
    //检查x方向上是否是5个棋子.
    for(int i=0;i<x;i++)
    {
    if(chessTable[15*y+x-i-1,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    left--;
    }
    judgenumber=0;
    for(int j=x+1;j<15;j++)
    {
    if(chessTable[15*y+j,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    right++;
    }
    judgenumber=0;
    //判断right-left
    if((right-left+1)==5)
    {
    if(thisTurn==bTurn)
    MessageBox.Show("本局黑棋赢!","Notes");
    else
    MessageBox.Show("本局白棋赢!","Notes");
    OnReSetGame();
    } //检查y方向上是否是5个棋子..
    for(int i=0;i<y;i++)
    {
    if(chessTable[15*(y-i-1)+x,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    top--;
    }
    judgenumber=0;
    for(int j=y+1;j<15;j++)
    {
      

  10.   

    if(chessTable[15*j+x,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    bottom++;
    }
    judgenumber=0;
    //判断right-left
    if((bottom-top+1)==5)
    {
    if(thisTurn==bTurn)
    MessageBox.Show("本局黑棋赢!","Notes");
    else
    MessageBox.Show("本局白棋赢!","Notes");
    OnReSetGame();
    } //判断lefttop to rightbottom这个方向上是否是5个棋子.
    int position1=(x>=y)?y:x;
    lefttop=position1;
        rightbottom=position1;
    int position2=0;
    if(position1==x)
    position2=x+(15-y);
    else
    position2=y+(15-x); for(int i=0;i<position1;i++)
    {
    if(chessTable[15*(y-i-1)+x-i-1,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    lefttop--;
    }
    judgenumber=0;
    for(int j=position1+1;j<position2;j++)
    {
    if(chessTable[15*(y+j-position1)+x+j-position1,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    rightbottom++;
    }
    judgenumber=0;
    //判断rightbottom-lefttop.
    if((rightbottom-lefttop+1)==5)
    {
    if(thisTurn==bTurn)
    MessageBox.Show("本局黑棋赢!","Notes");
    else
    MessageBox.Show("本局白棋赢!","Notes");
    OnReSetGame();
    }
    position1=position2=0; //判断在righttop to leftbottom 方向上是否是5颗棋子.
    int z=14-x;
    position1=(z>=y)?y:z;
    lefttop=position1;
    rightbottom=position1;
    position2=0;
    if(position1==z)
    position2=z+(15-y);
    else
    position2=y+(15-z); for(int i=0;i<position1;i++)
    {
    if(chessTable[15*(y-i-1)+x+i+1,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    righttop--;
    }
    judgenumber=0;
    for(int j=position1+1;j<position2;j++)
    {
    if(chessTable[15*(y+j-position1)+x-j+position1,0]!=thisTurn)
    judgenumber=1;
    if(judgenumber==0)
    leftbottom++;
    }
    judgenumber=0;
    //判断leftbottom-righttop.
    if((leftbottom-righttop+1)==5)
    {
    if(thisTurn==bTurn)
    MessageBox.Show("本局黑棋赢!","Notes");
    else
    MessageBox.Show("本局白棋赢!","Notes");
    OnReSetGame();
    }
    position1=position2=0;
     
               //局面下满是为平棋..
    if(count==224)
    {
    MessageBox.Show("本局为平棋!","Notes");
    OnReSetGame();
    }
                //检查禁手..
    }
    private void OnReSetGame()
    {
    nextTurn=bTurn;
    for(int i=0;i<225;i++)
    {
    chessTable[i,0]=0;
    counts[i]=0;
    }
    count=-1;
    this.Invalidate();
    }//单击右键悔棋..
    private void OnRButtonDown(Point p)
    {
    if(count>=2)
    {
    Graphics g=this.CreateGraphics();
    this.Invalidate(new Rectangle(new Point(chessTable[counts[count],1]-21,chessTable[counts[count],2]-21),new Size(42,42)));
    chessTable[counts[count],0]=0;
    count--;
    this.Invalidate(new Rectangle(new Point(chessTable[counts[count],1]-21,chessTable[counts[count],2]-21),new Size(42,42)));
    chessTable[counts[count],0]=0;
    count--;
    chessTable[counts[count],0]=0;
    count--;
    if(nextTurn==bTurn)
    DrawWhite(g,counts[count+1]);
    else
    DrawBlack(g,counts[count+1]);
    g.Dispose();
    }
    }//绘黑棋.
    private void DrawBlack(Graphics g,int i)
    {
    Brush myBrush=new SolidBrush(Color.Black);
    Brush redBrush=new SolidBrush(Color.Red);
    if(chessTable[i,0]==0)
    {
    chessbmp.MakeTransparent(Color.Black);
    g.DrawImage(chessbmp,new Rectangle((chessTable[i,1]-21),(chessTable[i,2]-21),42,42),new Rectangle(0,24,24,24),GraphicsUnit.Pixel);
    g.FillEllipse(redBrush,(chessTable[i,1]-5),(chessTable[i,2]-5),10,10);
    chessTable[i,0]=bTurn;
    nextTurn=wTurn;
    count++;
    counts[count]=i;
    if(count==0&&i!=112)
    {
    MessageBox.Show("黑棋第一步要放在天元!","Notes");
    OnReSetGame();
    } }
    if(count>0)
    {
    g.DrawImage(chessbmp,new Rectangle((chessTable[counts[count-1],1]-21),(chessTable[counts[count-1],2]-21),42,42),new Rectangle(24,24,24,24),GraphicsUnit.Pixel);
    }
    }
    //绘白棋.
    private void DrawWhite(Graphics g,int i)
    {
                Brush myBrush=new SolidBrush(Color.White);
                Brush pointBrush=new SolidBrush(Color.Red);
    if(chessTable[i,0]==0)
    {
    chessbmp.MakeTransparent(Color.Black);
    g.DrawImage(chessbmp,new Rectangle((chessTable[i,1]-21),(chessTable[i,2]-21),42,42),new Rectangle(24,24,24,24),GraphicsUnit.Pixel);
                    g.FillEllipse(pointBrush,(chessTable[i,1]-5),(chessTable[i,2]-5),10,10);
    chessTable[i,0]=wTurn;
    nextTurn=bTurn;
    count++;
    counts[count]=i;
    }
    if(count>0)
    {
    g.DrawImage(chessbmp,new Rectangle((chessTable[counts[count-1],1]-21),(chessTable[counts[count-1],2]-21),42,42),new Rectangle(0,24,24,24),GraphicsUnit.Pixel);
    }
    }
    //实现电脑智能..
    void ComputerTurn()
    {

    }
                

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    } #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Five));
    // 
    // Five
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
    this.ClientSize = new System.Drawing.Size(1028, 749);
    this.Cursor = System.Windows.Forms.Cursors.Default;
    this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
    this.Name = "Five";
    this.Text = "五子连珠";
    this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
    this.Load += new System.EventHandler(this.Five_Load); }
    #endregion /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Five());
    } private void Five_Load(object sender, System.EventArgs e)
    {

    }
    }
    }
      

  11.   

    这里有一个现成的例子,看看希望对你有帮助。http://www.tiantiansoft.com/bbs/w1sev_2004-3/20042323132675074.rar