0 Replies - 1496 Views - Last Post: 09 March 2013 - 04:01 PM Rate Topic: -----

#1 babyyphx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 09-March 13

Simple implementation of Pong

Posted 09 March 2013 - 04:01 PM

I've started off with a bouncing ball program in c# and I was wondering how to implement a rectangular paddle.

The initial code started like this :
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;

// added for GraphicsPath
using System.Drawing.Drawing2D;


namespace MyBouncingBallIIA
{
    public partial class MyBouncingBallIIA : Form
    {
        public MyBouncingBallIIA()
        {
            InitializeComponent();

            Text = "Bouncing Ball";
            Timer timer = new Timer();
            timer.Interval = iTimerInterval;
            timer.Tick += new EventHandler(TimerOnTick);
            timer.Start();

            // need to set up initial screen size information
            // before timer expires, which assumes that the
            // screen size stuff has been initialized
            this.onresize(EventArgs.Empty);

        }

        protected const int iTimerInterval = 10;    // In milliseconds
        protected const int iBallSize = 16;         // As fraction of client area
        protected int iMoveSize = 8;          // As fraction of ball size

        Bitmap bitmap;
        int xCenter, yCenter;
        int cxRadius, cyRadius, cxMove, cyMove, cxTotal, cyTotal;

        Color ballColor = Color.Blue;

        void DrawBall(Graphics grfx, Rectangle rect)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddEllipse(rect);

            PathGradientBrush pgbrush = new PathGradientBrush(path);
            pgbrush.CenterPoint = new PointF((rect.Left + rect.Right) / 3,
                (rect.Top + rect.Bottom) / 3);
            pgbrush.CenterColor = Color.White;
            pgbrush.SurroundColors = new Color[] { ballColor };

            grfx.FillRectangle(pgbrush, rect);
        }

        private void ReverseDirection()
        {
            cxMove = -cxMove;
            cyMove = -cyMove;
        }

        private void Bounce_MouseClick(object sender, EventArgs e)
        {
            ReverseDirection();
        }

        private void Bounce_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.R:
                    ballColor = Color.Red;
                    onresize(EventArgs.Empty);
                    break;

                case Keys.B:
                    ballColor = Color.Blue;
                    onresize(EventArgs.Empty);
                    break;

                case Keys.Y:
                    ballColor = Color.Yellow;
                    onresize(EventArgs.Empty);
                    break;

                    case Keys.S:
                    iMoveSize -= 2;
                    if (iMoveSize <2 )  iMoveSize = 8;
                    onresize(EventArgs.Empty);
                    break;

            }
        }
        
        protected override void onresize(EventArgs e)
        {
            Graphics grfx = CreateGraphics();
            grfx.Clear(BackColor);

            float fRadius = Math.Min(ClientSize.Width / grfx.DpiX,
                                     ClientSize.Height / grfx.DpiY)
                                          / iBallSize;

            cxRadius = (int)(fRadius * grfx.DpiX);
            cyRadius = (int)(fRadius * grfx.DpiY);

            grfx.Dispose();

            cxMove = Math.Max(1, cxRadius / iMoveSize);
            cyMove = Math.Max(1, cyRadius / iMoveSize);

            cxTotal = 2 * (cxRadius + cxMove);
            cyTotal = 2 * (cyRadius + cyMove);

            bitmap = new Bitmap(cxTotal, cyTotal);

            grfx = Graphics.FromImage(bitmap);
            grfx.Clear(BackColor);

            DrawBall(grfx, new Rectangle(cxMove, cyMove, 2 * cxRadius, 2 * cyRadius));

            grfx.Dispose();

            xCenter = ClientSize.Width / 2;
            yCenter = ClientSize.Height / 2;
        }

        public void TimerOnTick(object obj, EventArgs ea)
        {
            Graphics grfx = CreateGraphics();

            grfx.DrawImage(bitmap, xCenter - cxTotal / 2,
                           yCenter - cyTotal / 2,
                           cxTotal, cyTotal);

            grfx.Dispose();

            xCenter += cxMove;
            yCenter += cyMove;

            if ((xCenter + cxRadius >= ClientSize.Width) ||
                (xCenter - cxRadius <= 0))
                cxMove = -cxMove;

            if ((yCenter + cyRadius >= ClientSize.Height) ||
                (yCenter - cyRadius <= 0))
                cyMove = -cyMove;
        }

    }
}



Now I figured if I wanted to draw a paddle, it should look somewhat similar to the DrawBall function
        void DrawPaddle(Graphics grfx, Rectangle rect)
        {
            GraphicsPath path2 = new GraphicsPath();
            path2.AddRectangle(rect);

            PathGradientBrush pgbrush2 = new PathGradientBrush(path2);
            pgbrush2.CenterPoint = new PointF(1,1);
            pgbrush2.CenterColor = Color.White;
            pgbrush2.SurroundColors = new Color[] { Color.Red };

            grfx.FillRectangle(pgbrush2, rect);


        }



However when I try to call the DrawPaddle function in the onresize method, the paddle is constantly moving when I run the program. I thought that filling in integers instead of cxMove , cyMove, etc would keep the paddle in one location, but it didn't. How do I initialize and keep the paddle in a specific location instead of having it constantly move with the ball?

Is This A Good Question/Topic? 0
  • +

Page 1 of 1