0

I am making a game in C# and the game will not end once the Life has hit 0. The game however does show the ending MessageBox alerting the User that they have no live remaining which by pressing OK the game should close and the Gameover Dialog Form should show. However the game just restarts again from scratch while the Gameover form shows. Can anyone help me as this project has all the elements of a small indie side scroll game which I will be using this Game as an introduction to the components within my next project. This is the code which I have used, if you can help me it would be greatly appreciated. Bear in mind this is my first question posted and having searched extensively for the solution which I have not been able to find.

using System;       //--->
using System.Collections.Generic;   //--->
using System.ComponentModel;    //--->
using System.Data;  //--->
using System.Drawing;   //--->
using System.Linq;  //--->
using System.Text;  //--->
using System.Threading.Tasks;   //--->
using System.Windows.Forms; //------>  

namespace BouningBall
{
public partial class EasyGame : Form
{

    public int speed_Left = 5;      
    public int speed_Top = 3;  
    public int Lifes = 3;   
    int Points = 0;    

    public EasyGame()      
    {
        InitializeComponent();      


        EasyTimer.Enabled = true;   
        EasyLife.Text = Lifes.ToString();   

        this.FormBorderStyle = FormBorderStyle.Sizable; 
        this.TopMost = true;        
        this.Bounds = Screen.PrimaryScreen.Bounds;  
        Cursor.Hide(); 

        Bar.Top = Playground.Bottom - (Playground.Bottom / 10); 

    }

    private void EasyTimer_Tick(object sender, EventArgs e) 
    {
        Bar.Left = Cursor.Position.X - (Bar.Width / 2); 

        Ball.Left += speed_Left; 
        Ball.Top += speed_Top; 

        if (EasyLife.Text == "0")
        {

            EasyTimer.Stop();  

            if (Lifes == 0)

                EasyTimer.Stop();

            this.Hide();    

            Cursor.Show();  
            string info = "lost ...";       
            string snippet = "all lifes have been used...";   
            MessageBoxButtons digits = MessageBoxButtons.OK;    
            DialogResult ending;     

            ending = MessageBox.Show(info, snippet, digits); 

            if (ending == System.Windows.Forms.DialogResult.OK)
            {
                EasyTimer.Stop();  
                EndLife Final = new EndLife();  

                Final.Show();   
            }
        }

        if (Ball.Bottom >= Bar.Top && Ball.Bottom <= Bar.Bottom && Ball.Left    >= Bar.Left && Ball.Right <= Bar.Right)

        {


            speed_Top += 3;     
            speed_Left += 3;    
            speed_Top = -speed_Top;     
            Points += 1;        
            Easygamepoints.Text = Points.ToString();       

            }

            if (Ball.Left <= Playground.Left)  
            {
                speed_Left = -speed_Left;   
            }
            if (Ball.Right >= Playground.Right) 
            {
                speed_Left = -speed_Left;   
            }
            if (Ball.Top <= Playground.Top) 
            {
                speed_Top = -speed_Top; 
            }
            if (Ball.Bottom >= Playground.Bottom) 
            {

                EasyTimer.Stop();     

                Cursor.Show();     
                Lifes -= 1;  
                string message = "You have lost one of your lives...";
                string caption = "You died, Life Lost...";

                MessageBoxButtons buttons = MessageBoxButtons.OK;
                DialogResult result;   
                result = MessageBox.Show(message, caption, buttons);



                if (result == System.Windows.Forms.DialogResult.OK)        
                {
                    EasyTimer.Enabled = false; 
                    Cursor.Hide();  

                    EasyLife.Text = Lifes.ToString();

                    EasyTimer.Start();  

                    speed_Left = 5; 
                    speed_Top = 3;  

                    Ball.Top = 69;     
                    Ball.Left = 128;                             
                }
            }
        }
    }
}

