As the title says, I have created a maze and decided to add coins which are shown in picture boxes throughout it. I made them disappear when your cursor enters them but I can't seem to figure out how to increase the score in the label. I currently don't have anything written for the score so I'll be starting fresh, all I have is a label called "lblScore" and that is it.
Asked
Active
Viewed 29 times
-1
-
1I see folks marking your question down and voting to close. It would help if you read the documentation that explains how to ask a good question: https://stackoverflow.com/help/how-to-ask – Joe Mayo Apr 19 '20 at 01:29
1 Answers
1
how about class for player statistics:
public class PlayerStatistics {
public event EventHandler ScoreChange;
public int Score { get; private set; }
public void IncreaseScore(int valueToAdd)
{
this.Score += valueToAdd;
this.ScoreChange?.Invoke(this, EventArgs.Empty);
}
}
event on picturebox to listen for mouse enter:
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
this.PlayerStatistics.IncreaseScore(1);
}
player score implementation:
this.PlayerStatistics = new PlayerStatistics();
this.PlayerStatistics.ScoreChange += this.PlayerStatistics_ScoreChange;
and event listener:
private void PlayerStatistics_ScoreChange(object sender, EventArgs e)
{
this.label1.Text = "Score: " + this.PlayerStatistics.Score.ToString();
}

S. Kalinowski
- 111
- 7
-
I know this may seem like a very beginner thing but where exactly do I type these lines? I get the event for picturebox and the event listener, I'm not entirely sure where the other two go, I'm still very new to all of this. – sasha Korzikovas Apr 19 '20 at 11:02
-
1) You can create new class PlayerStatistics and it will be in new file -> thats a good to keep one class per file (and to match filename with class name). 2) register new event and add body to it 3) you might add implementation to constructor or under Click event on Start Button if you have one 4) just in Form so (3) have a method it is referencing to – S. Kalinowski Apr 19 '20 at 11:10
-
@sashaKorzikovas and remember to mark question as answered if my answer satisfies you so other will know it is solved – S. Kalinowski Apr 19 '20 at 11:14
-
Okay got it, just another quick follow up question, I am having trouble setting my cursors starting position when the application starts, is this something you can actually do? Thanks in advance. – sasha Korzikovas Apr 19 '20 at 12:35
-
https://stackoverflow.com/questions/8050825/how-to-move-mouse-cursor-using-c – S. Kalinowski Apr 19 '20 at 13:49