• (2 Pages)
  • +
  • 1
  • 2

C#: Basic Box Game Scrolling, Collision and Counting

#16 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8376
  • View blog
  • Posts: 31,134
  • Joined: 12-June 08

Posted 17 January 2013 - 08:21 AM

What you don't know how to add event handlers or use the designer's event form to set the key down?
Was This Post Helpful? 0
  • +
  • -

#17 Vinians  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 17-January 13

Posted 17 January 2013 - 08:28 AM

View Postmodi123_1, on 17 January 2013 - 08:21 AM, said:

What you don't know how to add event handlers or use the designer's event form to set the key down?

Friend, I'm not a begginer programmer. I'm a begginer in C# but, yes, my event handle is set correctly. What I said is that "cursor keys" are *not* working but other keys *are* working. I dont know why... What can be wrong? (my keyboard is ok btw).
Was This Post Helpful? 0
  • +
  • -

#18 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 249
  • View blog
  • Posts: 337
  • Joined: 06-March 10

Posted 08 February 2013 - 09:15 PM

View Postcocoto4, on 20 November 2012 - 10:12 PM, said:

hey! thanks alot for this tutorial! learned alot :D/>/> although i can't seem to figure out how to make you "respawn" when you hit the corners, neither how you find out if the player hit the corners, can you please help me on this?
This is my current code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
            
            private Rectangle Goal = new Rectangle(350, 600, 50, 50);
            private Rectangle Player = new Rectangle(350, 1, 50, 50);
            private Rectangle Enemy1 = new Rectangle(0, 150, 75, 75);
            private Rectangle Enemy2 = new Rectangle(599, 350, 75, 75);

        public Form1()
        {

        InitializeComponent();
        this.MaximumSize = new Size(750, 750);
        this.MinimumSize = new Size(750, 750);
        InitializeComponent();
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Green, Goal);
            e.Graphics.DrawRectangle(Pens.Blue, Player);
            e.Graphics.DrawRectangle(Pens.Red, Enemy1);
            e.Graphics.DrawRectangle(Pens.Red, Enemy2);
        }


        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;
         switch (e.KeyData)
        {
        case Keys.Up:
            Player.Location = new Point(PlayerX += 0, PlayerY -= 20);
            this.Refresh();
            break;
        case Keys.Down:
            Player.Location = new Point(PlayerX += 0, PlayerY += 20);
            this.Refresh();
            break;
        case Keys.Left:
            Player.Location = new Point(PlayerX -= 20, PlayerY += 0);
            this.Refresh();
            break;
        case Keys.Right:
            Player.Location = new Point(PlayerX += 20, PlayerY += 0);
            this.Refresh();
            break;
        }

  }
        
        private void timer1_Tick(object sender, EventArgs e)
        {
            int EX1 = Enemy1.Location.X;
            int EY1 = Enemy1.Location.Y;

            if (Enemy1.Location.X > 600)
            {
                Enemy1.Location = new Point(EX1 = 0, EY1 = 150);
            }

            Enemy1.Location = new Point(EX1 += 30, EY1 += 0);
            this.Refresh();

            int EX2 = Enemy2.Location.X;
            int EY2 = Enemy2.Location.Y;

            if (Enemy2.Location.X < 0)
            {
                Enemy2.Location = new Point(EX2 = 599, EY2 = 350);

            }

            Enemy2.Location = new Point(EX2 -= 30, EY2 += 0);
            this.Refresh();


        }


       
        private void HitDetect(object sender, EventArgs e)
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;

            if (Player.IntersectsWith(Goal))
            {
                Player.Location = new Point(PlayerX = 350, PlayerY = 0);
                MessageBox.Show("You won!", "Nice!");
            }
        
            if (Enemy1.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 350, PlayerY = 0);
                MessageBox.Show("You lose!");
            }

            if (Enemy2.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 350, PlayerY = 0);
                MessageBox.Show("You lose!");
                if (Player.Location.X > 650)
                {
                    Player.Location = new Point(PlayerX -= 20, PlayerY += 0);

                }

                if (Player.Location.X < 0)
                {
                    Player.Location = new Point(PlayerX += 20, PlayerY += 0);

                }

                if (Player.Location.Y > 590)
                {
                    Player.Location = new Point(PlayerX += 0, PlayerY -= 20);

                }

                if (Player.Location.Y < 1)
                {
                    Player.Location = new Point(PlayerX += 0, PlayerY += 20);

                }
            }
        }
       

    }
}