Due to Confusion, this body has been edited to allow better understanding of the objective that needs to be met.`enter code here`
  • Basically the game just restarts from the "If Ball.Bottom >= Playground.Bottom even if the Life has been depleted to = 0 and the Gameover Window shows. Any help will be appreciated. – Going Ham Shaw Dec 30 '15 at 16:54
  • You seem to have cut your code a bit short in the sample you pasted. I can see part of the final "if", but not what happens afterwards – Andrew Williamson Dec 30 '15 at 16:58
  • I would swap the order of the two "if" statements - check first if they have any lives left, and only then should you show them the message box to start another round. – Andrew Williamson Dec 30 '15 at 17:02
  • Ill try your solution and see if that helps: Basically you suggest that I switch the if (Life == 0) to the beginning to see if there are lives, followed by what ever code? – Going Ham Shaw Dec 30 '15 at 18:33
  • Yeah, see what happens. I suspect that the timer is being restarted in the first "if", and doesn't have enough time to process the stop signal that you send it from the second "if". Changing the order of the if statements will mean the timer is only restarted if you have lives left. – Andrew Williamson Dec 30 '15 at 18:38
  • Would it be better if I were to use Else if instead of If on the following statement of (Ball.Bottom <= Playground.Bottom) -- So If (Life == 0) then followed by (Ball.Bottom <= Playground.Bottom) I am just trying it now and ill get back to you, thank you for your help its much appreciated. – Going Ham Shaw Dec 30 '15 at 18:41
  • Ok I have changed the Location of if (Lifes == 0) to the beginning however there still seems to be an issue of the code restarting, I am new to C# on Visual Studio, if there any code that i can use like in visual basic to break the code or end once the parameter has been met of 0 lifes remaining? – Going Ham Shaw Dec 30 '15 at 18:53
  • I have looked online and there seems to be a return type but it does not work on the code that i have, apparently no return type has been defined. – Going Ham Shaw Dec 30 '15 at 18:53
  • 1
    There is a lot of your code that I can't see. Where are the last lines of this method? Where is EasyTimer initialized and started from, and most importantly, when do you call Close on the main form? – Andrew Williamson Dec 30 '15 at 19:10
  • The code that has been shown at the top in my first post is correct, however there are other lines of code before the code that is shown. The EasyTimer.Start(); starts before the first line of code shown and just after the Initialize components; At the very bottom of the code there are just closing brackets. The call close occurs when the Player has lost all lives, which should be automatic when the Life counter reaches 0 a message box appears and alerts – Going Ham Shaw Dec 30 '15 at 19:44
  • the player that all lifes have been lost. Which the player presses the "OK" button which should Hide the EasyGame window to then bring up the gameover form called EndLife. – Going Ham Shaw Dec 30 '15 at 19:44
  • My current code has replaced the previous code that was shown in my first comment. With removed annotations for better reading. – Going Ham Shaw Dec 30 '15 at 20:03
  • Ok, sorry, you misunderstood my other comment. The if statement has been brought too far up. I'm going to post as an answer, even though I don't know if it is the solution, so I can show you what I meant – Andrew Williamson Dec 30 '15 at 20:12
  • Thank you man, I am seriously clawing my hair out, being baffled by what the solution could be. – Going Ham Shaw Dec 30 '15 at 20:22

1 Answers1

0

NB: Potentially not the answer, but I can only post so many characters in the comments section, so it's here instead.

private void EasyTimer_Tick(object sender, EventArgs e) 
{
    Bar.Left = Cursor.Position.X - (Bar.Width / 2); 

    Ball.Left += speed_Left; 
    Ball.Top += speed_Top; 


    if (Ball.Bottom >= Bar.Top && Ball.Bottom <= Bar.Bottom && Ball.Left    >= Bar.Left && Ball.Right <= Bar.Right)
    {
        speed_Top += 3;     
        speed_Left += 3;    
        speed_Top = -speed_Top;     
        Points += 1;        
        Easygamepoints.Text = Points.ToString();
    }
    if (Ball.Left <= Playground.Left)  
    ...

    ...
    if (Ball.Bottom >= Playground.Bottom) 
    {
        EasyTimer.Stop();    
        Cursor.Show();

        Lifes -= 1;

        if (Lifes == 0)
        {
            DialogResult ending;
            MessageBox.Show("all lifes have been used...", "lost ...", MessageBoxButtons.OK); 

            EndLife Final = new EndLife();  
            Final.Show();
            // You need to return whether or not the user wants to play another game from this form
            // See http://stackoverflow.com/questions/280579/how-do-i-pass-a-value-from-a-child-back-to-the-parent-form
            if (Final.NewGame == true)
            {
                speed_Left = 5; 
                speed_Top = 3;  
                Ball.Top = 69;     
                Ball.Left = 128;
                Lifes = 3;
                EasyTimer.Start();  
            }
            else
                Close();
        }
        else
        {  
            DialogResult result = MessageBox.Show("You have lost one of your lives...", "You died, Life Lost...", MessageBoxButtons.OK);

            if (result == DialogResult.OK)        
            {
                EasyTimer.Start();
                Cursor.Hide();
                EasyLife.Text = Lifes.ToString();
                speed_Left = 5; 
                speed_Top = 3;
                Ball.Top = 69;     
                Ball.Left = 128;                             
            }
            else
                Close();
        }
    }
}

The important thing to notice here, is that you must check if the user wants to play another game or not, from the Final form. If they don't, then Close the main form. The game will never end if you don't close the form.

Andrew Williamson
  • 8,299
  • 3
  • 34
  • 62
  • The thing is, if I close the game as in this.Close(); the Points are not carried over to the Gameover screen, which means that the component that I have used does not Increment the points that are accumulated from the ball hitting the bar.bottom. i will try to work around this. – Going Ham Shaw Dec 30 '15 at 20:47
  • After analysing your code and trying to implement the changed within my own code, there seems to be an issue with "EndLife Final = new EndLife(); Final.Show(); if (Final.NewGame == true) this here :---> if (Final.NewGame == true) Endlife does not contain definition for NewGame or does not have extension method. – Going Ham Shaw Dec 30 '15 at 20:51
  • Yeah read the comment immediately above it. You need to define a NewGame property in the EndLife form. – Andrew Williamson Dec 30 '15 at 21:24
  • I cannot seem to Implement a correct property in the EndLife form, I have tried to my best of my ability "EasyGame Final = new EasyGame(); and also EndLife NewGame = new EndLife(); What am i doing wrong? – Going Ham Shaw Dec 30 '15 at 22:06
  • Hi Andrew Williamson, Sorry to be a pest. I would like to know what do you mean about define a new property in the EndLife form, I have read the Comment link you put into the code, and I still do not understand what do you mean. Is it possible if you can direct me in the correct path and give some insight in what you mean? I have also looked on google and there are different examples which i have tried within my code and none of them seem to work. Sorry again for being a pest. – Going Ham Shaw Jan 02 '16 at 14:47