New Posts  All Forums:Forum Nav:

Rectangle's in c# - Page 2

post #11 of 22
Quote:
Originally Posted by ipv89 View Post

Mmmm I see thank you for that information I think it might be easyr to use a class to create a rectangle than store it in an array.

Actually I think a big problem you have is trying to combine "rectangles" when you need to be combing "polygons" instead.

A polgon can be a rectangle, but once you combine rectangles of different sizes you have a polygon.

So make a polgon intersect and join function and then you can call that over and over again.
post #12 of 22
...and I completely forgot, the List collection is C# is variably sized. So this is doable. So now the trick is determining when two sides of the two polygons(ty kenny ^^^) intersect and getting the point where they intersect.

lol, now I want to make a snake game.

So here is what I am thinking so far.

You're going to need to add a List
Code:
public List<Point> pointCollection;

Soft of an initial constructor, so let's say you're working on a snake game in a 200x200 window and you want your snake to start right in the middle.
Code:
pointCollection.Add(new Point(100 - 2, 100 - 2));
pointCollection.Add(new Point(100 + 2, 100 - 2));
pointCollection.Add(new Point(100 + 2, 100 + 2));
pointCollection.Add(new Point(100 - 2, 100 + 2));

The +/- 2 is to emphasise that our snake will be 4 pixels wide.

Sadly you can't use the List object with any graphics drawing related method we need, so you have to create an array. This actually kind of sucks because if you have a ton of points, that will drastically slow down your program converrting to an array with every new polygon.
Code:
GraphicsObject.drawPolygon(Pen p, Points[] object);

Edited by Thereoncewasamaninparis - 5/29/13 at 3:18pm
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
post #13 of 22
Quote:
Originally Posted by Thereoncewasamaninparis View Post

...and I completely forgot, the List collection is C# is variably sized. So this is doable. So now the trick is determining when two sides of the two polygons(ty kenny ^^^) intersect and getting the point where they intersect.


So looking back at my code, you're going to create an all new Polygon Class, since the Graphics class cannot be inherited.


lol, now I want to make a snake game.

You would need to go through all sets of points for both polygons.
Find the highest and lowest points for both x and y (on both).
Check to see if any points overlap.
If so then add the polygon points together.

If you are using this to make a Snake game then you are doing it very hard.

Very easy way:

1) Make a queue of polygons (or rectangles).

2) Generate one random space (that isn't occupied) on board to be a "token".

3) Using a timer, every second add:
Right: +1 to all the x coordinates.
Left: -1 to all the x coordinates.
Up -1 to all the y coordinates.
Down: +1 to all the y coordinates.

You need 2 variables for this, lets say xMove and yMove.

4) When user hits:
Right key: xMove = +1
Left key: xMove = -1
Up key: yMove = -1
Down key: yMove = +1

5) When snake hits token (check snake position vs. token every time snake moves), simply add the token coordinates as a new polygon and add that into the queue.

The queue is first-in last-out. So when you add the token to the snake it just appears to latch onto original snake head).
post #14 of 22
Thread Starter 
Quote:
Originally Posted by kennyparker1337 View Post

You would need to go through all sets of points for both polygons.
Find the highest and lowest points for both x and y (on both).
Check to see if any points overlap.
If so then add the polygon points together.

If you are using this to make a Snake game then you are doing it very hard.

Very easy way:

1) Make a queue of polygons (or rectangles).

2) Generate one random space (that isn't occupied) on board to be a "token".

3) Using a timer, every second add:
Right: +1 to all the x coordinates.
Left: -1 to all the x coordinates.
Up -1 to all the y coordinates.
Down: +1 to all the y coordinates.

You need 2 variables for this, lets say xMove and yMove.

4) When user hits:
Right key: xMove = +1
Left key: xMove = -1
Up key: yMove = -1
Down key: yMove = +1

5) When snake hits token (check snake position vs. token every time snake moves), simply add the token coordinates as a new polygon and add that into the queue.

The queue is first-in last-out. So when you add the token to the snake it just appears to latch onto original snake head).

This sounds like it would work, ill try and see if i can get a basic snake game going with this. thumb.gif thank u
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #15 of 22
It does work. tongue.gif

I made my Snake game that way. biggrin.gif

BTW I love your avatar. Makes me laugh every time I see it. Dr. Who is a whoot.
post #16 of 22
Thread Starter 
Quote:
Originally Posted by kennyparker1337 View Post

It does work. tongue.gif

I made my Snake game that way. biggrin.gif

BTW I love your avatar. Makes me laugh every time I see it. Dr. Who is a whoot.

Hahaha thanks!

any examples of "Make a queue of polygons"? im still new to c#
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #17 of 22
Quote:
Originally Posted by ipv89 View Post

Quote:
Originally Posted by kennyparker1337 View Post

It does work. tongue.gif

I made my Snake game that way. biggrin.gif

BTW I love your avatar. Makes me laugh every time I see it. Dr. Who is a whoot.

