這是今天的傑作 乒乓球 按空白鍵開始
這個比較簡單 可是乒乓球跑起來卡卡的
目前請教高手中 順便附上程式碼
如果有人知道怎麼改 歡迎至訪客簿留言
按此下載
---------------------------------------------------------------------------------------------
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 WindowsFormsApplication2
{
public partial class Form1 : Form
{
Point Ball_pos = new Point();
int Ball_Width = 30;
Point Ball_Speed = new Point();
Pen myPen1 = new Pen(Color.Black, 5);
Pen myPen2 = new Pen(Color.Brown, 5);
int Margin_Left = 40;
int Margin_Right = 120;
int Bar_Len = 50;
int Y_Right = 0;
int Y_Left = 0;
Random rd = new Random();
public Form1()
{
InitializeComponent();
InitializeComponent();
Ball_pos.X = this.ClientSize.Width / 2;
Ball_pos.Y = this.ClientSize.Height / 2;
Ball_Speed = new Point(rd.Next(5) + 5, rd.Next(8) + 4);
Y_Left = this.ClientSize.Height / 2;
Y_Right = this.ClientSize.Height / 2;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
e.Graphics.FillEllipse(Brushes.Blue,
Ball_pos.X - Ball_Width / 2,
Ball_pos.Y - Ball_Width / 2,
Ball_Width, Ball_Width);
e.Graphics.DrawEllipse(Pens.Black,
Ball_pos.X - Ball_Width / 2,
Ball_pos.Y - Ball_Width / 2,
Ball_Width, Ball_Width);
e.Graphics.DrawLine(myPen1,
0 + Margin_Left, Y_Left - Bar_Len,
0 + Margin_Left, Y_Left + Bar_Len);
e.Graphics.DrawLine(myPen2,
this.ClientSize.Width - Margin_Right, Y_Right - Bar_Len,
this.ClientSize.Width - Margin_Right, Y_Right + Bar_Len);
}
private void timer1_Tick(object sender, EventArgs e)
{
Ball_pos.Y += Ball_Speed.Y;
if (Ball_pos.Y >= this.ClientSize.Height - Ball_Width / 2)
{
Ball_pos.Y = this.ClientSize.Height - Ball_Width / 2;
Ball_Speed.Y = -Ball_Speed.Y;
}
else if (Ball_pos.Y < Ball_Width / 2)
{
Ball_pos.Y = Ball_Width / 2;
Ball_Speed.Y = -Ball_Speed.Y;
}
Ball_pos.X += Ball_Speed.X;
if (Ball_pos.X < 200)
{
if (Ball_pos.Y > Y_Left)
Y_Left += 10 + rd.Next(11) - 5;
else if (Ball_pos.Y < Y_Left)
Y_Left -= 10 + rd.Next(11) - 5;
}
if (Ball_pos.X >= this.ClientSize.Width - Ball_Width / 2 - Margin_Right)
{
if (Ball_pos.Y <= Y_Right + Bar_Len + Ball_Width &&
Ball_pos.Y >= Y_Right - Bar_Len - Ball_Width)
{
Ball_pos.X = this.ClientSize.Width - Ball_Width / 2 - Margin_Right;
Ball_Speed.X = -Ball_Speed.X;
}
else
{
this.Invalidate();
timer1.Enabled = false;
MessageBox.Show("你輸了!");
}
}
else if (Ball_pos.X < Ball_Width / 2 + Margin_Left)
{
if (Ball_pos.Y <= Y_Left + Bar_Len + Ball_Width &&
Ball_pos.Y >= Y_Left - Bar_Len - Ball_Width)
{
Ball_pos.X = Ball_Width / 2 + Margin_Left;
Ball_Speed.X = -Ball_Speed.X;
}
else
{
this.Invalidate();
timer1.Enabled = false;
MessageBox.Show("你贏了!");
}
}
this.Invalidate();
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Y_Right = e.Y;
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Space)
{
Ball_pos.X = this.ClientSize.Width / 2;
Ball_pos.Y = this.ClientSize.Height / 2;
Ball_Speed = new Point(rd.Next(5) + 5, rd.Next(8) + 4);
Y_Left = this.ClientSize.Height / 2;
Y_Right = this.ClientSize.Height / 2;
timer1.Enabled = true;
}
}
}
}