|
No, that isn't possible - but it doesn't sound like a good way to me anyway. The += needs a EventHandler delegate instance as a parameter. This can be done by using either an existing instance, a new instance or just a method name with the correct signature.
Have you considered an alternative aproach - and using one event handler for all the picture boxes?
You can attach the event handler to all PictureBox es in a simple loop:
foreach (Control control in this.Controls)
if (control is PictureBox)
control.Click += PictureBoxClickHandler;
and your handling method would start like this:
private void PictureBoxClickHandler(object sender, EventArgs e)
{
PictureBox pictureBox = sender as PictureBox;
if (pictureBox != null)
{
}
}
You can now use any property (perhaps Name as you want to use strings, the Tag property is designed for this though) to identify the picture box in question and react as you wish. I usually prefer to subclass the control and add an Id property:
public class MyPictureBox : PictureBox
{
private int id;
public int Id
{
get { return id; }
set { id = value; }
}
}
foreach (Control control in this.Controls)
if (control is MyPictureBox)
control.Click += MyPictureBoxClickHandler;
private void MyPictureBoxClickHandler(object sender, EventArgs e)
{
MyPictureBox myPictureBox = sender as MyPictureBox;
if (myPictureBox != null)
{
switch (myPictureBox.Id)
{
case 1:
break;
case 2:
case 3:
break;
default:
break;
}
}
}
[Edit] You would obviously need to use MyPictureBox es instead of the built in PictureBox . After a build, it will show in your toolbox for drag and drop - or you can just change the code where they are created.
|
|
|
|
|
Dave,
Thanks, I will try your methodology, it looks pretty easy.
Pete
|
|
|
|
|
Hi,
I'm new in c# but have a bit of knowledge in c++ and vb.net. I have a small assignment here of writing a simple game in c#. The game consists of a single player who can move left or right. It also has the raindrops which appear in random position in the top quarter of the screen vertically.
The objective of the game is to move the player so that he dodges the raindrops. If raindrop touches the player then the game is over.
I understand c# code but my problem is on making the graphics/animation. How can I create the falling raindrops and the player moving horizontally across the screen?
Your help is highly appreciated.
|
|
|
|
|
Qobacha wrote: How can I create the falling raindrops and the player moving horizontally across the screen?
By updating his position on a keypress-event and drawing him somewhere else in the paint-handler of the form.
Check out the events of the form
Bastard Programmer from Hell
|
|
|
|
|
It appears that Form doesn't raise Key events, but it does raise Mouse events.
And forget about the Paint handler -- I created a Label with a person on it (Webdings typeface) and I set the Location while the user holds a mouse button down.
|
|
|
|
|
Can you please explain more.
|
|
|
|
|
Do you know how to draw a line in a form?
Bastard Programmer from Hell
|
|
|
|
|
Hi
I got you but do i do that?
|
|
|
|
|
By placing the players Location in a variable; paint your player there - I assume you know how to paint on a form?
Then, increase or decrease either x or y values depending on the buttons that you receive in the KeyDown event.
Bastard Programmer from Hell
|
|
|
|
|
Thanks for the info. I haven't used paint but I will read about it and see how far i'll go.
|
|
|
|
|
You could also use a PictureBox and move that around (vary it's location), but that uaully doesn't give the smoothests of animations.
The forms paint-event is described on MSDN[^], as well as the event[^] that's raised when the user presses a key.
Bastard Programmer from Hell
|
|
|
|
|
Hi,
I read about paint and managed to create the moving objects representing raindrops. But the problem i have now is of making these raindrops falls forever as long as they they don't come in contact with the player.
Right now they fall vertically from one end the screen and disappear on the other end. How could I create that makes them keep falling?
Thanks
|
|
|
|
|
Check if they exceeded the boundaries of your form, and reset them, possibly to a semi-random[^] location (varying horizontal starting point, vertical always from top of form).
Have you worked on catching user-input too? Simplest way would be by toggeling the KeyPreview[^]-property of the form, and handle the event that comes with it.
Can we expect an article on "writing your own game" when you're done?
Bastard Programmer from Hell
|
|
|
|
|
Hi, Y
You'll definitely get an article from this. Like you said i now stuck on catching the user input. Whats the best way to do this?
|
|
|
|
|
Qobacha wrote: You'll definitely get an article from this.
Cool, that'll help others who are investigating the topic
Qobacha wrote: Like you said i now stuck on catching the user input. Whats the best way to do this?
Toggle the KeyPreview[^] property, and handle the OnPreviewKeyDown[^] event.
Bastard Programmer from Hell
|
|
|
|
|
Hi,
Below is the code from the class that i was testing the movement of my balls with. I think the problem is on the "Move" method. In this example I made the ball to move in the opposite direction whenever it touches the top or bottom walls. But what I really want to do is for the ball start moving from the top again once it had reached the bottom. Any suggestion on the corrections I could make?
class Ball
{
public float x, y, xvel, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
xvel = r.Next(2) + 2;
yvel = r.Next(2) + 2;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y - radius < 0 || y + radius > gameHeight)
{
yvel = -yvel;
}
y += yvel;
}
public void Draw(Graphics g)
{
g.FillEllipse(brush, new RectangleF(x - radius, y - radius, radius * 2, radius * 2));
}
}
|
|
|
|
|
Qobacha wrote:
if(y - radius < 0 || y + radius > gameHeight)
{
yvel = -yvel;
}
This is where it inverts the direction. Check if y is larger than the screens' height, and if so, set it to 0
Bastard Programmer from Hell
|
|
|
|
|
You were right i have been so stupid. I have set y to 0 and its working. Now the first part done. Starting on user input and collision detection between the user and balls
Thanks
|
|
|
|
|
Small things are easily overlooked. Good luck
Bastard Programmer from Hell
|
|
|
|
|
Its me again. I'm now working on the very last part of the exercise. The program should now detect the collision between the balls and the rectangle. How do i check if the ball and the square are in contact.
|
|
|
|
|
Qobacha wrote: How do i check if the ball and the square are in contact.
Rectangle.IntersectsWith[^] would be an option
Bastard Programmer from Hell
|
|
|
|
|
I have tried Rectangle.IntersectWith but without success. I'm thinking that it may not be applicable in my case because of the way i have written my code. because i get the error saying something like values of x and y are not static.
Can you check if my code given below and see if I can be able to use IntersectWith.
public partial class Form1 : Form
{
bool collided = false;
Player player;
List<Ball> balls;
const int fps = 60;
public Form1()
{
InitializeComponent();
balls = new List<Ball>();
Random r = new Random();
for(int i =0; i<1;i ++)
{
balls.Add(new Ball(Width, Height,r));
}
var task = new Task(Run);
task.Start();
player = new Player()
{
x = this.Width/2,
y = (Height*9/10),
xvel = 10,
brush = Brushes.Black,
};
}
protected void Run()
{
while (true)
{
for(int i = 0; i < balls.Count; i++)
{
balls[i].Move(this.Width);
}
this.Invalidate();
Thread.Sleep(1000 / fps);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.Clear(Color.White);
for(int i = 0; i < balls.Count; i++)
{
balls[i].Draw(g);
}
player.DrawPlayer(g);
}
private void CheckCollision(PaintEventArgs e)
{
if (player.IntersectsWith(balls))
{
player.Intersect(balls);
if (!player.IsEmpty)
{
collided = true;
MessageBox.Show("collision detected");
}
}
}
}
public class Player
{
public float x, y, xvel;
public Brush brush;
public Player()
{
}
public void DrawPlayer(Graphics g)
{
g.FillRectangle(brush, new RectangleF(x, y, 30,30));
}
public void MovePlayerLeft(int gameWidth)
{
if (x > 0)
{
x -= xvel;
}
}
public void MovePlayerRight(int gameWidth)
{
if (x < gameWidth-47)
{
x += xvel;
}
}
}
public class Ball
{
public float x, y, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
yvel = r.Next(2) + 5;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y + radius >= gameHeight)
{
y = 0;
}
y += yvel;
}
public void Draw(Graphics g)
{
g.FillEllipse(brush, new RectangleF(x-radius,y - radius, radius * 2, radius * 2));
}
}
|
|
|
|
|
Maybe the approach i took to develop the game was wrong from the start. Starting the new game using pictures as my objects maybe a better option.
modified 1-May-12 8:58am.
|
|
|
|
|
The IntersectsWith method will only work for a Rectangle , not for "any" class.
Yes, it might be easier to use controls, since they all have a location , a Width and Height . That means that you'll still have to check whether those boundaries intersect.
Bastard Programmer from Hell
|
|
|
|
|
Before I could give up what do you think of this attempt. Making new method to check the position of the ball against the player?
Here is my attempt not sure if it will work though.
private bool collision(Player player)
{
return !(player.x > x + radius
|| player.x + 30 < x
|| player.y > y + radius
|| player.y + 30 < y);
}
namespace MyFirstGameTutorial
{
public class Ball
{
Player square;
public float x, y, yvel, radius;
public Brush brush;
public Ball(int gamewidth,int gameHeight,Random r)
{
x = r.Next(gamewidth);
y = r.Next(gameHeight);
yvel = r.Next(2) + 2;
radius = r.Next(10) + 5;
brush = new SolidBrush(Color.Blue);
}
public void Move(int gameHeight)
{
if (y + radius >= gameHeight)
{
y = 0;
}
y += yvel;
}
public void Draw(Graphics g)
{
RectangleF circle = new RectangleF(x - radius, y - radius, radius * 2, radius * 2);
g.FillEllipse(brush, circle);
}
private bool collision(Player player)
{
return !(player.x > x + radius
|| player.x + 30 < x
|| player.y > y + radius
|| player.y + 30 < y);
}
}
}
|
|
|
|