【程式設計】【C#】乒乓遊戲
			1.先將表單視窗放到最大,並將標題列隱藏
WindowState –> Maximized
FormBorderStyle –> None
 
2.2個pictureBox元件,一個是球,一個是球拍/棒
 請自行Google適當的圖 (或僅設背景顏色)
3.程式一開始時,把球拍放在畫面的最下方 (表單底部)
4.使用滑鼠控制球拍的左右移動
請自行Google適當的圖 (或僅設背景顏色)
3.程式一開始時,把球拍放在畫面的最下方 (表單底部)
4.使用滑鼠控制球拍的左右移動
 
 
 程式碼-1
程式碼-1
		 請自行Google適當的圖 (或僅設背景顏色)
3.程式一開始時,把球拍放在畫面的最下方 (表單底部)
4.使用滑鼠控制球拍的左右移動
請自行Google適當的圖 (或僅設背景顏色)
3.程式一開始時,把球拍放在畫面的最下方 (表單底部)
4.使用滑鼠控制球拍的左右移動
 
 
 程式碼-1
程式碼-1
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;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int 水平位移量 = 5;
        int 垂直位移量 = 5;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            球.Left += 水平位移量;
            球.Top += 垂直位移量;
            if ((球.Top + 球.Height) >= this.ClientSize.Height || 球.Top <= 0) 垂直位移量 = 垂直位移量 * -1; //變化方向,縱向
            if ((球.Left + 球.Width) >= this.ClientSize.Width || 球.Left <= 0) 水平位移量 = 水平位移量 * -1; //變化方向,橫向
        }
    }
}
 
程式碼-2,加上棒子,並可用滑鼠來移動
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;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        int 水平位移量 = 5;
        int 垂直位移量 = 5;
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            棒子.Top = this.ClientSize.Height - 棒子.Height;
            棒子.Left = this.ClientSize.Width/2 - 棒子.Width/2;
            //以上二行將棒子移到畫面的正中、下方處
            Cursor.Hide();//將游標隱藏起來
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            球.Left += 水平位移量;
            球.Top += 垂直位移量;
            if ((球.Top + 球.Height) >= this.ClientSize.Height || 球.Top <= 0) 垂直位移量 = 垂直位移量 * -1; //變化方向,縱向
            if ((球.Left + 球.Width) >= this.ClientSize.Width || 球.Left <= 0) 水平位移量 = 水平位移量 * -1; //變化方向,橫向
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            棒子.Left = Cursor.Position.X; //棒子的水平位置 與 游標的水平位置聯動
        }
    }
}
 
程式碼-3,棒子擊球,並判斷球沒接到後的處理方式
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;
namespace bounceBall
{
    public partial class Form1 : Form
    {
        int 水平位移量 = 10, 垂直位移量 = 10; //控制水平與垂直的移動距離(跳5個像素)
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //水平方向方向的判斷,右邊碰觸的判斷 + 左邊碰觸的判斷
            if ((球.Location.X + 球.Width) > this.ClientSize.Width || 球.Location.X < 0)
                水平位移量 = 0 - 水平位移量; //正變負,負變正,也就是變換方向
            //上方碰觸的判斷
            if (球.Location.Y < 0)
                垂直位移量 = 0 - 垂直位移量; //正變負,負變正,也就是變換方向
            //棒子打中球的判斷
            if (球.Left >= 棒子.Left && 球.Left <= (棒子.Left + 棒子.Width) &&
                球.Top >= 棒子.Top - 球.Height)
            {
                垂直位移量 = 0 - 垂直位移量; //正變負,負變正,也就是變換方向
            }
            //底下是判斷球是否碰到畫面底碰部,是的話,遊戲結束
            if (球.Top + 球.Height >=  this.ClientSize.Height) //球碰到底部
            { //沒接到球…
                timer1.Enabled = false; //關閉計時器,不要讓球繼續跑
                球.Left = 10; 球.Top = 10; //讓球回到點(10, 10)
                Cursor.Show();//讓游標顯示出來
                MessageBox.Show("你出局了!");
                this.Close(); //結束遊戲程式
            }
            //將水平位移量, 垂直位移量加到pictureBox1的X與Y
            球.Location = new Point(球.Location.X + 水平位移量, 球.Location.Y + 垂直位移量);
            /* 或者用Left和Top來移動球的位置
              球.Left += 水平位移量;
              球.Top += 垂直位移量;
             */
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            棒子.Top = this.ClientSize.Height - 棒子.Height;
            棒子.Left = this.ClientSize.Width / 2 - 棒子.Width / 2;
            //以上二行將棒子移到畫面的正中、下方處
            Cursor.Hide();//將游標隱藏起來
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            棒子.Left = Cursor.Position.X; //棒子的水平位置 與 游標的水平位置聯動
        }
    }
}
 
