Portfolio

Monday, September 14, 2015

How to Create an Etch a Sketch Game in C#

Who remembers playing Etch a Sketch as a kid?  Was it fun or what?  Well, I have created a cool game for all you kids and adults that are kid like.

I really had a lot of fun coding this game.  When I first learned how to manipulate the controls on Visual Studio in C#, it was all uphill from there.

I have included buttons, graphics and a simple program to do this game.  And for all the coders out there, this ones for you.




Here's the 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 Etch_a_sketch
{
    public partial class Form1 : Form
    {
        float posX, posY;
        Graphics g;


        public Form1()
        {
            InitializeComponent();

            posX = DrawingPanel.Width / 2;
            posY = DrawingPanel.Height / 2;

            g = DrawingPanel.CreateGraphics();

        }

        private void btnRight_Click(object sender, EventArgs e)
        {
            g.DrawLine(new Pen(Color.Red), posX, posY, posX + 5, posY);
            posX= posX + 5;
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            g.Clear(Color.White);
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            g.DrawLine(new Pen(Color.Red), posX, posY, posX, posY + 5);
            posY = posY + 5;
        }

        private void btnLeft_Click(object sender, EventArgs e)
        {
            g.DrawLine(new Pen(Color.Red), posX, posY, posX - 5, posY);
            posX = posX - 5;
           
        }

        private void btnUp_Click(object sender, EventArgs e)
        {
            g.DrawLine(new Pen(Color.Red), posX, posY, posX, posY - 5);
            posY= posY - 5; ;
        }



    }
}


No comments:

Post a Comment