c# pong game

posted in phil67rpg's Blog
Published December 25, 2018
Advertisement

I am working on a pong game using c# and gdi+. here is my code so far.


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;

namespace winpong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

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

        int move = 0;
        float comp_move = 0.0f, comp_velocity = 1.0f, screen_height = 250.0f, flag = 0.0f;
        
        public void Form1_KeysDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)//key up
            {
                move-=5;
                if (move <= -250) 
                {
                    move = -250;
                }
            }
            
            if (e.KeyCode == Keys.Down)//key down
            {
                move+=5;
                if(move >= 210)
                {
                    move = 210;
                }
            }
            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comp_velocity = (comp_move < screen_height && flag == 0.0f) ? (comp_velocity = 5.0f) : (comp_velocity = -5.0f);
           
            comp_move = (comp_velocity == 5.0f) ? (comp_move += 5.0f) : (comp_move -= 5.0f);
            
            if(comp_move >= screen_height-40.0f)
            {
                flag = 1.0f;
            }
            
            if(comp_move <= -screen_height)
            {
                flag = 0.0f;
            }
            Invalidate();
        }

        float x = 0.0f, y = 0.0f, xstep = 5.0f, ystep = 5.0f;
       
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (x >= 395.0f || x <= -390.0f) 
            {
                xstep = -xstep;
            }
            if (y >= 270.0f || y <= -290.0f) 
            {
                ystep = -ystep;
            }
            x += xstep;
            y += ystep;
            Invalidate();
        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            RectangleF rect1 = new RectangleF(0, 250 + comp_move, 35, 100); //blue paddle
            Rectangle rect2 = new Rectangle(750, 250 + move, 35, 100); //red paddle
            RectangleF rect3 = new RectangleF(390+x, 290+y, 10, 10); //white ball
            g.FillRectangle(blueBrush, rect1);
            g.FillRectangle(redBrush, rect2);
            g.FillRectangle(whiteBrush, rect3);
            g.Dispose();
        }
    }
}

 

Previous Entry 1942 plane game
Next Entry c# tic tac toe game
1 likes 15 comments

Comments

phil67rpg

I am almost done with my pong game all I have to do is implement the scoring algorithm.


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;

namespace winpong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
        {
            // AABB 1
            float x1Min = x;
            float x1Max = x + oWidth;
            float y1Max = y + oHeight;
            float y1Min = y;

            // AABB 2
            float x2Min = xTwo;
            float x2Max = xTwo + oTwoWidth;
            float y2Max = yTwo + oTwoHeight;
            float y2Min = yTwo;

            // Collision tests
            if (x1Max < x2Min || x1Min > x2Max) return false;
            if (y1Max < y2Min || y1Min > y2Max) return false;

            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    
        float move = 0.0f;
        float comp_move = 0.0f, comp_velocity = 1.0f, screen_height = 250.0f, flag = 0.0f;
        
        public void Form1_KeysDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)//key up
            {
                move-=5.0f;
                if (move <= -250.0f) 
                {
                    move = -250.0f;
                }
            }
            
            if (e.KeyCode == Keys.Down)//key down
            {
                move+=5.0f;
                if(move >= 210.0f)
                {
                    move = 210.0f;
                }
            }
            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comp_velocity = (comp_move < screen_height && flag == 0.0f) ? (comp_velocity = 5.0f) : (comp_velocity = -5.0f);
           
            comp_move = (comp_velocity == 5.0f) ? (comp_move += 5.0f) : (comp_move -= 5.0f);
            
            if(comp_move >= screen_height-40.0f)
            {
                flag = 1.0f;
            }
            
            if(comp_move <= -screen_height)
            {
                flag = 0.0f;
            }
            Invalidate();
        }

        float x = 0.0f, y = 0.0f, xstep = 5.0f, ystep = 5.0f;
       
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (x >= 395.0f || x <= -390.0f) 
            {
                xstep = -xstep;
            }
            if (y >= 270.0f || y <= -290.0f) 
            {
                ystep = -ystep;
            }
            x += xstep;
            y += ystep;
            Invalidate();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 750.0f;
            float yTwo = 250.0f + move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 0.0f;
            float yTwo = 250.0f + comp_move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            RectangleF rect1 = new RectangleF(0, 250 + comp_move, 35, 100); //blue paddle
            RectangleF rect2 = new RectangleF(750, 250 + move, 35, 100); //red paddle
            RectangleF rect3 = new RectangleF(390+x, 290+y, 10, 10); //white ball
            g.FillRectangle(blueBrush, rect1);
            g.FillRectangle(redBrush, rect2);
            g.FillRectangle(whiteBrush, rect3);
            g.Dispose();
        }
    }
}

 

