Scrolling Game Development Kit Forum
General => Off-Topic => Topic started by: SmartBoy16 on 2010-02-27, 06:51:13 PM
-
This is somewhat related, but i am having a few issues for solidity.
I am making a Diver-dives-into-water-to-get-the-treasure-and-avoid-sharks game for a final. the game is made up of Windows form labels, a "lblPlayer" label as the player "sprite", and a "Grid[Row, Col]" resembling tiles. so far this is what i have to detect solidity.
// Move Player
x += dx;
y += dy;
PixelX = Convert.ToInt32(x);
PixelY = Convert.ToInt32(y);
bool HitSolid = false;
// Tile Interactions
for (int Row = 0; Row < Gh; Row++)
{
for (int Col = 0; Col < Gw; Col++)
{
if (lblPlayer.Bounds.IntersectsWith(Grid[Row, Col].Bounds))
{
//if (Grid[Row, Col].BackColor == Color.Gold) DONT DELETE THIS!!! THIS IS FOR GETTING THE TREASURE!!!
//{
// btnMap.Enabled = true;
//}
if ((Grid[Row, Col].BackColor == Color.Gray) || (Grid[Row, Col].BackColor == Color.SandyBrown)) // if the color of the tile is gray (rock) or SandyBrown (ground)
{
HitSolid = true;
//Check Vertical Collision
if (dy != 0) //if moving up or down
{
if (y + 20 > Grid[Row, Col].Bounds.Top && dy > 0) // if passed the top and moving downward
{
y = Grid[Row, Col].Bounds.Top - 22;
break;
}
else if (y < Grid[Row, Col].Bounds.Bottom && dy < 0) // else if passed the bottom and moving upward
{
y = Grid[Row, Col].Bounds.Bottom + 2;
break;
}
}
//Check Horizontal collision
if (dx != 0) // if moving left or right
{
if (x + 20 > Grid[Row, Col].Bounds.Left && dx > 0) // if passed the Leftside and moving right
{
x = Grid[Row, Col].Bounds.Left - 22;
break;
}
else if (x < Grid[Row, Col].Bounds.Right && dx < 0) // else if passed rightside and moving left
{
x = Grid[Row, Col].Bounds.Right + 2;
break;
}
}
}
}
}
if (HitSolid) break;
}
if (HitSolid)
{
PixelX = Convert.ToInt32(x);
PixelY = Convert.ToInt32(y);
}
// Draw Player
lblPlayer.Location = new Point(PixelX, PixelY);
this is all done in the timer with a 1 millisecond interval. (basically, as fast as it can go)
im using a keydown for movement and a keyup to stop the player
this is the link to the program in question: http://www.box.net/shared/6ao5z2qmzu
-
Two questions:
1. Is there a question in there somewhere? Or were you just stating facts?
2. Why are you running the solidity check every frame? Why not just run it once when the player taps up, down, left or right?
-
1. The question is, Is there an easier way to do this? (forgot to put that in there)
2. it was the way i was taught.
is there a way i can check once I push u/d/l/r, like in the KeyDown function?
-
EDIT:// Nevermind, didn't read through all the posts.
-
You shouldn't need a timer at all for any of this. Just run the collision detection code once, when the player taps a direction key. I'd have to know more about how your labels are laid out, but if they are in a grid, you shouldn't have to run through two loops either. You should just have to check the tile the player sprite is moving into, to see if it's solid. If it is, do nothing. If not, move the player into the new location.
-
this is the code for making the tiles.
public Form1()
{
InitializeComponent();
for (int Row = 0; Row < Gh; Row++) // draws grid
{
for (int Col = 0; Col < Gw; Col++)
{
Grid[Row, Col] = new Label();
Grid[Row, Col].Size = new Size(19, 19);
Grid[Row, Col].Location = new Point(50 + Col * 20, 50 + Row * 20);
Grid[Row, Col].BackColor = Color.Aqua;
Grid[Row, Col].BorderStyle = BorderStyle.FixedSingle;
Controls.Add(Grid[Row, Col]);
}
}
}
wait, did i mention that this is being done on a Windows Form?
-
It would be simpler to just draw colored rectangles during the OnPaint event instead of managing all those labels. The TileShapeTest program I just uploaded in another discussion is a good example.
System.Color[,] Grid;
...
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
for (short y=0; y<32; y++)
{
for (short x = 0; x<32; x++)
{
e.Graphics.FillRectangle(Grid[x,y].Color, x * 32, y * 32, 32, 32);
}
}
}