how can I make this wok?
multi laser and boom trops????
using
System;
using
System.Drawing;
using
System.Collections;
using
System.ComponentModel;
using
System.Windows.Forms;
using
System.Data;
namespace
cyberspace_invader
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Game : System.Windows.Forms.Form
{
private System.Windows.Forms.Timer animationTimer;
private System.Windows.Forms.Timer bombTimer;
private System.Windows.Forms.PictureBox pictureBox1;
private System.ComponentModel.IContainer components;
private Graphics paper;
private User user;
private Alien alien;
private Laser laser;
internal System.Windows.Forms.PictureBox alienImage;
internal System.Windows.Forms.PictureBox userImage;
private Bomb bomb ;
public Game()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
paper = pictureBox1.CreateGraphics();
NewGame();
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (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();
System.Resources.
ResourceManager resources = new System.Resources.ResourceManager(typeof(Game));
this.alienImage = new System.Windows.Forms.PictureBox();
this.userImage = new System.Windows.Forms.PictureBox();
this.animationTimer = new System.Windows.Forms.Timer(this.components);
this.bombTimer = new System.Windows.Forms.Timer(this.components);
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// alienImage
//
this.alienImage.Image = ((System.Drawing.Bitmap)(resources.GetObject("alienImage.Image")));
this.alienImage.Location = new System.Drawing.Point(280, 112);
this.alienImage.Name = "alienImage";
this.alienImage.Size = new System.Drawing.Size(32, 24);
this.alienImage.TabIndex = 3;
this.alienImage.TabStop = false;
this.alienImage.Visible = false;
//
// userImage
//
this.userImage.Image = ((System.Drawing.Bitmap)(resources.GetObject("userImage.Image")));
this.userImage.Location = new System.Drawing.Point(288, 40);
this.userImage.Name = "userImage";
this.userImage.Size = new System.Drawing.Size(32, 24);
this.userImage.TabIndex = 4;
this.userImage.TabStop = false;
this.userImage.Visible = false;
//
// animationTimer
//
this.animationTimer.Enabled = true;
this.animationTimer.Tick += new System.EventHandler(this.animationTimer_Tick);
//
// bombTimer
//
this.bombTimer.Enabled = true;
this.bombTimer.Interval = 1000;
this.bombTimer.Tick += new System.EventHandler(this.bombTimer_Tick);
//
// pictureBox1
//
this.pictureBox1.Location = new System.Drawing.Point(32, 16);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(216, 216);
this.pictureBox1.TabIndex = 5;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
this.pictureBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.pictureBox1_MouseMove_1);
//
// Game
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(328, 266);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.pictureBox1,
this.userImage,
this.alienImage});
this.Name = "Game";
this.Text = "Cyberspace invader";
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[
STAThread]
static void Main()
{
Application.Run(new Game());
}
private void bombTimer_Tick(object sender, System.EventArgs e)
{
if (bomb == null)
{
bomb =
new Bomb(alien.X, alien.Y);
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
int initialX = user.X + user.Width / 2;
int initialY = user.Y - user.Height;
if (laser == null)
{
laser =
new Laser(initialX, initialY);
}
}
private void pictureBox1_MouseMove_1(object sender, System.Windows.Forms.MouseEventArgs e)
{
user.Move(e.X);
DrawAll();
}
private void animationTimer_Tick(object sender, System.EventArgs e)
{
MoveAll();
DrawAll();
CheckHits();
}
public void MoveAll()
{
alien.Move();
if (bomb != null)
{
bomb.Move();
}
if (laser != null)
{
laser.Move();
}
}
private void CheckHits()
{
if (Collides(laser, alien))
{
EndGame(
"user");
}
else
{
if (Collides(bomb, user))
{
EndGame(
"alien");
}
}
if (bomb != null)
{
if (bomb.Y > pictureBox1.Height)
{
bomb =
null;
}
}
if (laser != null)
{
if (laser.Y < 0)
{
laser =
null;
}
}
}
public bool Collides(Sprite one, Sprite two)
{
if (one == null || two == null)
{
return false;
}
if ( one.X > two.X
&& one.Y < (two.Y + two.Height)
&& (one.X + one.Width) < (two.X + two.Width)
&& (one.Y + one.Width) > (two.Y))
{
return true;
}
else
{
return false;
}
}
private void EndGame(string winner)
{
laser =
null;
bomb =
null;
animationTimer.Enabled =
false;
bombTimer.Enabled =
false;
MessageBox.Show("game over - " + winner + " wins");
NewGame();
}
public void NewGame()
{
animationTimer.Enabled =
true;
bombTimer.Enabled =
true;
user =
new User(userImage);
alien =
new Alien(alienImage);
}
private void DrawAll()
{
paper.Clear(
Color.White);
user.Draw(paper);
alien.Draw(paper);
if (laser != null)
{
laser.Draw(paper);
}
if (bomb != null)
{
bomb.Draw(paper);
}
}
}
}
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).