球出現在隨機的水平位置 +運動方向的改變 (左下方、正下方、右下方)
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;
namespace bounceBall
{
    public partial class Form1 : Form
    {
        int 水平位移量 = 10, 垂直位移量 = 10; //控制水平與垂直的移動距離(跳5個像素)
        int 分數 = 0;
        public Form1()
        {
            InitializeComponent();
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            //水平方向方向的判斷,右邊碰觸的判斷 + 左邊碰觸的判斷
            if ((球.Location.X + 球.Width) > this.ClientSize.Width || 球.Location.X < 0)
                水平位移量 = 0 - 水平位移量; //正變負,負變正,也就是變換方向
            //上方碰觸的判斷
            if (球.Location.Y < 0)
                垂直位移量 = 0 - 垂直位移量; //正變負,負變正,也就是變換方向
            //棒子打中球的判斷
            if (球.Left >= 棒子.Left && 球.Left <= (棒子.Left + 棒子.Width) &&
                球.Top >= 棒子.Top - 球.Height)
            {
                水平位移量 += 5;
                垂直位移量 += 5;
                垂直位移量 = 0 - 垂直位移量; //正變負,負變正,也就是變換方向
                分數 = 分數 + 1;
                label1.Text = "分數:" + 分數;
            }
            //底下是判斷球是否碰到畫面底部,是的話,遊戲....
            if (球.Top + 球.Height >=  this.ClientSize.Height) //球碰到底部
            { //沒接到球…
                timer1.Enabled = false; //關閉計時器,不要讓球繼續跑
                GameOver.Visible = true;
            }
            //將水平位移量, 垂直位移量加到pictureBox1的X與Y
            球.Location = new Point(球.Location.X + 水平位移量, 球.Location.Y + 垂直位移量);
            /* 或者用Left和Top來移動球的位置
              球.Left += 水平位移量;
              球.Top += 垂直位移量;
             */
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            棒子.Top = this.ClientSize.Height - 棒子.Height;
            棒子.Left = this.ClientSize.Width / 2 - 棒子.Width / 2;
            GameOver.Left = (this.ClientSize.Width - GameOver.Width) / 2;
            GameOver.Top = (this.ClientSize.Height - GameOver.Height) / 2;
            GameOver.Visible = false;
            //將球依隨機的水平位置來放
            Random Rnd = new Random(); //新建一個隨機數(Random)物件
            球.Left = Rnd.Next(10, this.ClientSize.Width - 球.Width);
            int 球的運動方式 = Rnd.Next(1, 4 ); //1:左下,2:正下方跑,3:右下(原本的方式)
            if (球的運動方式 == 1) 水平位移量 = 0 - 水平位移量;
            if (球的運動方式 == 2) 水平位移量 = 0 ;
            //if (球的運動方式 == 3)  原本的方式
            //以上二行將棒子移到畫面的正中、下方處
            Cursor.Hide();//將游標隱藏起來
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            棒子.Left = Cursor.Position.X; //棒子的水平位置 與 游標的水平位置聯動
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F1)
            {
                水平位移量 = 10;
                垂直位移量 = 10;
                分數 = 0;
                label1.Text = "分數:" + 分數;
                球.Top = 20;
                //將球依隨機的水平位置來放
                Random Rnd = new Random(); //新建一個隨機數(Random)物件
                球.Left = Rnd.Next(10, this.ClientSize.Width - 球.Width);
                int 球的運動方式 = Rnd.Next(1, 4); //1:左下,2:正下方跑,3:右下(原本的方式)
                if (球的運動方式 == 1) 水平位移量 = 0 - 水平位移量;
                if (球的運動方式 == 2) 水平位移量 = 0;
                if (球的運動方式 == 3) 水平位移量 = 10;
                GameOver.Visible = false;
                timer1.Enabled = true;
            }
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }
    }
}
 
參考資料:
【程式設計】【C#】乒乓遊戲
 					