PS: is there any other way i can get the MessageBox to stop spamming when hit goal? except putting out that you go back to start afterwards?


I gave your code a go, and it seemed to work (almost) out of the box for me.

I'm sorry for the broken code and the issue you're having, My only suggestion is perhaps modifying the switch statement from e.KeyPress to e.KeyCode

Both worked for me.

Also, Be aware this tutorial is very old so you may want to set yourself a challenge to write it in a much better, simpler way.

This post has been edited by Fungle: 16 February 2013 - 07:33 AM

Was This Post Helpful? 0
  • +
  • -

#19 qobacha  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 27-April 12

Posted 23 April 2013 - 07:52 AM

Thanks for the informative tutorial especially for someone new in C#. I have one more question. Suppose we want to increase the number of objects moving sequentially vertically(Enemies in this tut) across the form to make the game more challenging. Is there anyone with an idea of how this could be done? I have thought of using List collection but couldn't figure out how to store Rectangle objects and to paint them sequentially.
Was This Post Helpful? 0
  • +
  • -

#20 Beeka  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 03-June 13

Posted 03 June 2013 - 09:18 AM

Hi! First off, thanks to the creator of this tutorial. I am a newbie to C# and I love this example. So I have a question for any one out there willing to drop some knowledge on me:

I was able to add a difficulty mode to the game where depending on one of three levels, the number of opponents would change. For example, Level 1 (easy) has just the 2 rectangles. Level 2 (moderate) has 3, and Level 3 (hard) has 4. I figured out a way to paint the game depending on the level chosen, but the timer is not working. I used switch cases to determine which scenario to paint, and instead of declaring the sizes at the top, this is also done via a switch case. Hopefully the code below would make more sense to you. My question is why is the timer not working? I noticed that as long as I declare the sizes as done in the original code, the timer works fine.

