本文介绍了我的mousedown事件在笔记本电脑上不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我建造了这个简单的大炮游戏,问题是我的mousedown事件负责射击弹丸不起作用....
请帮助我!
你可以在这里下载游戏
[]
I built this simple cannon game, well the problem is my mousedown event that is responsible for shooting projectile doesn''t work....
Help me please!
You can download the game here
http://www.4shared.com/rar/EPqDla1s/Canon1.html[^]
public partial class Form1 : Form
{
const int SCREEN_WIDTH = 600;
const int SCREEN_HEIGHT = 480;
const int PROJECTILE_SPEED = 1;
const int BASE_TARGET_SPEED = 8;
const float COLLISION_DISTANCE = 20.0f;
const int STARTING_LIVES = 3;
const int GAME_DIFFICULTY_CONTROL = 3;
const int LIVES_TILE_WIDTH = 16;
int targetVelocityX;
int targetVelocityY;
Random random;
bool isGameRunning = false;
int lives;
int level;
public Form1()
{
InitializeComponent();
this.ClientSize = new Size(SCREEN_WIDTH,SCREEN_HEIGHT);
random = new Random();
}
#region events
private void Form1_Load(object sender, EventArgs e)
{
Player.Load("player.png");
Target.Load("target.png");
Projectile.Load("projectile.png");
LivesDisplay.Load("lives.png");
TitleScreen.Load("target.png");
TitleScreen.Top = 0;
TitleScreen.Left = 0;
TitleScreen.Width = SCREEN_WIDTH;
TitleScreen.Height = SCREEN_HEIGHT;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Player.Left = e.X - Player.Width/2;
Player.Top = SCREEN_HEIGHT - Player.Height;
}
private void GameUpdateTimer_Tick(object sender, EventArgs e)
{
Projectile.Top -= PROJECTILE_SPEED;
Target.Top += targetVelocityY;
Target.Left += targetVelocityX;
CheckGroundCollision();
CheckAirCollision();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if(isGameRunning)
{
FirePorjectile();
}
else
{
BeginGame();
}
}
#endregion
#region gameplay methods
private void BeginGame()
{
isGameRunning = true;
lives = STARTING_LIVES;
level = 1;
DisplayLives();
ResetPorjectile();
ResetTarget();
TitleScreen.Hide();
Cursor.Hide();
}
private void EndGame()
{
isGameRunning = false;
TitleScreen.Show();
Cursor.Show();
}
private void KillPlayer()
{
ResetTarget();
lives--;
DisplayLives();
if (lives == 0)
{
EndGame();
}
}
private void KillTarget()
{
level++;
ResetTarget();
ResetPorjectile();
}
private void DisplayLives()
{
LivesDisplay.Width = lives * LIVES_TILE_WIDTH;
}
private void FirePorjectile()
{
if (Projectile.Bottom < 0)
{
Projectile.Left = Player.Left + Player.Width / 2 - Projectile.Width / 2;
Projectile.Top = Player.Top - Projectile.Height;
}
}
private void ResetPorjectile()
{
Projectile.Top = -Projectile.Height;
}
private void ResetTarget()
{
targetVelocityX = random.Next(2,6);
targetVelocityY = BASE_TARGET_SPEED + level/ GAME_DIFFICULTY_CONTROL ;
if (random.Next(2) == 0)
{
targetVelocityX *= -1;
}
Target.Top = -Target.Height;
Target.Left = SCREEN_WIDTH / 2 - Target.Width / 2;
}
#endregion
#region collision methods
private void CheckGroundCollision()
{
if (Target.Bottom > SCREEN_HEIGHT)
{
KillPlayer();
}
}
private void CheckAirCollision()
{
Point projectileCenter = GetCenter(Projectile);
Point targetCenter = GetCenter(Target);
if (Distance(projectileCenter, targetCenter) < COLLISION_DISTANCE)
{
KillTarget();
}
}
#endregion
#region utility methods
private Point GetCenter(Control c)
{
return new Point(c.Left + c.Width/2,c.Top + c.Height/2);
}
private float Distance(Point pointA, Point pointB)
{
int a = pointA.X - pointB.X;
int b = pointA.Y - pointB.Y;
float c = (float)Math.Sqrt((a * a) + (b*b));
return c;
}
#endregion
}
}
设计类
Design class
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Player = new System.Windows.Forms.PictureBox();
this.Target = new System.Windows.Forms.PictureBox();
this.Projectile = new System.Windows.Forms.PictureBox();
this.GameUpdateTimer = new System.Windows.Forms.Timer(this.components);
this.LivesDisplay = new System.Windows.Forms.PictureBox();
this.TitleScreen = new System.Windows.Forms.PictureBox();
((System.ComponentModel.ISupportInitialize)(this.Player)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Target)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Projectile)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.LivesDisplay)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.TitleScreen)).BeginInit();
this.SuspendLayout();
//
// Player
//
this.Player.Location = new System.Drawing.Point(92, 12);
this.Player.Name = "Player";
this.Player.Size = new System.Drawing.Size(32, 32);
this.Player.TabIndex = 0;
this.Player.TabStop = false;
//
// Target
//
this.Target.Enabled = false;
this.Target.Location = new System.Drawing.Point(92, 106);
this.Target.Name = "Target";
this.Target.Size = new System.Drawing.Size(32, 32);
this.Target.TabIndex = 1;
this.Target.TabStop = false;
//
// Projectile
//
this.Projectile.Enabled = false;
this.Projectile.Location = new System.Drawing.Point(137, 200);
this.Projectile.Name = "Projectile";
this.Projectile.Size = new System.Drawing.Size(9, 9);
this.Projectile.TabIndex = 2;
this.Projectile.TabStop = false;
//
// GameUpdateTimer
//
this.GameUpdateTimer.Enabled = true;
this.GameUpdateTimer.Interval = 16;
this.GameUpdateTimer.Tick += new System.EventHandler(this.GameUpdateTimer_Tick);
//
// LivesDisplay
//
this.LivesDisplay.Location = new System.Drawing.Point(4, 4);
this.LivesDisplay.Name = "LivesDisplay";
this.LivesDisplay.Size = new System.Drawing.Size(48, 16);
this.LivesDisplay.TabIndex = 3;
this.LivesDisplay.TabStop = false;
//
// TitleScreen
//
this.TitleScreen.Enabled = false;
this.TitleScreen.Location = new System.Drawing.Point(172, 68);
this.TitleScreen.Name = "TitleScreen";
this.TitleScreen.Size = new System.Drawing.Size(100, 50);
this.TitleScreen.TabIndex = 3;
this.TitleScreen.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.TitleScreen);
this.Controls.Add(this.LivesDisplay);
this.Controls.Add(this.Projectile);
this.Controls.Add(this.Target);
this.Controls.Add(this.Player);
this.Enabled = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.MaximizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
((System.ComponentModel.ISupportInitialize)(this.Player)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.Target)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.Projectile)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.LivesDisplay)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.TitleScreen)).EndInit();
this.ResumeLayout(false);
}
#endregion
推荐答案
这篇关于我的mousedown事件在笔记本电脑上不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!