Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7981077
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T10:15:51+00:00 2026-06-04T10:15:51+00:00

So i working on a breakout game and so far everything has been going

  • 0

So i working on a breakout game and so far everything has been going well, but when i make the the ball picturebox go off screen i seem to getting an unlimited amount of message boxes.

here is the code snippet

   using System;
   using System.Collections.Generic;
   using System.ComponentModel;
   using System.Data;
   using System.Drawing;
   using System.Linq;
   using System.Text;
   using System.Windows.Forms;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
    List<Brick> bricks = new List<Brick>();
    Ball _ball;
    Paddle _paddle;
    DateTime startTime = DateTime.Now;
    DateTime startTime2 = DateTime.Now;

    float ballVelocityX = 0;
    float ballVelocityY = -1;
    float ballSpeed = 20;

    public Form1()
    {
        InitializeComponent();
        timer1.Enabled = true;
        timer2.Enabled = true;
        startTime = DateTime.Now;
        System.Media.SoundPlayer sndPlayer;
        sndPlayer = new System.Media.SoundPlayer(Properties.Resources._01_Calm_1);
        foreach (PictureBox brick in panel1.Controls)
        {
            if (brick.Name != "ball" && brick.Name != "paddle")
                bricks.Add(new Brick(brick));
            else if (brick.Name == "ball")
                _ball = new Ball(brick);
            else if (brick.Name == "paddle")
                _paddle = new Paddle(brick);
            //sndPlayer.Play();

        }
    }

    private void gameTimer_Tick(object sender, EventArgs e)
    {
        checkBrickCollision();
        checkPaddleCollision();
        checkBallScreenBounds();

        ball.SetBounds(ball.Location.X + (int)(ballVelocityX * ballSpeed),
            ball.Location.Y + (int)(ballVelocityY * ballSpeed),
            ball.Size.Width, ball.Size.Height);
        paddle.SetBounds(Cursor.Position.X - panel1.Location.X, paddle.Location.Y,
            paddle.Size.Width, paddle.Size.Height);
    }

    private void checkBallScreenBounds()
    {
        if (_ball.BallRectangle.Location.X + 25 > panel1.Size.Width)
            ballVelocityX *= -1;
        else if (_ball.BallRectangle.Location.X < 0)
            ballVelocityX *= -1;
        else if (_ball.BallRectangle.Location.Y < 0)
            ballVelocityY *= -1;
        else if (_ball.BallRectangle.Location.Y > panel1.Size.Height)
            ballOffScreen();
    }

    private void ballOffScreen()
    {
        new_start();
        Startnew startnew = new Startnew();
        startnew.Show();
        this.Close();
    }

    private void new_start()
    {            
        TimeSpan elapsedTime = DateTime.Now - startTime2;
        DialogResult result;
        result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes + elapsedTime.Seconds
            + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo);
        if (result == DialogResult.No)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
        else if (result == DialogResult.Yes)
        {
            this.Close();
        }

        //MessageBox.Show("Total time is " + elapsedTime.Minutes + ":" + elapsedTime.Seconds);

    }

    private void checkPaddleCollision()
    {
        int tmpBallVelocityX = _paddle.CheckPaddleMovement();

        if (_ball.BallRectangle.IntersectsWith(_paddle.PaddleRectangle))
        {
            ballVelocityX = tmpBallVelocityX;
            ballVelocityY *= -1;
        }
    }

    private void checkBrickCollision()
    {
        Rectangle ballRectangle = _ball.BallRectangle;

        foreach (Brick brick in bricks)
            if (brick.IntersectWith(ballRectangle))
            {
                bricks.Remove(brick);
                ballVelocityX *= -1;
                ballVelocityY *= -1;
                break;
            }
    }

    private void panel1_MouseEnter(object sender, EventArgs e)
    {
        Cursor.Hide();
    }

    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        Cursor.Show();
    }
    public void Form_closse(object sender, FormClosedEventArgs e)
    {
        //ProgramInfo.ProgramState = State.Splash_Screen;
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }
    private void timer1_tick(object sender, EventArgs e)
    {
        LabelTime.Text = "It is " + DateTime.Now;
    }
    private void timer2_tick(object sender, EventArgs e)
    {
        TimeSpan elapsedTime = DateTime.Now - startTime;
        labelElapsedTime.Text = "Time Elapsed " + elapsedTime.Minutes + ":" + elapsedTime.Seconds;
      }
    }
  }
    ` #**EDIT**#`
    `**because things were getting a bit confusing i posted all my code.**`

ive done this with a button and it works fine and all, but i need to pop up only once. is there any way to only have it called once?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T10:15:52+00:00Added an answer on June 4, 2026 at 10:15 am

    if new_start() is part of Form1,

    Following line looks like culprit, but more code is required for closer look.

    if (result == DialogResult.No)
        {
            Form1 form1 = new Form1();
            form1.Show(); //This line is creating endless messagebox on selecting NO
        }
    

    UPDATE Based on information in comments,

    you can control the new_start function with flag

    private bool checkNewGame = false;
    private void new_start()
    {        
        if(checkNewGame) return; 
        checkNewGame = true;    
        TimeSpan elapsedTime = DateTime.Now - startTime2;
        DialogResult result;
        result = MessageBox.Show("Your total time is " + ":" + elapsedTime.Minutes +   elapsedTime.Seconds
            + " would you like to play aigan?", "Application1", MessageBoxButtons.YesNo);
        if (result == DialogResult.No)
        {
            Form1 form1 = new Form1();
            form1.Show();
        }
        else if (result == DialogResult.Yes)
        {
           checkNewGame = true; 
           this.Close();
    
        }
    

    UPDATE 2

    Instead of

    if (result == DialogResult.No)
    {
        Form1 form1 = new Form1();
        form1.Show(); //This line is creating endless messagebox on selecting NO
    }
    

    do something like

    if (result == DialogResult.No)
    {
        this.Clear(); // Create new function Clear, and clear all the Form state.
    }
    
    void clear()
    {
      ... //RESET all form state
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Working on a website that has Employee and Branch entities, using a database table
Working on a new app and using restful-authentication. I was trying to make it
working on my paypal integration and its going great - I was wondering that
I'm working on the Breakout assignment from the Stanford lectures on iTunes U (still
I'm working on migrating a fairly large site to a new framework, but we're
Working with MySQL database. I have a store table. The store table has at
I'm working on an assignment for uni where I have to create a Breakout
So i am currently working on a simple game project that most people start
working on using a formula ive been using for a while to find distances
Working on a bunch of forms at the moment and I'm finding that I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.