Hahaha thanks!

any examples of "Make a queue of polygons"? im still new to c#

I would do this:
Code:
using System.Drawing;

private class SnakePart
{
   //This will make the snakepart invisible by default.
   //Feed this color instead to make it not invisible.
   public SnakePart()
   {
      Color = Color.Transparent;
   }

   public Rectangle Shape {get;set;}
   public Color Color {get;set;}
}

Queue<SnakePart> Snake = new Queue<SnakePart>();

If you make the Snake as long as the it possibly can be...
Say the gameboard can be 1000 pieces; make the snake 1000 snake parts.
Then when you add to the Snake, you won't have to do any calculations.
Code:
for(int i = 0; i < 1000; i++)
{
   Snake.Enqueue(new SnakePart() {Shape = ...});
}

If you want the Snake to start with 10 parts, then make the first 10 parts have a seen color like "Green" and the other 990 parts "Transparent".
Find the first SnakePart with the color "transparent" and then make it the snake color.
Code:
for(int i=0; i < 10; i++)
{
   Snake.Peek(i).Color = Color.Transparent;
   //Since SnakePart is a class, when we peek at it we can change subitems in it. Can't change the actual SnakePart itself though.
}

You can simply use an if statement checking on the color to avoid drawing 1000 rectangles and only what the user should see.
Code:
draw function()
{
    for(int i = 0; i < 1000; i++)
   {
      snakePart =  Snake.Peek(i);
      if(snakePart.Color != Color.Transparent)
      {
          //draw snakePart
      }
   }
}

I highly recommend you get it working with Rectangles first. They are much easier to work with than Polygons.
Edited by kennyparker1337 - 5/30/13 at 11:34am
post #18 of 22
Damn, I assumed he was working on an assignment given limitations of only one object or something.
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
Croissant
(15 items)
 
  
CPUMotherboardGraphicsRAM
2600k Asus Sabertooth Z77 GTX 680 2x4 gigs G-SKill 
Hard DriveOptical DriveCoolingOS
Crucial C300 2x Asus DVD-RW Silver Arrow Win 7 
MonitorPowerCaseAudio
Dual ASUS 24" Rosewill 750 Capstone Corsair Carbide Air 540 The Basic sound blaster Z-Series 
  hide details  
Reply
post #19 of 22
Thread Starter 
Quote:
Originally Posted by kennyparker1337 View Post

I would do this:
Code:
using System.Drawing;

private class SnakePart
{
   //This will make the snakepart invisible by default.
   //Feed this color instead to make it not invisible.
   public SnakePart()
   {
      Color = Color.Transparent;
   }

   public Rectangle Shape {get;set;}
   public Color Color {get;set;}
}

Queue<SnakePart> Snake = new Queue<SnakePart>();

If you make the Snake as long as the it possibly can be...
Say the gameboard can be 1000 pieces; make the snake 1000 snake parts.
Then when you add to the Snake, you won't have to do any calculations.
Code:
for(int i = 0; i < 1000; i++)
{
   Snake.Enqueue(new SnakePart() {Shape = ...});
}

If you want the Snake to start with 10 parts, then make the first 10 parts have a seen color like "Green" and the other 990 parts "Transparent".
Find the first SnakePart with the color "transparent" and then make it the snake color.
Code:
for(int i=0; i < 10; i++)
{
   Snake.Peek(i).Color = Color.Transparent;
   //Since SnakePart is a class, when we peek at it we can change subitems in it. Can't change the actual SnakePart itself though.
}

You can simply use an if statement checking on the color to avoid drawing 1000 rectangles and only what the user should see.
Code:
draw function()
{
    for(int i = 0; i < 1000; i++)
   {
      snakePart =  Snake.Peek(i);
      if(snakePart.Color != Color.Transparent)
      {
          //draw snakePart
      }
   }
}

I highly recommend you get it working with Rectangles first. They are much easier to work with than Polygons.

awesome thanks man I have managed to get a small game going now Im going to keep building upon it to learn more.
Going to try to fill the rectangles with images now thumb.gif

Quote:
Originally Posted by Thereoncewasamaninparis View Post

Damn, I assumed he was working on an assignment given limitations of only one object or something.

Not quite sure what you mean? there are no limitations im just trying to learn and ask here if I get stuck. my other threads that are regarding homework say so.
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
Mid range build
(12 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i5 3570k P8Z77-V ASUS ROG RX480 Corsair 8GB 2x 2GB + kingston hyperx 16gb 
Hard DriveOptical DriveCoolingOS
Samsung Evo SSD 500gb none corsair H80 windows 7 64 
MonitorPowerCaseMouse
Samsung s22b360 corsair tx 750 m NZXT phantom 410 (GM) logitech G400 
  hide details  
Reply
post #20 of 22
Good luck! Didn't mean to give so much code but that's just how I help.

Hope you can adapt all the code in to your own personal creation!

C# isn't the best at making games, but it sure can do a lot.
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming