碰撞帮助,多个Class'继承 [英] Collision Help, multiple Class' with inheritance
问题描述
为了我的生命 我似乎无法弄清楚我做错了什么,我已经设法制造一个玩家并让它拍摄。还设法让敌人在图片框中以随机位置/速度生成。
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.
我一直试图使用 "Ball .Bounds.IntersectsWith( pictureBox2.Bounds)"但似乎Ball.bounds是错误的。
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
使用System;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;使用System.Drawing
; //图片
命名空间shootyarcher
{
class box
{
public float x;
public float y;
public float w;
公共浮动h;
public float xspeed;
public float yspeed;
public Image pic;
public Box()//构造函数
{
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);
}
公共浮动宽度()
{
返回pic.Width;
}
公共浮动高度()
{
返回pic.Height;
}
公共浮动左()
{
返回x;
}
公共浮动权利()
{
返回x + Width();
}
公共浮动顶部()
{
返回y;
}
public float Bottom()
{
return y + Height();
}
}
}
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(); } } }
推荐答案
Ball没有属性边界。
Ball does not have a property bounds.
这篇关于碰撞帮助,多个Class'继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!