Click here to Skip to main content
15,914,924 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I need when the ball passes from the board then show a defeat`Game Over.I don't know how to write that code.Please help me.
Thanks.

What I have tried:

class Ball
    {
        public int x;
        public int y;
        public int vx = 3;
        public int vy = 3;
        public int r;
        public Graphics gf;
        public Game game;
        public SolidBrush myColor;
        public Ball(int x,int y,int vx,int vy,int r,Graphics gf,Game game) 
        {
            this.x = x;
            this.y = y;
            this.vx = vx;
            this.vy = vy;
            this.r = r;
            this.gf = gf;
            this.game = game;
            myColor = new SolidBrush(Color.IndianRed);
        }
        public void Draw()
        {
            this.gf.FillEllipse(myColor, x, y, r, r);
        }
        public void Move()
        {
            this.x += this.vx;
            this.y += this.vy;
            if(this.x <= 0 || this.x > game.ClientSize.Width - r)
            {
                Random rnd = new Random();
                int r = rnd.Next(0, 256);
                int g = rnd.Next(0, 256);
                int b = rnd.Next(0, 256);
                SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b));
                this.myColor = guyn;
                vx *= -1;
            }
            if(this.y <= 0)
            {
                Random rnd = new Random();
                int r = rnd.Next(0, 256);
                int g = rnd.Next(0, 256);
                int b = rnd.Next(0, 256);
                SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b));
                this.myColor = guyn;
                this.vy *= -1;
            }
            this.Draw();
        }

        public bool hits(Board board)
        {
            int x1 = this.x + this.r / 2;
            int x2 = board.x + board.w / 2;
            int y1 = this.y;
            int y2 = board.y;
            int y_dist = Math.Abs(y1 - y2);
            int x_dist = Math.Abs(x1 - x2);
            if(y_dist <= r/2 + board.h/2 && x_dist <= board.w/2 + r / 2)
            {
                return true;
            }
            else
            {
                return false;
            }  
        }
    }


class Board
    {
        public int x;
        public int y;
        public int w;
        public int h;
        public Graphics gf;
        public Game game;
        public SolidBrush MyColor;
        public Board(int x, int y, int w, int h, Graphics gf, Game game)
        {
            this.x = x;
            this.y = y;
            this.w = w;
            this.h = h;
            this.gf = gf;
            this.game = game;
            MyColor = new SolidBrush(Color.Black);
        }
        public void draw()
        {
            gf.FillRectangle(MyColor, x, y, w, h);
        }

        public void gna(string v)
        {

            if (v == "left" && x > 0)
            {
                this.x -= 10;
            }
            else if (v == "right" && x < this.game.ClientSize.Width - 85)
            {
                this.x += 10;
            }
            this.draw();
        }
    }


public partial class Game : Form
    {
        Graphics gf;
        Ball ball;
        Board board;
        public Game()
        {
            InitializeComponent();
            this.gf = this.CreateGraphics();
            this.board = new Board(this.ClientSize.Width / 2 - 40, this.ClientSize.Height - 60, 80, 20,gf,this);
            this.ball = new Ball(this.ClientSize.Width / 2 - 10, this.ClientSize.Height/2 - 10, 3, 3, 20, gf, this);
        }

        private void Game_Paint(object sender, PaintEventArgs e)
        {
            this.board.draw();
            this.ball.Draw();
        }

        private void Game_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left:
                case Keys.A:
                    this.board.gna("left");
                    break;
                case Keys.Right:
                case Keys.D:
                    this.board.gna("right");
                    break;

            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.gf.FillRectangle(Brushes.White, 0, 0, this.ClientSize.Width, this.ClientSize.Height);
            this.board.draw();
            this.ball.Draw();
            this.ball.Move();
            if (this.ball.hits(this.board))
            {
                Random rnd = new Random();
                int r = rnd.Next(0, 256);
                int g = rnd.Next(0, 256);
                int b = rnd.Next(0, 256);
                SolidBrush guyn = new SolidBrush(Color.FromArgb(255, r, g, b));
                this.ball.myColor = guyn;
                this.board.MyColor = guyn;
                this.ball.vy *= -1;
            }
        }

    }
Posted
Updated 12-Mar-18 22:05pm
v2

1 solution

This looks a bit dated, but maybe it will be useful to you: How to build a breakout style game with animation and sound.[^]
Also see the answer here: Making breakout game[^]

You know the board.w and board.h, so simply check if the ball position is greater than these values. Also check if the ball position is less than board.x and board.y.

If you want a 'Game Over' screen, then just create a new Form and set the background image property to a picture, this can be a .jpg, .gif or .png image.
See: Control.BackgroundImage Property (System.Windows.Forms)[^]
 
Share this answer
 
v4
Comments
Suren97 11-Mar-18 12:10pm    
It didn't help me
Dave Kreskowiak 11-Mar-18 12:25pm    
No, you're not going to find code to "copy'n'paste" into your own and it'll magically work. You MUST analyze the example code and understand what it's doing so you can rewrite your code to do the same thing.
Suren97 11-Mar-18 13:08pm    
i need Game over code,i didn't find it there.
Dave Kreskowiak 11-Mar-18 17:04pm    
You have code to determine if the ball "falls off" the board? It's just another if statement to see if there are any lives left. If that condition it true, put up your "game over" form.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900