Hopefully my work makes sense and look forward to any help given!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Box_Game
{
    public partial class Form1 : Form
    {
        Rectangle Player = new Rectangle(290, 30, 50, 50);
        Rectangle Block1;
        Rectangle Block2;
        Rectangle Block3;
        Rectangle Block4;
      
        int Level;

        public Form1()
        {
            InitializeComponent();

            // Game is set to easy as a default
            Level = 1;

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

        }

        private void DrawRectangle(PaintEventArgs e, Rectangle Block)
        {
            e.Graphics.DrawRectangle(Pens.Black, Block);         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            switch (Level)
            {
                case 1:
                    Block1 = new Rectangle(5, 245, 75, 75);
                    Block2 = new Rectangle(553, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);

                    break;
                case 2:
                    Block1 = new Rectangle(0, 150, 75, 75);
                    Block2 = new Rectangle(560, 300, 75, 75);
                    Block3 = new Rectangle(0, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);
                    DrawRectangle(e, Block3);

                    break;
                case 3:
                    Block1 = new Rectangle(0, 150, 75, 75);
                    Block2 = new Rectangle(560, 250, 75, 75);
                    Block3 = new Rectangle(0, 350, 75, 75);
                    Block4 = new Rectangle(560, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);
                    DrawRectangle(e, Block3);
                    DrawRectangle(e, Block4);

                    break;
            }

            Color CustomRed = Color.FromArgb(150, Color.Red);
            Color CustomBlue = Color.FromArgb(165, Color.RoyalBlue);
            Color CustomGreen = Color.FromArgb(170, Color.SeaGreen);
            Color CustomPurple = Color.FromArgb(170, Color.Plum);
            Color CustomOrange = Color.FromArgb(170, Color.Orange);
            Color CustomBrown = Color.FromArgb(170, Color.SandyBrown);

            SolidBrush FillGoal = new SolidBrush(CustomRed);
            SolidBrush FillPlayer = new SolidBrush(CustomBlue);
            SolidBrush FillBlock1 = new SolidBrush(CustomGreen);
            SolidBrush FillBlock2 = new SolidBrush(CustomPurple);
            SolidBrush FillBlock3 = new SolidBrush(CustomOrange);
            SolidBrush FillBlock4 = new SolidBrush(CustomBrown);

            e.Graphics.DrawRectangle(Pens.Black, Player);
            e.Graphics.FillRectangle(FillPlayer, Player);

            e.Graphics.FillRectangle(FillBlock1, Block1);
            e.Graphics.FillRectangle(FillBlock2, Block2);
            e.Graphics.FillRectangle(FillBlock3, Block3);
            e.Graphics.FillRectangle(FillBlock4, Block4);

            // Render the Goal
            Point point1 = new Point(270, 650);
            Point point2 = new Point(270, 700);
            Point point3 = new Point(380, 700);
            Point point4 = new Point(380, 650);
            Point point5 = new Point(360, 650);
            Point point6 = new Point(360, 686);
            Point point7 = new Point(290, 686);
            Point point8 = new Point(290, 650);

            Point[] Goal = 
            {
                 point1,
                 point2,
                 point3,
                 point4,
                 point5,
                 point6,
                 point7,
                 point8
             };

            e.Graphics.DrawPolygon(Pens.Black, Goal);
            e.Graphics.FillPolygon(FillGoal, Goal);
      }

        public void CollisonDetect()
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;

            DialogResult Result;

            if( (Player.Location.X >= 290 && Player.Location.X <= 310) && Player.Location.Y > 635 ) 
            {
                Result = MessageBox.Show("You Won!! Play Again?", "Game Won", MessageBoxButtons.YesNo);

                if (Result == DialogResult.Yes)
                {
                    Player.Location = new Point(314, 0);
                }
                else
                {
                    this.Close();
                }
            }

            if (Block1.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 1!!");
            }

            if (Block2.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 2!!");
            }

            if (Block3.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 3!!");
            }

            if (Block4.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 350, PlayerY = 0);
                MessageBox.Show("You've Been Hit By Block 4!!");
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;

            switch (e.KeyData)
            {
                case Keys.Up:
                    if (Player.Location.Y < 0)
                    {
                        Player.Location = new Point(290, 30);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 0, PlayerY -= 20);
                    }

                    this.Refresh();
                    break;

                case Keys.Down:
                    if (Player.Location.Y >= 650)
                    {
                        Player.Location = new Point(290, 30);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 0, PlayerY += 20);
                    }

                    CollisonDetect();

                    this.Refresh();
                    break;

                case Keys.Left:
                    if (Player.Location.X < 0)
                    {
                        Player.Location = new Point(580, Player.Location.Y);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX -= 20, PlayerY += 0);
                    }

                    this.Refresh();
                    break;

                case Keys.Right:
                    if (Player.Location.X > 580)
                    {
                        Player.Location = new Point(0, Player.Location.Y);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 20, PlayerY += 0);
                    }

                    this.Refresh();
                    break;
            }
        }

        private void changeLevelToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 1;
        }

        private void moderateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 2;     
        }

        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 3;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int Block1_X = Block1.Location.X;
            int Block1_Y = Block1.Location.Y;

            int Block2_X = Block2.Location.X;
            int Block2_Y = Block2.Location.Y;

            int Block3_X = Block3.Location.X;
            int Block3_Y = Block3.Location.Y;

            int Block4_X = Block4.Location.X;
            int Block4_Y = Block4.Location.Y;

            if (Block1.Location.X > 580)
            {
                Block1.Location = new Point(Block1_X = 0, Block1_Y = 150);
            }

            Block1.Location = new Point(Block1_X += 12, Block1_Y += 0);

            if (Block2.Location.X < 0)
            {
                Block2.Location = new Point(Block2_X = 580, Block2_Y = 350);
            }

            Block2.Location = new Point(Block2_X -= 18, Block2_Y += 0);

             this.Refresh(); 
        }
    }

        
}



