网上看到一个Tank游戏的代码,学习一下,可是运行后,按键盘老是叮叮的响,为什么?
网上看到一个Tank游戏的代码,学习一下,可是运行后,按键盘老是叮叮的响,为什么?代码如下:
Form1.cs
using System;--------------------编程问答-------------------- Tank.cs
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Tank
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool begin = false;//开关
Tank myTank = new Tank();//实例化 Tank
int enemyNumber = 5;
Enemy aEnemy = new Enemy(5);//实例化 Enemy
Fight aFight = new Fight();//实例化 Fight
int tankWay = 1;// Tank运动的方向
int cannonWay = 0;//炮的方向
int xMove = 0;//一个参数用来控制炮的方向
bool fire = false;//射击开关
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (begin)
{ //控制xMove的变化来控制炮的方向
if (xMove <= 20 && xMove >= -20)
myTank.cannonpoint = new Point(myTank.tankpoint.X + xMove,
myTank.tankpoint.Y - (int)Math.Sqrt(400 - xMove * xMove));
else if (xMove > 20 && xMove <= 60)
{
int x = 40 - xMove;
myTank.cannonpoint = new Point(myTank.tankpoint.X + x,
myTank.tankpoint.Y + (int)Math.Sqrt(400 - x * x));
if (xMove == 60)
xMove = -20;
}
else if (xMove < -20 && xMove >= -60)
{
int x = 40 + xMove;
myTank.cannonpoint = new Point(myTank.tankpoint.X - x,
myTank.tankpoint.Y + (int)Math.Sqrt(400 - x * x));
if (xMove == -60)
xMove = 20;
}
Graphics g = e.Graphics;
aEnemy.DrawEnemy(g);//画敌人,5个实心圆表示
//Tank的运动方向来确定其画法
if (tankWay % 2 == 0)
myTank.DrawTankX(g, Color.Blue);
if (tankWay % 2 == 1)
myTank.DrawTankY(g, Color.Blue);
if (fire)//开火
{
myTank.Fire(g);
if (aFight.Win(myTank, aEnemy))//是否打到敌人
enemyNumber--;
if (enemyNumber == 0)
{
MessageBox.Show("", "");
}
fire = false;
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
timer1.Interval = 10;
timer2.Interval = 20;
//WASD来控制坦克的方向
if (e.KeyCode == Keys.A)
{
tankWay = 4;
timer1.Enabled = true;
}
else if (e.KeyCode == Keys.W)
{
tankWay = 1;
timer1.Enabled = true;
}
else if (e.KeyCode == Keys.D)
{
tankWay = 2;
timer1.Enabled = true;
}
else if (e.KeyCode == Keys.S)
{
tankWay = 3;
timer1.Enabled = true;
}
//数字键盘1.2键控制炮的转动
if (e.KeyCode == Keys.NumPad1)
{
cannonWay = 1;
timer2.Enabled = true;
}
else if (e.KeyCode == Keys.NumPad2)
{
cannonWay = 2;
timer2.Enabled = true;
}
//空格控制开火
if (e.KeyCode == Keys.Space)
{
fire = true;
}
pictureBox1.Refresh();
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.A ||
e.KeyCode == Keys.W ||
e.KeyCode == Keys.D ||
e.KeyCode == Keys.S)
timer1.Enabled = false;
if (e.KeyCode == Keys.NumPad1 ||
e.KeyCode == Keys.NumPad2)
timer2.Enabled = false;
pictureBox1.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{ //初始化
begin = true;
timer3.Interval = 100;
timer3.Enabled = true;
myTank.tankpoint = new Point(190, 160);
pictureBox1.Refresh();
button1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
//坦克的运动
if (tankWay == 1)
{
myTank.tankpoint = new Point(myTank.tankpoint.X, myTank.tankpoint.Y - 1);
if (myTank.tankpoint.Y < 10)
myTank.tankpoint = new Point(myTank.tankpoint.X, 10);
}
else if (tankWay == 2)
{
myTank.tankpoint = new Point(myTank.tankpoint.X + 1, myTank.tankpoint.Y);
if (myTank.tankpoint.X > 370)
myTank.tankpoint = new Point(370, myTank.tankpoint.Y);
}
else if (tankWay == 3)
{
myTank.tankpoint = new Point(myTank.tankpoint.X, myTank.tankpoint.Y + 1);
if (myTank.tankpoint.Y > 310)
myTank.tankpoint = new Point(myTank.tankpoint.X, 310);
}
else if (tankWay == 4)
{
myTank.tankpoint = new Point(myTank.tankpoint.X - 1, myTank.tankpoint.Y);
if (myTank.tankpoint.X < 10)
myTank.tankpoint = new Point(10, myTank.tankpoint.Y);
}
pictureBox1.Refresh();
}
private void timer2_Tick(object sender, EventArgs e)
{ //炮的转动
if (cannonWay == 1)
xMove--;
else if (cannonWay == 2)
xMove++;
pictureBox1.Refresh();
}
private void timer3_Tick(object sender, EventArgs e)
{ //敌人的靠近
aEnemy.EnemyMove(myTank, enemyNumber);
//坦克的安全
/* if (aFight.destroy(myTank, aEnemy))
{
myTank = null;
MessageBox.Show("", "");
timer1.Enabled = false;
timer2.Enabled = false;
timer3.Enabled = false;
}*/
pictureBox1.Refresh();
}
}
}
using System;--------------------编程问答-------------------- Bomb.cs
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Tank
{
class Tank
{
private Point tankPoint;
public Point tankpoint
{
get
{
return tankPoint;
}
set
{
tankPoint = value;
}
}
private Point cannonPoint;
public Point cannonpoint
{
get
{
return cannonPoint;
}
set
{
cannonPoint = value;
}
}
public void DrawTankX(Graphics g, Color colour)
{
Rectangle top = new Rectangle(tankPoint.X - 5, tankPoint.Y - 5, 10, 10);
Rectangle body = new Rectangle(tankPoint.X - 10, tankPoint.Y - 5, 20, 10);
Rectangle wheelA = new Rectangle(tankPoint.X - 13, tankPoint.Y - 10, 26, 5);
Rectangle wheelB = new Rectangle(tankPoint.X - 13, tankPoint.Y + 5, 26, 5);
Pen myPen = new Pen(Color.Gray);
Pen myBigpen = new Pen(colour, 3);
SolidBrush myBrush = new SolidBrush(colour);
g.DrawRectangle(myPen, body);
g.DrawEllipse(myPen, wheelA);
g.DrawEllipse(myPen, wheelB);
g.FillEllipse(myBrush, top);
g.DrawLine(myBigpen, tankPoint, cannonPoint);
}
public void DrawTankY(Graphics g, Color colour)
{
Rectangle top = new Rectangle(tankPoint.X - 5, tankPoint.Y - 5, 10, 10);
Rectangle body = new Rectangle(tankPoint.X - 5, tankPoint.Y - 10, 10, 20);
Rectangle wheelA = new Rectangle(tankPoint.X - 10, tankPoint.Y - 13, 5, 26);
Rectangle wheelB = new Rectangle(tankPoint.X + 5, tankPoint.Y - 13, 5, 26);
Pen myPen = new Pen(Color.Gray);
Pen myBigpen = new Pen(colour, 3);
SolidBrush myBrush = new SolidBrush(colour);
g.DrawRectangle(myPen, body);
g.DrawEllipse(myPen, wheelA);
g.DrawEllipse(myPen, wheelB);
g.FillEllipse(myBrush, top);
g.DrawLine(myBigpen, tankPoint, cannonPoint);
}
public void Fire(Graphics g)
{
Point endPoint = new Point(cannonPoint.X + (cannonPoint.X - tankPoint.X) * 25,
cannonPoint.Y + (cannonPoint.Y - tankPoint.Y) * 25);
Pen firePen = new Pen(Color.Gold, 2);
g.DrawLine(firePen, cannonPoint, endPoint);
}
}
}
using System;--------------------编程问答-------------------- Enemy.cs
using System.Collections.Generic;
using System.Text;
using System.Drawing;
namespace Tank
{
class Bomb
{
private Point bombPoint;
public Point bombpoint
{
get
{
return bombPoint;
}
set
{
bombPoint = value;
}
}
public void DrowBomb(Graphics g)
{
SolidBrush myBrush = new SolidBrush(Color.Red);
g.FillEllipse(myBrush, bombPoint.X - 8, bombPoint.Y - 8, 16, 16);
}
public Point GetPoint(int i)
{
Point ePoint;
int x, y;
Random rdm = new Random(System.DateTime.Now.Millisecond + i);
x = rdm.Next(5, 375);
y = rdm.Next(5, 315);
if (x % 4 == 0)
ePoint = new Point(x, 5);
else if (x % 4 == 1)
ePoint = new Point(x, 315);
else if (x % 4 == 2)
ePoint = new Point(5, y);
else
ePoint = new Point(375, y);
return ePoint;
}
}
}
using System;--------------------编程问答-------------------- Fight.cs
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;
namespace Tank
{
class Enemy
{
Bomb abomb;
public ArrayList enemyList = new ArrayList();
public Enemy(int count)
{
for (int i = 0; i < count; i++)
{
abomb = new Bomb();
abomb.bombpoint = abomb.GetPoint(i);
enemyList.Add(abomb);
}
}
public void DrawEnemy(Graphics g)
{
for (int i = 0; i < enemyList.Count; i++)
{
abomb = (Bomb)enemyList[i];
abomb.DrowBomb(g);
}
}
public void EnemyMove(Tank aTank, int count)
{
for (int i = 0; i < count; i++)
{
abomb = (Bomb)enemyList[i];
if (abomb.bombpoint.X < aTank.tankpoint.X)
abomb.bombpoint = new Point(abomb.bombpoint.X + i + 1, abomb.bombpoint.Y);
if (abomb.bombpoint.Y < aTank.tankpoint.Y)
abomb.bombpoint = new Point(abomb.bombpoint.X, abomb.bombpoint.Y + i + 1);
if (abomb.bombpoint.X >= aTank.tankpoint.X)
abomb.bombpoint = new Point(abomb.bombpoint.X - i - 1, abomb.bombpoint.Y);
if (abomb.bombpoint.Y >= aTank.tankpoint.Y)
abomb.bombpoint = new Point(abomb.bombpoint.X, abomb.bombpoint.Y - i - 1);
enemyList.Add(abomb);
}
for (int i = 0; i < count; i++)
enemyList.Remove(enemyList[i]);
}
}
}
using System;--------------------编程问答-------------------- 留个脚印~ 需要时回来看看 --------------------编程问答-------------------- 再往上顶顶 --------------------编程问答-------------------- 弄个代码大家下下不是更方便 --------------------编程问答-------------------- 不知道怎么上传代码,只好这样贴出来了 --------------------编程问答-------------------- 以前也遇到过,后来换个案件事件声音消失了! --------------------编程问答-------------------- 我也遇到过,但是不知道莫名其妙的就没有了! --------------------编程问答-------------------- 我记得以前碰到过的是按Enter会想,就判断是Enter件后,执行晚了相应的操作,删掉那个键码
using System.Collections.Generic;
using System.Text;
namespace Tank
{
class Fight
{
public bool destroy(Tank aTank, Enemy aEnemy)
{
Bomb aBomb;
for (int i = 0; i < aEnemy.enemyList.Count; i++)
{
aBomb = (Bomb)aEnemy.enemyList[i];
int X = aTank.tankpoint.X - aBomb.bombpoint.X;
int Y = aTank.tankpoint.Y - aBomb.bombpoint.Y;
if (X * X + Y * Y <= 320)
return true;
}
return false;
}
public bool Win(Tank aTank, Enemy aEnemy)
{
Bomb aBomb;
for (int i = 0; i < aEnemy.enemyList.Count; i++)
{
aBomb = (Bomb)aEnemy.enemyList[i];
int X = aTank.tankpoint.X - aBomb.bombpoint.X;
int Y = aTank.tankpoint.Y - aBomb.bombpoint.Y;
int n = (int)Math.Sqrt(X * X + Y * Y);
double a = (aTank.tankpoint.X +
(aTank.cannonpoint.X - aTank.tankpoint.X) * n / 20);
double b = (aTank.tankpoint.Y +
(aTank.cannonpoint.Y - aTank.tankpoint.Y) * n / 20);
int x = (int)(a - aBomb.bombpoint.X);
int y = (int)(b - aBomb.bombpoint.Y);
if (x * x + y * y <= 64)
{
aEnemy.enemyList.Remove(aEnemy.enemyList[i]);
return true;
}
}
return false;
}
}
}
你做完操作吧e清空了看看 --------------------编程问答-------------------- mark~~
补充:.NET技术 , C#