December 30, 2018 11:22 PM
Brain

This is looking good so far.

for added challenge, how would you implement realistic bouncing of the ball at the inverse of the angle it hits a surface at, eg like a pool ball would in a game of pool?

hint: read up on vectors and normals, this will help.

i look forward to the next instalment!

have fun!

December 31, 2018 03:53 PM
phil67rpg

thanks a lot I also have taken a course in linear algebra in college. 

January 01, 2019 03:29 AM
phil67rpg

here is my pong game with scoring algorithm.


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;

namespace winpong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
        {
            // AABB 1
            float x1Min = x;
            float x1Max = x + oWidth;
            float y1Max = y + oHeight;
            float y1Min = y;

            // AABB 2
            float x2Min = xTwo;
            float x2Max = xTwo + oTwoWidth;
            float y2Max = yTwo + oTwoHeight;
            float y2Min = yTwo;

            // Collision tests
            if (x1Max < x2Min || x1Min > x2Max) return false;
            if (y1Max < y2Min || y1Min > y2Max) return false;

            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }
    
        float move = 0.0f;
        float comp_move = 0.0f, comp_velocity = 1.0f, screen_height = 250.0f, flag = 0.0f;
        
        public void Form1_KeysDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)//key up
            {
                move-=5.0f;
                if (move <= -250.0f) 
                {
                    move = -250.0f;
                }
            }
            
            if (e.KeyCode == Keys.Down)//key down
            {
                move+=5.0f;
                if(move >= 210.0f)
                {
                    move = 210.0f;
                }
            }
            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comp_velocity = (comp_move < screen_height && flag == 0.0f) ? (comp_velocity = 5.0f) : (comp_velocity = -5.0f);
           
            comp_move = (comp_velocity == 5.0f) ? (comp_move += 5.0f) : (comp_move -= 5.0f);
            
            if(comp_move >= screen_height-40.0f)
            {
                flag = 1.0f;
            }
            
            if(comp_move <= -screen_height)
            {
                flag = 0.0f;
            }
            Invalidate();
        }

        float x = 0.0f, y = 0.0f, xstep = 5.0f, ystep = 5.0f;
        int num_one = 0, num_two = 0;
       
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (x >= 395.0f)  
            {
                num_one++;
                if(num_one==10)
                {
                    num_one = 10;
                }
                x = 0.0f;
            }
            if (x <= -390.0f)
            {
                num_two++;
                if (num_two == 10)
                {
                    num_two = 10;
                }
                x = 0.0f;
            }
            if (y >= 270.0f || y <= -290.0f) 
            {
                ystep = -ystep;
            }
            x += xstep;
            y += ystep;
            Invalidate();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 750.0f;
            float yTwo = 250.0f + move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 0.0f;
            float yTwo = 250.0f + comp_move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            RectangleF rect1 = new RectangleF(0, 250 + comp_move, 35, 100); //blue paddle
            RectangleF rect2 = new RectangleF(750, 250 + move, 35, 100); //red paddle
            RectangleF rect3 = new RectangleF(390+x, 290+y, 10, 10); //white ball
            g.FillRectangle(blueBrush, rect1);
            g.FillRectangle(redBrush, rect2);
            g.FillRectangle(whiteBrush, rect3);

            if (num_one == 0)
            {
                String drawString_one = "0";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 1)
            {
                String drawString_one = "1";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 2)
            {
                String drawString_one = "2";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 3)
            {
                String drawString_one = "3";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 4)
            {
                String drawString_one = "4";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 5)
            {
                String drawString_one = "5";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 6)
            {
                String drawString_one = "6";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 7)
            {
                String drawString_one = "7";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 8)
            {
                String drawString_one = "8";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 9)
            {
                String drawString_one = "9";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_one == 10)
            {
                String drawString_one = "10";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Red);
                PointF drawPoint_one = new PointF(150.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 0)
            {
                String drawString_one = "0";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 1)
            {
                String drawString_one = "1";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 2)
            {
                String drawString_one = "2";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 3)
            {
                String drawString_one = "3";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 4)
            {
                String drawString_one = "4";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 5)
            {
                String drawString_one = "5";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 6)
            {
                String drawString_one = "6";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 7)
            {
                String drawString_one = "7";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 8)
            {
                String drawString_one = "8";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 9)
            {
                String drawString_one = "9";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            if (num_two == 10)
            {
                String drawString_one = "10";
                Font drawFont_one = new Font("Arial", 16);
                SolidBrush drawBrush_one = new SolidBrush(Color.Blue);
                PointF drawPoint_one = new PointF(550.0F, 150.0F);
                g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);
            }
            g.Dispose();
        }
    }
}

 