Was This Post Helpful? 0
  • +
  • -

#21 emile2012  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 30-December 12

Posted 16 July 2013 - 07:40 AM

Nice tutorial, great project for beginners.

A minor addition though: it would probably be good to also call the HitDetect() method in the timer1 tick event, so it also checks for intersections when the 'Enemies' move - otherwise, it won't be detected if the enemies move into the player's rectangle.
Was This Post Helpful? 0
  • +
  • -

#22 alexeyich  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 13

Posted 23 September 2013 - 05:08 AM

nice work :) <3
Was This Post Helpful? 0
  • +
  • -

#23 Monkey.d garp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 13

Posted 15 December 2013 - 03:17 AM

View PostBeeka, on 03 June 2013 - 09:18 AM, said:

Hi! First off, thanks to the creator of this tutorial. I am a newbie to C# and I love this example. So I have a question for any one out there willing to drop some knowledge on me:

I was able to add a difficulty mode to the game where depending on one of three levels, the number of opponents would change. For example, Level 1 (easy) has just the 2 rectangles. Level 2 (moderate) has 3, and Level 3 (hard) has 4. I figured out a way to paint the game depending on the level chosen, but the timer is not working. I used switch cases to determine which scenario to paint, and instead of declaring the sizes at the top, this is also done via a switch case. Hopefully the code below would make more sense to you. My question is why is the timer not working? I noticed that as long as I declare the sizes as done in the original code, the timer works fine.

