• (2 Pages)
  • +
  • 1
  • 2

C#: Basic Box Game Scrolling, Collision and Counting

#1 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 11 November 2010 - 03:51 PM

*
POPULAR

Welcome to my second tutorial, This one is about a basic C# Windows Form game, Though this is not conventionally used, It is a nice way for beginners to make a rewarding program while learning basic C# Programming.

The requirements for this tutorial are:

- Visual Studio 2008/2010 [Full or C# Express]

This tutorial takes into account that you know how to do the following already:
- How to declare variables
- How to use If Statements

We are going to use the following libarys:
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;



Now we have the requirements sorted, Lets get started.

First off we're going to need three rectangles so, lets make our rectangle's variables to do this we will type the following code:

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

    public Form1()
    {
        InitializeComponent();
    }
}



Now then, lets break that down. We are making our variables for the rectangles, The format for the four numbers you see after the new Rectangle( ); peice it works like this: new Rectangle(X,Y,Width,Height) it is good to know this, if you want to change the starting locations of any of them.

Now we've declared our rectangles, We need to paint them on our form. Create a paint event for the form so that we can paint them.

There are many ways to paint rectangles but for this tutorial we will use a single line for each rectangle.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Red, Goal);
    e.Graphics.DrawRectangle(Pens.Blue, Player);
    e.Graphics.DrawRectangle(Pens.Red, Enemy1);
    e.Graphics.DrawRectangle(Pens.Red, Enemy2);
}



What this part does is it draws the rectangles on the form, We only need two parts to use the method and they are the Pen colour which will be the border colour of the rectangles and the name of the rectangle we want to draw.

Later on we'll need to limit the form size, so lets set it to 750x750 and limit it.
public Form1()
{
    InitializeComponent();
    this.MaximumSize = new Size(750, 750);
    this.MinimumSize = new Size(750, 750);
}



Ok, So we have our rectangles but they don't really do much at the moment, So lets make a way to control our rectangle which is called Player.

To do this we will need to make a KeyDown event on the form, Do this and go to the code.

In order for us to move the rectangle we're going to need to be able to change the location so we need two more variables, one for the X position and one for the Y position.

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



Ok, Now we're going to make a switch, So we can determine what the program should do when a certain key is pressed.

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;
    }
}



Ok, So for the switch we want it to pick up the event key data so we put e.KeyData, the e stands for event and the key data is what key is pushed.

You can see that for each case they are very similar, The Keys.Up part means if the up key is pressed the switch will perform that peice of code.

The next important part is the peices inside the case statements. What we're doing is moving the Players location by 20 pixels each time, so if we want to move it up we want to take away 20 pixels from it's current y location so it goes up! simple ^^.

We need to refresh so that the rectangle will move on the GUI also.

Right, We have a user controlled rectangle now, lets make a custom method to detect if any rectangles hit eachother.

public void HitDetect()
{
    if (Player.IntersectsWith(Goal))
    {
        MessageBox.Show("You Win!");
    }

    int PlayerX = Player.Location.X;
    int PlayerY = Player.Location.Y;
    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!");

    }
}



Ok what we've done is made an if statement to detect if the player's rectangle intersects with the any other rectangle, when it does it show a message box. But, if the player hits an enemy it will move it back to the beginning.

Now we've made our method, let's call it after the switch statement, To do this we simply type HitDetect();

I mean, This is all well and good but what's the point in moving without anything else moving, it's too easy! so lets make our enemies move around a bit.

In order to do this add a timer to your form, set it to enabled and make the event for the tick.

Because above we have set the form to 750 by 750 so we know the boundries of the form.

As when we were moving the player we need variables for the two enemies X and Y location.
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();
}



Ok, The important part of this is the if statement and the line underneath it. The if statement detects if the X location is greater than 600 and if it is, it will reset the enemy back to the other side of the form. The code outside the if statement will keep it moving at 30 pixels per tick of our timer.

Just to make the game a little bit harder, we'll make the other enemy go the other way so we need to do the following code;
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();



Notice because we want it to move the other way, we -30 pixels on each tick.

Ok well, We have those moving but the player can cheat! it can go outside the form, Lets fix that by limiting it in the same way.

Below where we called our HitDetect method we'll put
  if (Player.Location.X > 650)
            {
                Player.Location = new Point(X -= 20, Y += 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);

            }



We have four so that we can't go outside any part of the form, to prevent any cheating. But this time, when we hit the border it will move the player back by the value it is trying to move forward so we can't get past the border!

and that's it, You have a working game ^^

Here is the full program listing, Remeber I put my timer in using the designer mode, not in code so make sure you either make a coded one or implement one from the designer.

