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 5997211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:13:52+00:00 2026-05-23T00:13:52+00:00

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Drawing.Drawing2D; using System.Text;

  • 0
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;


namespace Pong
{
    public partial class Form1 : Form
    {
        //variables
        float ballX = 255,
        ballY = new Random().Next(1, 280),
        ballSpeed = 3.0f,
        paddleHeight = 80,
        cpuHeight = 80,
        cpuSkill = 2.0f;
        int hits = 0;
        Timer t = new Timer();
        bool directionX = true,
        directionY = true,
        ingame = false;
        int player1score = 0, player2score = 0;
        SolidBrush drawBrush = new SolidBrush(Color.White);
        String beginString = "Welcome to Pong! \n" + "by Javier!";

        public Form1()
        {
            //Initializes the component
            InitializeComponent();
        }

        private void normalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menuStrip1.Visible = false;
            ingame = true;
            t = new Timer();
            hits = 0;
            ballX = 255;
            ballY = new Random().Next(1, 280);
            ballSpeed = 3.0f;
            directionY = true;
            directionX = true;

            //Sets up timer
            t.Interval = 5;
            t.Tick += new EventHandler(t_Tick);
            t.Start();

            //shows picture
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

            //Creates mouse movement
            this.MouseMove += new MouseEventHandler(Form1_MouseMove);
        }

        private void destroyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void battleToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = true;
            mediumToolStripMenuItem.Checked = false;
            hardToolStripMenuItem.Checked = false;
            pongmasterToolStripMenuItem.Checked = false;
            cpuSkill = 0.0f;
        }

        private void mediumToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = true;
            hardToolStripMenuItem.Checked = false;
            pongmasterToolStripMenuItem.Checked = false;
            cpuSkill = 2.5f;
        }

        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = false;
            hardToolStripMenuItem.Checked = true;
            pongmasterToolStripMenuItem.Checked = false;
            cpuSkill = 5.5f;
        }

        private void pongmasterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            easyToolStripMenuItem.Checked = false;
            mediumToolStripMenuItem.Checked = false;
            hardToolStripMenuItem.Checked = false;
            pongmasterToolStripMenuItem.Checked = true;
            cpuSkill = 6.5f;
        }

        void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            //Moves the paddle to the mouse's y-coordinate
            paddleHeight = e.Y - 40;
        }

        void t_Tick(object sender, EventArgs e)
        {
            //Causes ball to bounce of windows
            if (ballX + 10 >= 439)
            {
                //directionX = false;

            }
            if (ballX - 10 <= 1)
                directionX = true;
            if (ballY + 10 >= 263)
                directionY = false;
            if (ballY - 10 <= 1)
                directionY = true;

            //movement of ball
            if (directionX == true)
                ballX += ballSpeed;
            if (directionY == true)
                ballY += ballSpeed;
            if (directionX == false)
                ballX -= ballSpeed;
            if (directionY == false)
                ballY -= ballSpeed;

            //AI Vs player
            if (directionY == true && ballX >= 225 && cpuHeight + 40 < ballY)
                cpuHeight += cpuSkill;
            if (directionY == false && ballX >= 225 && cpuHeight + 40 > ballY)
                cpuHeight -= cpuSkill;

            // Player vs paddle
            if (directionX == false && ballY + 10 > paddleHeight && ballY - 10 < paddleHeight + 80 && ballX <= 20)
            {
                directionX = true;
                hits += 1;




            }
            if (directionX == true && ballY + 10 > cpuHeight && ballY - 10 < cpuHeight + 80 && ballX >= 421)
            {
                directionX = false;
                hits += 1;
            }

            if (((ballX - 10) <= 1) || ((ballX + 20) >= 450))                 
            {
                if( (ballX - 10)<=1)
                    ++player2score;
                if((ballX + 20) >= 450)
                    ++player1score;

            }



            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.PixelOffsetMode = PixelOffsetMode.HighQuality;
            Pen p = new Pen(Color.Black);
            SolidBrush b = new SolidBrush(Color.Orange);
            SolidBrush drawBrush = new SolidBrush(Color.White);
            if (ingame == true)
            {
                p.Width = 2;
                g.DrawEllipse(p, ballX - 10, ballY - 10, 20, 20);
                g.FillEllipse(b, ballX - 10, ballY - 10, 20, 20);
                g.DrawRectangle(p, 5, paddleHeight, 6, 80);
                g.DrawRectangle(p, 431, cpuHeight, 6, 80);
                b.Color = Color.Blue;
                g.FillRectangle(b, 431, cpuHeight, 6, 80);
                g.FillRectangle(b, 5, paddleHeight, 6, 80);
                g.DrawString(string.Format("{0} : {1}",player1score, player2score), new Font(FontFamily.GenericMonospace, 14),drawBrush,215, 10);


            }
            if (ingame == false)
                g.DrawString(beginString, new Font(Font.FontFamily, 16), new SolidBrush(Color.Blue), new Point(140, 140), StringFormat.GenericDefault);














        }
    }
}
  • 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-05-23T00:13:53+00:00Added an answer on May 23, 2026 at 12:13 am

    For that AI, I would suggest creating a random number to make the AI’s paddle vary rather than being +-2. The AI’s paddle is currently still “aligned” to the ball, its just offset by a number. Not really an AI when you are cloning the coordinates.

    For the scoring system, make sure you added up the dimensions properly. It is very hard to make sense of all the different pixels plastered in the code. How big is the actual playing area?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; using System.Threading; namespace ClassLibrary { public
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; namespace ConsoleApplication1 { public
here's the code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using
My Code: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace GenericCount { class Program {

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.