Hopefully my work makes sense and look forward to any help given!!

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Box_Game
{
    public partial class Form1 : Form
    {
        Rectangle Player = new Rectangle(290, 30, 50, 50);
        Rectangle Block1;
        Rectangle Block2;
        Rectangle Block3;
        Rectangle Block4;
      
        int Level;

        public Form1()
        {
            InitializeComponent();

            // Game is set to easy as a default
            Level = 1;

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

        }

        private void DrawRectangle(PaintEventArgs e, Rectangle Block)
        {
            e.Graphics.DrawRectangle(Pens.Black, Block);         
        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            switch (Level)
            {
                case 1:
                    Block1 = new Rectangle(5, 245, 75, 75);
                    Block2 = new Rectangle(553, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);

                    break;
                case 2:
                    Block1 = new Rectangle(0, 150, 75, 75);
                    Block2 = new Rectangle(560, 300, 75, 75);
                    Block3 = new Rectangle(0, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);
                    DrawRectangle(e, Block3);

                    break;
                case 3:
                    Block1 = new Rectangle(0, 150, 75, 75);
                    Block2 = new Rectangle(560, 250, 75, 75);
                    Block3 = new Rectangle(0, 350, 75, 75);
                    Block4 = new Rectangle(560, 450, 75, 75);

                    DrawRectangle(e, Block1);
                    DrawRectangle(e, Block2);
                    DrawRectangle(e, Block3);
                    DrawRectangle(e, Block4);

                    break;
            }

            Color CustomRed = Color.FromArgb(150, Color.Red);
            Color CustomBlue = Color.FromArgb(165, Color.RoyalBlue);
            Color CustomGreen = Color.FromArgb(170, Color.SeaGreen);
            Color CustomPurple = Color.FromArgb(170, Color.Plum);
            Color CustomOrange = Color.FromArgb(170, Color.Orange);
            Color CustomBrown = Color.FromArgb(170, Color.SandyBrown);

            SolidBrush FillGoal = new SolidBrush(CustomRed);
            SolidBrush FillPlayer = new SolidBrush(CustomBlue);
            SolidBrush FillBlock1 = new SolidBrush(CustomGreen);
            SolidBrush FillBlock2 = new SolidBrush(CustomPurple);
            SolidBrush FillBlock3 = new SolidBrush(CustomOrange);
            SolidBrush FillBlock4 = new SolidBrush(CustomBrown);

            e.Graphics.DrawRectangle(Pens.Black, Player);
            e.Graphics.FillRectangle(FillPlayer, Player);

            e.Graphics.FillRectangle(FillBlock1, Block1);
            e.Graphics.FillRectangle(FillBlock2, Block2);
            e.Graphics.FillRectangle(FillBlock3, Block3);
            e.Graphics.FillRectangle(FillBlock4, Block4);

            // Render the Goal
            Point point1 = new Point(270, 650);
            Point point2 = new Point(270, 700);
            Point point3 = new Point(380, 700);
            Point point4 = new Point(380, 650);
            Point point5 = new Point(360, 650);
            Point point6 = new Point(360, 686);
            Point point7 = new Point(290, 686);
            Point point8 = new Point(290, 650);

            Point[] Goal = 
            {
                 point1,
                 point2,
                 point3,
                 point4,
                 point5,
                 point6,
                 point7,
                 point8
             };

            e.Graphics.DrawPolygon(Pens.Black, Goal);
            e.Graphics.FillPolygon(FillGoal, Goal);
      }

        public void CollisonDetect()
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;

            DialogResult Result;

            if( (Player.Location.X >= 290 && Player.Location.X <= 310) && Player.Location.Y > 635 ) 
            {
                Result = MessageBox.Show("You Won!! Play Again?", "Game Won", MessageBoxButtons.YesNo);

                if (Result == DialogResult.Yes)
                {
                    Player.Location = new Point(314, 0);
                }
                else
                {
                    this.Close();
                }
            }

            if (Block1.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 1!!");
            }

            if (Block2.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 2!!");
            }

            if (Block3.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 290, PlayerY = 30);
                MessageBox.Show("You've Been Hit By Block 3!!");
            }

            if (Block4.IntersectsWith(Player))
            {
                Player.Location = new Point(PlayerX = 350, PlayerY = 0);
                MessageBox.Show("You've Been Hit By Block 4!!");
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            int PlayerX = Player.Location.X;
            int PlayerY = Player.Location.Y;

            switch (e.KeyData)
            {
                case Keys.Up:
                    if (Player.Location.Y < 0)
                    {
                        Player.Location = new Point(290, 30);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 0, PlayerY -= 20);
                    }

                    this.Refresh();
                    break;

                case Keys.Down:
                    if (Player.Location.Y >= 650)
                    {
                        Player.Location = new Point(290, 30);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 0, PlayerY += 20);
                    }

                    CollisonDetect();

                    this.Refresh();
                    break;

                case Keys.Left:
                    if (Player.Location.X < 0)
                    {
                        Player.Location = new Point(580, Player.Location.Y);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX -= 20, PlayerY += 0);
                    }

                    this.Refresh();
                    break;

                case Keys.Right:
                    if (Player.Location.X > 580)
                    {
                        Player.Location = new Point(0, Player.Location.Y);
                    }
                    else
                    {
                        Player.Location = new Point(PlayerX += 20, PlayerY += 0);
                    }

                    this.Refresh();
                    break;
            }
        }

        private void changeLevelToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void easyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 1;
        }

        private void moderateToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 2;     
        }

        private void hardToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Level = 3;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int Block1_X = Block1.Location.X;
            int Block1_Y = Block1.Location.Y;

            int Block2_X = Block2.Location.X;
            int Block2_Y = Block2.Location.Y;

            int Block3_X = Block3.Location.X;
            int Block3_Y = Block3.Location.Y;

            int Block4_X = Block4.Location.X;
            int Block4_Y = Block4.Location.Y;

            if (Block1.Location.X > 580)
            {
                Block1.Location = new Point(Block1_X = 0, Block1_Y = 150);
            }

            Block1.Location = new Point(Block1_X += 12, Block1_Y += 0);

            if (Block2.Location.X < 0)
            {
                Block2.Location = new Point(Block2_X = 580, Block2_Y = 350);
            }

            Block2.Location = new Point(Block2_X -= 18, Block2_Y += 0);

             this.Refresh(); 
        }
    }

        
}



Was This Post Helpful? 0
  • +
  • -

#24 Monkey.d garp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-December 13

Posted 15 December 2013 - 03:23 AM