January 02, 2019 01:21 AM
Brain

Hmm, why not just convert num_two and num_one to a string and put it in drawString_one, rather than the big long chain of if statements?

What happens if the player scores more than ten points?

January 02, 2019 01:24 AM
phil67rpg

I put a limit of score of 10.

January 02, 2019 01:43 AM
phil67rpg

I have put in the ToString() function in my game, so I have got rid of the if statements and the score is unlimited.

here is my updated code.


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;

namespace winpong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
        {
            // AABB 1
            float x1Min = x;
            float x1Max = x + oWidth;
            float y1Max = y + oHeight;
            float y1Min = y;

            // AABB 2
            float x2Min = xTwo;
            float x2Max = xTwo + oTwoWidth;
            float y2Max = yTwo + oTwoHeight;
            float y2Min = yTwo;

            // Collision tests
            if (x1Max < x2Min || x1Min > x2Max) return false;
            if (y1Max < y2Min || y1Min > y2Max) return false;

            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }
    
        float move = 0.0f;
        float comp_move = 0.0f, comp_velocity = 1.0f, screen_height = 250.0f, flag = 0.0f;
        
        public void Form1_KeysDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)//key up
            {
                move-=5.0f;
                if (move <= -250.0f) 
                {
                    move = -250.0f;
                }
            }
            
            if (e.KeyCode == Keys.Down)//key down
            {
                move+=5.0f;
                if(move >= 210.0f)
                {
                    move = 210.0f;
                }
            }
            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comp_velocity = (comp_move < screen_height && flag == 0.0f) ? (comp_velocity = 5.0f) : (comp_velocity = -5.0f);
           
            comp_move = (comp_velocity == 5.0f) ? (comp_move += 5.0f) : (comp_move -= 5.0f);
            
            if(comp_move >= screen_height-40.0f)
            {
                flag = 1.0f;
            }
            
            if(comp_move <= -screen_height)
            {
                flag = 0.0f;
            }
            Invalidate();
        }

        float x = 0.0f, y = 0.0f, xstep = 5.0f, ystep = 5.0f;
        int num_one = 0, num_two = 0;
       
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (x >= 395.0f)  
            {
                num_one++;
                x = 0.0f;
            }
            if (x <= -390.0f)
            {
                num_two++;
                x = 0.0f;
            }
            if (y >= 270.0f || y <= -290.0f) 
            {
                ystep = -ystep;
            }
            x += xstep;
            y += ystep;
            Invalidate();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 750.0f;
            float yTwo = 250.0f + move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 0.0f;
            float yTwo = 250.0f + comp_move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            RectangleF rect1 = new RectangleF(0, 250 + comp_move, 35, 100); //blue paddle
            RectangleF rect2 = new RectangleF(750, 250 + move, 35, 100); //red paddle
            RectangleF rect3 = new RectangleF(390+x, 290+y, 10, 10); //white ball
            g.FillRectangle(blueBrush, rect1);
            g.FillRectangle(redBrush, rect2);
            g.FillRectangle(whiteBrush, rect3);

            String drawString_one = num_one.ToString();
            Font drawFont_one = new Font("Arial", 16);
            SolidBrush drawBrush_one = new SolidBrush(Color.Red);
            PointF drawPoint_one = new PointF(150.0F, 150.0F);
            g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);

            String drawString_two = num_two.ToString();
            Font drawFont_two = new Font("Arial", 16);
            SolidBrush drawBrush_two = new SolidBrush(Color.Blue);
            PointF drawPoint_two = new PointF(550.0F, 150.0F);
            g.DrawString(drawString_two, drawFont_two, drawBrush_two, drawPoint_two);

            g.Dispose();
        }
    }
}

 

