private void GameOver()
{
gameClock.Stop();
gameClock.Dispose();
MessageBox.Show("游戏结束", "游戏结束", MessageBoxButtons.OK);
clrForm = true;
Controls.Clear();//加了一句!就好了。******
this.Invalidate();
MainMenu();
}
好运!!!!!!!!!!!!
{
gameClock.Stop();
gameClock.Dispose();
MessageBox.Show("游戏结束", "游戏结束", MessageBoxButtons.OK);
clrForm = true;
Controls.Clear();//加了一句!就好了。******
this.Invalidate();
MainMenu();
}
好运!!!!!!!!!!!!
解决方案 »
- 设计时实现“文档大纲”
- winfrom或者ASP.NET 做windows桌面效果
- httpHandlers 怎么用?
- #if 跟if有什么区别,什么时候用#if?
- 求VS2005用TableAdapter怎样增,删,改 DataGridView里面选中的信息?
- c# 中 有 using microsoft.windowsCE.Forms ??ms的例子中有这句,可是编译不过????????
- 关于 .NET 框架 程序设计 这本书
- 请问,注册了这个事件,listbox的选定项就不能用键盘控制了呢
- 如问题
- 菜鸟请教~~~在c#中制作的工程所生成的exe文件能否在win9x下运行?
- 怪事了。C:\llq\例子\DataBrow\Form_datatext.cs(71,24): warning CS1595: 已在多处定义“DataBrow.Form_datatext”;使用“C:\llq\例子\
- 在OnPaint过程中绘图时,dotNet总是先用背景色擦除需要重画的区域,然后再用自己写的代码绘制。能否不让其擦除?即使区域很小,该区域仍
using System;
using System.Windows.Forms;
using System.Drawing;public class OldGame : Form
{
int ball_x, ball_y, p_x, p_y;
int x_vel=-5, y_vel=-5;
int score = 0;
int n=0;
Pen ballPen, paddlePen;
Graphics g;
Timer gameClock;
Random randNum;
Button play, quit;
Label lbl1, lblScore;
bool clrForm = false;
public OldGame()
{
MainMenu();
}
private void MainMenu()
{
//初始化窗体
n++;
this.Text = "弹球游戏";
this.Size = new Size(400, 400);
this.BackColor = Color.White;
this.MaximizeBox = false;
this.MaximumSize = new Size(400, 400);
this.MinimumSize = new Size(400, 400); //初始化控件
lbl1 = new Label();
lbl1.Text = "main menu";
lbl1.Size = new Size(300, 30);
lbl1.Location = new Point(165, 75);
lblScore = new Label();
lblScore.Text = "积分: " + score;
lblScore.ForeColor = Color.Green ;
lblScore.Size = new Size(60, 20);
lblScore.Location = new Point(175, 120);
play = new Button();
play.Text = "start game";
play.BackColor = Color.WhiteSmoke;
play.Size = new Size(100, 100);
play.Location = new Point(50, 150);
quit = new Button();
quit.Text = "exit game";
quit.BackColor = Color.WhiteSmoke;
quit.Size = new Size(100, 100);
quit.Location = new Point(250, 150);
eAdd();
//添加控件
Controls.Add(lbl1);
Controls.Add(lblScore);
Controls.Add(play);
Controls.Add(quit);
}
private void eAdd()
{
play.Click += new EventHandler(myPlay);
quit.Click += new EventHandler(myQuit);
}
private void eClear()
{
Controls.Remove(lbl1);
Controls.Remove(lblScore);
Controls.Remove(play);
Controls.Remove(quit);
quit.Dispose();
}
private void myPlay(Object sender, EventArgs e)
{
//在游戏进行期间移除控件
Controls.Remove(lbl1);
Controls.Remove(play);
Controls.Remove(quit);
Controls.Remove(lblScore);
gameClock = new Timer();
randNum = new Random();
ball_x = randNum.Next(295)+10;
ball_y = randNum.Next(295)+10;
p_x = 100;
p_y = 100;
ballPen = new Pen(Color.Blue , 1);
paddlePen = new Pen(Color.Blue,5);
score = 0;
//建立事件处理关联
if(n<2)
{
this.MouseMove += new MouseEventHandler(myMouse);
this.Paint += new PaintEventHandler(myPaint);
this.KeyUp += new KeyEventHandler(keyHandler);
}
clrForm = false; //启动游戏时钟
gameClock.Interval = 30;
gameClock.Tick += new EventHandler(gameLoop);
gameClock.Start();
}
//若用户按下Esc键,则结束游戏
private void keyHandler(Object sender, KeyEventArgs e)
{
if ( e.KeyCode == Keys.Escape )
GameOver();
}
//退出游戏
private void myQuit(Object sender, EventArgs e)
{
Application.Exit();
}
//根据鼠标位置移动挡板
private void myMouse(Object sender, MouseEventArgs e)
{
p_x = e.X;
p_y = e.Y;
}
//界面重画
private void myPaint(Object sender, PaintEventArgs e)
{
g = e.Graphics;
GameDraw();
}
//更新游戏界面
private void GameDraw()
{
if ( !clrForm )
{
g.DrawEllipse(ballPen, ball_x, ball_y, 5, 5);
g.DrawLine(paddlePen, p_x, 350, p_x+50, 350);
//g.DrawLine(paddlePen, 350, p_y, 350, p_y+50);
}
}
//更新游戏AI
private void gameLoop(Object sender, EventArgs e)
{
//实现镜面反弹
if ( ball_x <= 1 )
x_vel *= -1;
if ( ball_y <= 1 )
y_vel *= -1;
if (ball_x>= 375 )
x_vel *= -1;
//若小球被下挡板挡住,加分
if ( ball_y+5 >= 350 && ball_y+5 <= 355 && ball_x+3 >= p_x && ball_x+3 <= p_x+50 )
{
y_vel *= -1;
score++;
//lblScore.text ="7";
}
//小球越界,游戏结束
if ( ball_y >= 380 || ball_x >= 380)
GameOver();
//设定小球的下一位置
ball_x += x_vel;
ball_y += y_vel;
//进行界面重画
Invalidate();
}
//结束当前游戏
private void GameOver()
{
gameClock.Stop();
gameClock.Dispose();
MessageBox.Show("游戏结束", "游戏结束", MessageBoxButtons.OK);
//eClear();
clrForm = true;
Invalidate();
MainMenu();
} private void InitializeComponent()
{
//
// OldGame
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 273);
this.Name = "OldGame"; } public static void Main()
{
//启动程序
Application.Run(new OldGame());
}
}
//若用户按下Esc键,则结束游戏
private void keyHandler(Object sender, KeyEventArgs e)
{
if(blnTerminate==false)
if ( e.KeyCode == Keys.Escape )
{
blnTerminate=true;
GameOver();
}
}