public partial class Form1 : Form
{
    private Rectangle Goal = new Rectangle(350, 600, 50, 50);
    private Rectangle Player = new Rectangle(350, 0, 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);
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawRectangle(Pens.Red, 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;
        }

        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);
        }

        HitDetect();
    }

    public void HitDetect()
    {
        if (Player.IntersectsWith(Goal))
        {
            MessageBox.Show("You Win!");
        }

        int PlayerX = Player.Location.X;
        int PlayerY = Player.Location.Y;
        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!");

        }
    }

    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();
    }
}



Tasks for you to do:

- Add a Label that if you get hit by the enemies you lose a point, but if you hit the goal you gain a point

- Try to make it so the form does not flicker as much

Author notes:

Why did you make this: I made this so that beginners to programming can make a rewarding game whilst learning more about programming and how it works.

You did it in a bad way: I am aware this is not the best of ways to do this program, But please remeber this is for beginners, Your welcome to post your opinions but be gentle!

Please enjoy this tutorial, I hope you learnt something new, with several functions mentioned you could make a much better windows form game, The best way to learn is to play around with things good luck and Happy Programming!

Is This A Good Question/Topic? 5
  • +

Replies To: C#: Basic Box Game

#2 keisal  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 2
  • Joined: 05-December 10

Posted 05 December 2010 - 10:43 PM

In your edge detection code, the first if statement should be PlayerX and PlayerY not X and Y. Also, when I compiled the code I just get a blank window. Haven't figured out why yet
Was This Post Helpful? 0
  • +
  • -

#3 R4P  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 10

Posted 13 December 2010 - 12:45 PM

nice tutorial
although this simple game , but your effort clearly appeared in the tutorial .
personally, this tuttorial very useful to me.

Was This Post Helpful? 0
  • +
  • -

#4 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Posted 15 December 2010 - 10:36 AM

Another task for people to do:

When you get in the path of an enemy, you can stop moving. The enemy will pass through you without HitDetect() method being called.
Was This Post Helpful? 0
  • +
  • -

#5 R4P  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 01-December 10

Posted 15 December 2010 - 11:07 AM

View PostRobin19, on 15 December 2010 - 04:36 PM, said:

Another task for people to do:

When you get in the path of an enemy, you can stop moving. The enemy will pass through you without HitDetect() method being called.



i handled it , and perform the previous tasks

have a look in my code .

********REMOVED LINK***********




MOD EDIT: Please do not post links to download sites. If you have code to share, copy and paste the code into your post in code tags

:code:

This post has been edited by JackOfAllTrades: 24 December 2010 - 02:10 PM

Was This Post Helpful? 0
  • +
  • -

#6 krjay  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 24-December 10

Posted 24 December 2010 - 08:49 AM

I'm just starting out with C# myself, have a few basics down but when I get this far no rectangles paint on my form:
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 BoxGame
{
    public partial class Form1 : Form
    {
        private Rectangle Goal = new Rectangle(350, 600, 50, 50);
        private Rectangle Player = new Rectangle(0, 150, 75, 75);
        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);
        }

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


Was This Post Helpful? 0
  • +
  • -

#7 alexdinca  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 23-January 11

Posted 29 January 2011 - 11:26 AM

i have the same problem as krjay. the rectangles don't appear.
Was This Post Helpful? 0
  • +
  • -

#8 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 24 July 2011 - 05:21 AM

After reading some of the comments; I noticed that it was not drawing also; Here is some code for just a target rectangle and a player rectangle; Very simple and adaptable to the overall project.

I'm sorry it took so long, However I had done this tutorial a while ago while I was still grasping the basics.

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Box
{
    public partial class MainWindow : Form
    {
        private Rectangle Player = new Rectangle(0, 0, 25, 25);
        private Rectangle Target = new Rectangle(100, 100, 25, 25);
        private short X, Y;
        
        public MainWindow()
        {
            InitializeComponent();
            
        }

        private void Evaluate()
        {
            if (X > 300 || X < 0)
            {
                X = 0;
            }

            else if (Y > 300 || Y < 0)
            {
                Y = 0;
            }

            if (Player.IntersectsWith(Target))
            {
                MessageBox.Show("Player Intersected with Blue Target.");
            }
        }

        private void MainWindow_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(Pens.Red, Player);
            e.Graphics.DrawRectangle(Pens.Blue, Target);
        }

        private void MainWindow_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.W:
                    Player.Location = new Point(X, Y-= 10);
                    break;
                case Keys.A:
                    Player.Location = new Point(X-= 10, Y);
                    break;
                case Keys.S:
                    Player.Location = new Point(X, Y+=10);
                    break;
                case Keys.D:
                    Player.Location = new Point(X+=10, Y);
                    break;
            }
            Evaluate();
            this.Invalidate();
        }
    }
}