[quote name='Monkey.d garp' date='15 December 2013 - 03:17 AM' timestamp='1387102664' post='1947396']
[quote name='Beeka' date='03 June 2013 - 09:18 AM' timestamp='1370276325' post='1858640']
Hi! First off, thanks to the creator of this tutorial. I am a newbie to C# and I love this example. So I have a question for any one out there willing to drop some knowledge on me:

I was able to add a difficulty mode to the game where depending on one of three levels, the number of opponents would change. For example, Level 1 (easy) has just the 2 rectangles. Level 2 (moderate) has 3, and Level 3 (hard) has 4. I figured out a way to paint the game depending on the level chosen, but the timer is not working. I used switch cases to determine which scenario to paint, and instead of declaring the sizes at the top, this is also done via a switch case. Hopefully the code below would make more sense to you. My question is why is the timer not working? I noticed that as long as I declare the sizes as done in the original code, the timer works fine.

Hopefully my work makes sense and look forward to any help given!!

[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Box_Game
{
public partial class Form1 : Form
{
Rectangle Player = new Rectangle(290, 30, 50, 50);
Rectangle Block1;
Rectangle Block2;
Rectangle Block3;
Rectangle Block4;

int Level;

public Form1()
{
InitializeComponent();

// Game is set to easy as a default
Level = 1;

}

private void Form1_Load(object sender, EventArgs e)
{

}

private void DrawRectangle(PaintEventArgs e, Rectangle Block)
{
e.Graphics.DrawRectangle(Pens.Black, Block);
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
switch (Level)
{
case 1:
Block1 = new Rectangle(5, 245, 75, 75);
Block2 = new Rectangle(553, 450, 75, 75);

DrawRectangle(e, Block1);
DrawRectangle(e, Block2);

break;
case 2:
Block1 = new Rectangle(0, 150, 75, 75);
Block2 = new Rectangle(560, 300, 75, 75);
Block3 = new Rectangle(0, 450, 75, 75);

DrawRectangle(e, Block1);
DrawRectangle(e, Block2);
DrawRectangle(e, Block3);

break;
case 3:
Block1 = new Rectangle(0, 150, 75, 75);
Block2 = new Rectangle(560, 250, 75, 75);
Block3 = new Rectangle(0, 350, 75, 75);
Block4 = new Rectangle(560, 450, 75, 75);

DrawRectangle(e, Block1);
DrawRectangle(e, Block2);
DrawRectangle(e, Block3);
DrawRectangle(e, Block4);

break;
}

Color CustomRed = Color.FromArgb(150, Color.Red);
Color CustomBlue = Color.FromArgb(165, Color.RoyalBlue);
Color CustomGreen = Color.FromArgb(170, Color.SeaGreen);
Color CustomPurple = Color.FromArgb(170, Color.Plum);
Color CustomOrange = Color.FromArgb(170, Color.Orange);
Color CustomBrown = Color.FromArgb(170, Color.SandyBrown);

SolidBrush FillGoal = new SolidBrush(CustomRed);
SolidBrush FillPlayer = new SolidBrush(CustomBlue);
SolidBrush FillBlock1 = new SolidBrush(CustomGreen);
SolidBrush FillBlock2 = new SolidBrush(CustomPurple);
SolidBrush FillBlock3 = new SolidBrush(CustomOrange);
SolidBrush FillBlock4 = new SolidBrush(CustomBrown);

e.Graphics.DrawRectangle(Pens.Black, Player);
e.Graphics.FillRectangle(FillPlayer, Player);

e.Graphics.FillRectangle(FillBlock1, Block1);
e.Graphics.FillRectangle(FillBlock2, Block2);
e.Graphics.FillRectangle(FillBlock3, Block3);
e.Graphics.FillRectangle(FillBlock4, Block4);

// Render the Goal
Point point1 = new Point(270, 650);
Point point2 = new Point(270, 700);
Point point3 = new Point(380, 700);
Point point4 = new Point(380, 650);
Point point5 = new Point(360, 650);
Point point6 = new Point(360, 686);
Point point7 = new Point(290, 686);
Point point8 = new Point(290, 650);

Point[] Goal =
{
point1,
point2,
point3,
point4,
point5,
point6,
point7,
point8
};

e.Graphics.DrawPolygon(Pens.Black, Goal);
e.Graphics.FillPolygon(FillGoal, Goal);
}

public void CollisonDetect()
{
int PlayerX = Player.Location.X;
int PlayerY = Player.Location.Y;

DialogResult Result;

if( (Player.Location.X >= 290 && Player.Location.X <= 310) && Player.Location.Y > 635 )
{
Result = MessageBox.Show("You Won!! Play Again?", "Game Won", MessageBoxButtons.YesNo);

if (Result == DialogResult.Yes)
{
Player.Location = new Point(314, 0);
}
else
{
this.Close();
}
}

if (Block1.IntersectsWith(Player))
{
Player.Location = new Point(PlayerX = 290, PlayerY = 30);
MessageBox.Show("You've Been Hit By Block 1!!");
}

if (Block2.IntersectsWith(Player))
{
Player.Location = new Point(PlayerX = 290, PlayerY = 30);
MessageBox.Show("You've Been Hit By Block 2!!");
}

if (Block3.IntersectsWith(Player))
{
Player.Location = new Point(PlayerX = 290, PlayerY = 30);
MessageBox.Show("You've Been Hit By Block 3!!");
}

if (Block4.IntersectsWith(Player))
{
Player.Location = new Point(PlayerX = 350, PlayerY = 0);
MessageBox.Show("You've Been Hit By Block 4!!");
}
}

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
int PlayerX = Player.Location.X;
int PlayerY = Player.Location.Y;

switch (e.KeyData)
{
case Keys.Up:
if (Player.Location.Y < 0)
{
Player.Location = new Point(290, 30);
}
else
{
Player.Location = new Point(PlayerX += 0, PlayerY -= 20);
}

this.Refresh();
break;

case Keys.Down:
if (Player.Location.Y >= 650)
{
Player.Location = new Point(290, 30);
}
else
{
Player.Location = new Point(PlayerX += 0, PlayerY += 20);
}

CollisonDetect();

this.Refresh();
break;

case Keys.Left:
if (Player.Location.X < 0)
{
Player.Location = new Point(580, Player.Location.Y);
}
else
{
Player.Location = new Point(PlayerX -= 20, PlayerY += 0);
}

this.Refresh();
break;

case Keys.Right:
if (Player.Location.X > 580)
{
Player.Location = new Point(0, Player.Location.Y);
}
else
{
Player.Location = new Point(PlayerX += 20, PlayerY += 0);
}

this.Refresh();
break;
}
}

private void changeLevelToolStripMenuItem_Click(object sender, EventArgs e)
{

}

private void easyToolStripMenuItem_Click(object sender, EventArgs e)
{
Level = 1;
}

private void moderateToolStripMenuItem_Click(object sender, EventArgs e)
{
Level = 2;
}

private void hardToolStripMenuItem_Click(object sender, EventArgs e)
{
Level = 3;
}

private void timer1_Tick(object sender, EventArgs e)
{
int Block1_X = Block1.Location.X;
int Block1_Y = Block1.Location.Y;

int Block2_X = Block2.Location.X;
int Block2_Y = Block2.Location.Y;

int Block3_X = Block3.Location.X;
int Block3_Y = Block3.Location.Y;

int Block4_X = Block4.Location.X;
int Block4_Y = Block4.Location.Y;

if (Block1.Location.X > 580)
{
Block1.Location = new Point(Block1_X = 0, Block1_Y = 150);
}

Block1.Location = new Point(Block1_X += 12, Block1_Y += 0);

if (Block2.Location.X < 0)
{
Block2.Location = new Point(Block2_X = 580, Block2_Y = 350);
}

Block2.Location = new Point(Block2_X -= 18, Block2_Y += 0);

this.Refresh();
}
}


}

sorry about the previous reply but I am kind new in this site.
anyway I gave your code a go but the enemy is not moving and the ToolStripMenuItem event handlers isn't working can you help?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2