January 02, 2019 02:13 AM
Brain

Much better!

I'm liking what i see! What's next on your todo? Perhaps a title screen or a high score table?

January 02, 2019 02:16 AM
phil67rpg

maybe a title screen would be appropriate, could you elaborate on what it should look like? I am not very creative.

January 02, 2019 02:21 AM
Brain

Well, i would keep it simple.

If you look at most title screens they have a 'zen' look of simplicity about them that draws the eye to the 'start' or 'continue' option, generally you can start the game just by pressing the start button or enter key.

For example look at this screen for skyrim:

Apart from the logo and smoke and music, it's as simple as you can get.

You'd only need two, perhaps 3 options:

  • Start game
  • High scores
  • Exit game

Note that there's a hidden reason that they ask you to press start before showing the options proper; it's simply so that the system can detect which controller the player is using. XInput and similar support up to 4 controllers, so by pressing start you explicitly confirm which one is active and in use. I didn't know this until i started looking into making XBox Live indie games on the 360 some years ago, it was part of their approval process to go live with a game. I'm not sure if it's still the case for things like ID@XBox, but still best practive even on PC.

January 02, 2019 02:44 AM
phil67rpg

I am going to implement a very simple high score system.

January 02, 2019 02:57 AM
phil67rpg

I have implemented a very simple high scoring algorithm


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;