This post has been edited by Fungle: 24 July 2011 - 05:22 AM

Was This Post Helpful? 1
  • +
  • -

#9 Fallenrat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 14-October 11

Posted 15 October 2011 - 04:52 AM

Wrote all the code and it didnt work...hrrmmm...Showed a blank window.
Wrote it in Visual C# Express 2010 in a windows form application in the Form.cs. Is that right?
Was This Post Helpful? 0
  • +
  • -

#10 loosCanon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-November 11

Posted 12 November 2011 - 11:26 PM

Thanx! This was a really easy and understandable little game. I hope u don't mind but I added a few tweaks. -.-

This might be a little confusing but DON'T type "private void Form1_Paint(object sender, PaintEventArgs e) .....". In the design view, select your form and in the properties box click on the little thunderbolt to add an event, then double click on paint. Now copy the code inside the { }.
private void Form1_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawRectangle(Pens.Red, Goal);
    e.Graphics.DrawRectangle(Pens.Blue, Player);
    e.Graphics.DrawRectangle(Pens.Red, Enemy1);
    e.Graphics.DrawRectangle(Pens.Red, Enemy2);
}



Remember to add event on this and NOT to copy the "private void Form1_KeyDown(object sender, KeyEventArgs e)".
 
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    int PlayerX = Player.Location.X;
    int PlayerY = Player.Location.Y;
}



I changed some thing on the Form1_KeyDown method, it just worked better for me but the original code works too.
The if statement that check if your moving off the screen is part of the switch statement in mine.
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 - 10 >= 0)
                {
                    Player.Location = new Point(PlayerX += 0, PlayerY -= 20);
                    this.Refresh();
                }
	            break;
            case Keys.Down:
                if (Player.Location.Y + 20 < 680)
                {
                    Player.Location = new Point(PlayerX += 0, PlayerY += 20);
                    this.Refresh();
                }
	            break;
	        case Keys.Left:
                if (Player.Location.X - 20 >= 0)
                {
                    Player.Location = new Point(PlayerX -= 20, PlayerY += 0);
                    this.Refresh();
                }
	            break;
	        case Keys.Right:
                if (Player.Location.X + 20 < 680)
                {
                    Player.Location = new Point(PlayerX += 20, PlayerY += 0);
                    this.Refresh();
                }
	            break;
	        }
        }



One more thing I did is to call the HitDetect(); after the last this.refresh(); in the timer1_Tick method. When u move into a enemy's path and stop moving the enemy will pass through you without detecting the hit, the previous fix... well fixes this problem.
Was This Post Helpful? 0
  • +
  • -

#11 loosCanon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-November 11

Posted 12 November 2011 - 11:39 PM

Quote

One more thing I did is to call the HitDetect(); after the last this.refresh(); in the timer1_Tick method. When u move into a enemy's path and stop moving the enemy will pass through you without detecting the hit, the previous fix... well fixes this problem.


If you use this you will have to add "Player.Location = new Point(350, 0);" to the HitDetect(); method.

if (Player.IntersectsWith(Goal))
{
Player.Location = new Point(350, 0);
MessageBox.Show("You Win!");
}



Was This Post Helpful? 0
  • +
  • -

#12 Bursted  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-April 12

Posted 15 April 2012 - 01:06 PM

A good excercise for someone like me who is new to C#. getting past the blank window problem and setting up the design / code timers was fun.
Was This Post Helpful? 0
  • +
  • -

#13 Fungle  Icon User is offline

  • D.I.C Regular
  • member icon

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

Posted 19 August 2012 - 02:50 PM

I'd like to issue an apology for my lack of support and activity for this tutorial. I wrote this tutorial as I was still new so there are issues with it.

In terms of the lack of support I have had some real life issues so I needed to keep stress to a minimum.

I am truely sorry, but I am glad some of you have been able to learn from what is essentially a broken tutorial. It appears to give guidance on the code structure and leaves it to you to do what programming is all about, problem solving.

I will do my best to help in the future and look forward to becoming more active on here.
Was This Post Helpful? 0
  • +
  • -

#14 cocoto4  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 15-November 12

Posted 20 November 2012 - 03:12 PM

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?
Was This Post Helpful? 0
  • +
  • -

#15 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:14 AM

Nice tutorial and worked perfectly but then I changed to reconize Left, Right, Up and Down but KeyDown event is not being generadted for this keys.. What can be wrong? (I did put a ShowMessage() to test if the event was generated but the message do not display with cursor keys, but with other keys all work fine. KeyPreview property is true...
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2