Collision Help, multiple Class' with inheritance

  • Thread starter Thread starter RussLee33
  • Start date Start date
R

RussLee33

Guest
for the life of me i just cant seem to be working out what i am doing wrong, i have managed to make a player and make it shoot. also managed to make enemy spawn in at random locations/speeds in the picture box.

i just cant seem to get any Collision to work, i am trying to keep all enemies in the picture box and bounce back and also bounce off each other, and then destroy them when shot.

i have been trying to use "Ball.Bounds.IntersectsWith(pictureBox2.Bounds)" but it seems that Ball.bounds is wrong.



Form1.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;


namespace shootyarcher
{

public partial class Form1 : Form
{
Archer player;
List<Arrow> Shiv;
List<Ball> Kill;



public Form1()
{
InitializeComponent();

Cursor.Hide();

this.FormBorderStyle = FormBorderStyle.None;
this.TopMost = true;
this.Bounds = Screen.PrimaryScreen.Bounds;


player = new Archer(0, 0);
Shiv = new List<Arrow>();
Kill = new List<Ball>();
for (int i = 0; i < 400; i++)
{
Ball temp = new Ball();
Kill.Add(temp);
}

}


private void timer1_Tick(object sender, EventArgs e)
{
player.Move();
foreach (Arrow t in Shiv)
{
t.Move();

}
foreach (Ball m in Kill)
{
m.Move();
}


pictureBox1.Invalidate();
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
player.moveup = true;
}
if (e.KeyCode == Keys.S)
{
player.movedown = true;
}
if (e.KeyCode == Keys.Space)
{
Arrow temp = new Arrow(player.x, player.y);
Shiv.Add(temp);
}
if (e.KeyCode == Keys.Escape)
{
this.Close();
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
player.moveup = false;
}
if (e.KeyCode == Keys.S)
{
player.movedown = false;
}
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
player.Draw(e.Graphics);
foreach (Arrow t in Shiv)
{
t.Draw(e.Graphics);
}
foreach (Ball m in Kill)
{
m.Draw(e.Graphics);
}

}

}
}


Ball.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace shootyarcher
{
class Ball : Box
{


static private Random generator = new Random();
private int countdown;

public Ball ()
{
pic = Properties.Resources.Balloon;
x = generator.Next(60, 1920);
y = generator.Next(100, 1000);
xspeed = generator.Next(-10, 4);
yspeed = generator.Next(-10, 4);
countdown = generator.Next(100, 200);
}
public new void Move()
{
countdown--;
if (countdown == 0)
{
xspeed = generator.Next(-4, 4);
yspeed = generator.Next(-4, 4);
countdown = generator.Next(20, 40);
}
x = x + xspeed;
y = y + yspeed;
}



}
}




box.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing; //pictures

namespace shootyarcher
{
class Box
{


public float x;
public float y;
public float w;
public float h;
public float xspeed;
public float yspeed;
public Image pic;

public Box() // constructor
{

x = 0;
y = 0;
xspeed = 0;
yspeed = 0;
}
public void Move()
{
x = x + xspeed;
y = y + yspeed;
}
public void Draw(Graphics g)
{
g.DrawImage (pic, x, y);
}

public float Width()
{

return pic.Width;
}

public float Height()
{

return pic.Height;
}

public float Left()
{

return x;
}

public float Right()
{

return x + Width();
}

public float Top()
{

return y;
}

public float Bottom()
{

return y + Height();
}
}
}

Continue reading...
 
Back
Top