namespace winpong
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        bool checkCollide(float x, float y, float oWidth, float oHeight, float xTwo, float yTwo, float oTwoWidth, float oTwoHeight)
        {
            // AABB 1
            float x1Min = x;
            float x1Max = x + oWidth;
            float y1Max = y + oHeight;
            float y1Min = y;

            // AABB 2
            float x2Min = xTwo;
            float x2Max = xTwo + oTwoWidth;
            float y2Max = yTwo + oTwoHeight;
            float y2Min = yTwo;

            // Collision tests
            if (x1Max < x2Min || x1Min > x2Max) return false;
            if (y1Max < y2Min || y1Min > y2Max) return false;

            return true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
  
        }
    
        float move = 0.0f;
        float comp_move = 0.0f, comp_velocity = 1.0f, screen_height = 250.0f, flag = 0.0f;
        
        public void Form1_KeysDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up)//key up
            {
                move-=5.0f;
                if (move <= -250.0f) 
                {
                    move = -250.0f;
                }
            }
            
            if (e.KeyCode == Keys.Down)//key down
            {
                move+=5.0f;
                if(move >= 210.0f)
                {
                    move = 210.0f;
                }
            }
            Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            comp_velocity = (comp_move < screen_height && flag == 0.0f) ? (comp_velocity = 5.0f) : (comp_velocity = -5.0f);
           
            comp_move = (comp_velocity == 5.0f) ? (comp_move += 5.0f) : (comp_move -= 5.0f);
            
            if(comp_move >= screen_height-40.0f)
            {
                flag = 1.0f;
            }
            
            if(comp_move <= -screen_height)
            {
                flag = 0.0f;
            }
            Invalidate();
        }

        float x = 0.0f, y = 0.0f, xstep = 5.0f, ystep = 5.0f;
        int num_one = 0, num_two = 0, hs_one=0, hs_two=0;

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (x >= 395.0f)  
            {
                num_one++;
                if(num_one>=11)
                {
                    hs_one = 11;
                    num_one = 11;
                }
                x = 0.0f;
            }
            if (x <= -390.0f)
            {
                num_two++;
                if(num_two>=11)
                {
                    hs_two = 11;
                    num_two = 11;
                }
                x = 0.0f;
            }
            if (y >= 270.0f || y <= -290.0f) 
            {
                ystep = -ystep;
            }
            x += xstep;
            y += ystep;
            Invalidate();
        }

        private void timer3_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 750.0f;
            float yTwo = 250.0f + move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        private void timer4_Tick(object sender, EventArgs e)
        {
            float x1 = 390.0f + x;
            float y1 = 270.0f + y;
            float oWidth = 10.0f;
            float oHeight = 10.0f;

            float xTwo = 0.0f;
            float yTwo = 250.0f + comp_move;
            float oTwoWidth = 35.0f;
            float oTwoHeight = 100.0f;

            if (checkCollide(x1, y1, oWidth, oHeight, xTwo, yTwo, oTwoWidth, oTwoHeight) == true)
            {
                xstep = -xstep;
            }
            Invalidate();
        }

        public void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush blueBrush = new SolidBrush(Color.Blue);
            SolidBrush redBrush = new SolidBrush(Color.Red);
            SolidBrush whiteBrush = new SolidBrush(Color.White);
            RectangleF rect1 = new RectangleF(0, 250 + comp_move, 35, 100); //blue paddle
            RectangleF rect2 = new RectangleF(750, 250 + move, 35, 100); //red paddle
            RectangleF rect3 = new RectangleF(390+x, 290+y, 10, 10); //white ball
            g.FillRectangle(blueBrush, rect1);
            g.FillRectangle(redBrush, rect2);
            g.FillRectangle(whiteBrush, rect3);

            String drawString_one = num_one.ToString();
            Font drawFont_one = new Font("Arial", 16);
            SolidBrush drawBrush_one = new SolidBrush(Color.Red);
            PointF drawPoint_one = new PointF(150.0F, 150.0F);
            g.DrawString(drawString_one, drawFont_one, drawBrush_one, drawPoint_one);

            String drawString_two = num_two.ToString();
            Font drawFont_two = new Font("Arial", 16);
            SolidBrush drawBrush_two = new SolidBrush(Color.Blue);
            PointF drawPoint_two = new PointF(550.0F, 150.0F);
            g.DrawString(drawString_two, drawFont_two, drawBrush_two, drawPoint_two);

            String drawString_three = "High Score";
            Font drawFont_three = new Font("Arial", 16);
            SolidBrush drawBrush_three = new SolidBrush(Color.White);
            PointF drawPoint_three = new PointF(325.0F, 10.0F);
            g.DrawString(drawString_three, drawFont_three, drawBrush_three, drawPoint_three);
            if (hs_one == 11) 
            {
                String drawString_four = num_one.ToString();
                Font drawFont_four = new Font("Arial", 16);
                SolidBrush drawBrush_four = new SolidBrush(Color.Red);
                PointF drawPoint_four = new PointF(375.0F, 50.0F);
                g.DrawString(drawString_four, drawFont_four, drawBrush_four, drawPoint_four);

                String drawString_five = "Red Wins";
                Font drawFont_five = new Font("Arial", 16);
                SolidBrush drawBrush_five = new SolidBrush(Color.Red);
                PointF drawPoint_five = new PointF(350.0F, 100.0F);
                g.DrawString(drawString_five, drawFont_five, drawBrush_five, drawPoint_five);
            }
            if (hs_two == 11)
            {
                String drawString_four = num_two.ToString();
                Font drawFont_four = new Font("Arial", 16);
                SolidBrush drawBrush_four = new SolidBrush(Color.Blue);
                PointF drawPoint_four = new PointF(375.0F, 50.0F);
                g.DrawString(drawString_four, drawFont_four, drawBrush_four, drawPoint_four);

                String drawString_five = "Blue Wins";
                Font drawFont_five = new Font("Arial", 16);
                SolidBrush drawBrush_five = new SolidBrush(Color.Blue);
                PointF drawPoint_five = new PointF(350.0F, 100.0F);
                g.DrawString(drawString_five, drawFont_five, drawBrush_five, drawPoint_five);
            }
            g.Dispose();
        }
    }
}

 

