Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42220

Bouncing Ball not working properly

$
0
0
Hello , Im creating a game like bounce , for that i was creating a code for continues bouncing of ball , it will bounce after getting to bottom of form
i have added a acceleration to make bouncing realistic , but each time when it rebounds from bottom of form it does not attain same height as before and after same rebounds it is almost at bottom ( like real ball as it will keep bouncing at low height each time , but i dont want this to happen )

my second problem is , im redrawing ball with form paint event each 100 ms (its velocity is also calculated with 100 ms interval) , but as velocity of ball increases , it looks like skipping of frames (lagging) of ball , it does not look smooth , i want to fix that too ,

im currently using normal c # to create this game , can someone give me example of creating game with XNA framework (or other like it integrated with VS2012)

Here is my code:_ ( this is whole code , u can add it to any form with button1 and test it )
Code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bounce
{
    public partial class Form1 : Form
    {
        int dx;
        int dy;
        int x;
        int y;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            Random rnd = new Random();
            dx = 1;
            dy = 1;
            x = 100 ; //rnd.Next(0, this.ClientSize.Width - 50);
            y = 0;// rnd.Next(0, this.ClientSize.Height - 50);


        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.Clear(this.BackColor);
            e.Graphics.FillEllipse(Brushes.Black, x, y, 50, 50);
            e.Graphics.DrawEllipse(Pens.Black, x, y, 50, 50);
        }

        private void button1_Click(object sender, EventArgs e)
        {

            if (button1.Text == "start")
            {
                ToBounce = true;
                System.Threading.Thread newThread;
                newThread = new System.Threading.Thread(startmechBounceOnFormBottem);
                newThread.Start();

                // timer1.Enabled = true;
                button1.Text = "stop";
                return;
            }
            else if (button1.Text == "stop")
            {
                ToBounce = false;
                //timer1.Enabled = false;
                button1.Text = "start";
                return;
            }

        }

        Boolean ToBounce;
        Boolean GoingUP;
        private void startmechBounceOnFormBottem()
        {
            GoingUP = false;
            int a = 1;
            int uY = 0;
            ToBounce = true;
            while (ToBounce == true)
            {
                Thread.Sleep(100);
                if (y >= 0 && y < this.ClientSize.Height - 50)
                {
                    if (GoingUP == false)
                    {
                        uY = uY + a;
                        if (y + uY > this.ClientSize.Height - 50)
                        {
                            y = this.ClientSize.Height - 50;
                            this.Invalidate();
                        }
                        else
                        {
                            y = y + uY;
                        }
                    }
                    else if (GoingUP == true)
                    {
                        if (uY <= 0)
                        {
                            GoingUP = false;
                        }
                        else
                        {
                            uY = uY - a;
                            y = y - uY;
                        }

                    }

                }
                else if (y >= this.ClientSize.Height - 50)
                {
                    y = this.ClientSize.Height - 50;
                    this.Invalidate();
                    uY = uY - a;
                    y = y - uY;
                    GoingUP = true;
                }

                this.Invalidate();

           

            }

        }
    }
}


Viewing all articles
Browse latest Browse all 42220

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>