January 03, 2019 01:17 AM
Brain

Looks good , what do you plan to create next?

Keep it up!

January 03, 2019 10:44 PM
phil67rpg

I am working on tic tac toe using c#

January 04, 2019 12:13 AM
phil67rpg

here is my code for a tic tac toe game, I am working on the computer player


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;

namespace wintictactoe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
        
        int [,] board_x=new int[3,3];
        int[,] board_o = new int[3, 3];

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left)
            {
                Random rnd = new Random();

                int col = rnd.Next(325, 476);
                int row = rnd.Next(200, 351);

                Graphics g1 = this.CreateGraphics();
                if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 215));
                    board_x[0, 0] = 1;
                }
                if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 215));
                    board_x[0, 1] = 1;
                }
                if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 200 && e.Y - 10 <= 250)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 215));
                    board_x[0, 2] = 1;
                }
                if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 260));
                    board_x[1, 0] = 1;
                }
                if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 260));
                    board_x[1, 1] = 1;
                }
                if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 250 && e.Y - 10 <= 300)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 260));
                    board_x[1, 2] = 1;
                }
                if (e.X - 10 >= 325 && e.X - 10 <= 375 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 315));
                    board_x[2, 0] = 1;
                }
                if (e.X - 10 >= 375 && e.X - 10 <= 425 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 315));
                    board_x[2, 1] = 1;
                }
                if (e.X - 10 >= 425 && e.X - 10 <= 475 && e.Y - 10 >= 300 && e.Y - 10 <= 350)
                {
                    g1.DrawString("X", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 315));
                    board_x[2, 2] = 1;
                }

                if (col >= 325 && col <= 375 && row >= 200 && row <= 250)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 215));
                    board_o[0, 0] = 1;
                }
                if (col >= 375 && col <= 425 && row >= 200 && row <= 250)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 215));
                    board_o[0, 1] = 1;
                }
                if (col >= 425 && col <= 475 && row >= 200 && row <= 250)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 215));
                    board_o[0, 2] = 1;
                }

                if (col >= 325 && col <= 375 && row >= 250 && row <= 300)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 260));
                    board_o[1, 0] = 1;
                }
                if (col >= 375 && col <= 425 && row >= 250 && row <= 300)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 260));
                    board_o[1, 1] = 1;
                }
                if (col >= 425 && col <= 475 && row >= 250 && row <= 300)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 260));
                    board_o[1, 2] = 1;
                }

                if (col >= 325 && col <= 375 && row >= 300 && row <= 350)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(340, 315));
                    board_o[2, 0] = 1;
                }
                if (col >= 375 && col <= 425 && row >= 300 && row <= 350)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(390, 315));
                    board_o[2, 1] = 1;
                }
                if (col >= 425 && col <= 475 && row >= 300 && row <= 350)
                {
                    g1.DrawString("O", new Font("Arial", 16), new SolidBrush(Color.Blue), new Point(440, 315));
                    board_o[2, 2] = 1;
                }
                g1.Dispose();
            }
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen redPen = new Pen(Color.Red,3);
            g.DrawLine(redPen, 375, 200, 375, 350);
            g.DrawLine(redPen, 425, 200, 425, 350);
            g.DrawLine(redPen, 325, 250, 475, 250);
            g.DrawLine(redPen, 325, 300, 475, 300);
            g.Dispose();
        }
    }
}

 

January 05, 2019 12:50 AM
You must log in to join the conversation.
Don't have a GameDev.net account? Sign up!
Profile
Author
Advertisement

Latest Entries

asteroids wars

2652 views

plane game

3401 views

rgg platformer

2033 views

win 32 pong

2514 views

bug invaders

2178 views

c# book

2465 views

c# bug invaders

2122 views

c# console snake game

23768 views
Advertisement