|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
GoalsHaving established that my original design was too ambitious, the idea now is to simplify the drawing process while at the same time adding some elements of gameplay - planets that are not in a repeating pattern. that grow in number as the game progresses, and a ship we can control. Drawing the backgroundI've kept the code that makes the star field different each time, but now the star field no longer scrolls. This makes life a lot easier, because now the drawing all occurs in the The timer code now repositions the ship and the asteroids, and adds/removes them, but the planets and ship are drawn in the Adding planets and scoringTo add planets in a semi random manner, I use the Random class, which has a Next method which accepts a value which is the maximum returned by the random number generator. I score 10 points for every planet passed, and I increase the likelihood of a planet being added according to the score. The initial value of the counter is set to a negative to force a minimum gap between planets. The code looks like this: ++m_nAddPlanet;
Random r = new Random();
if (m_nAddPlanet > r.Next(Math.Max(300 - m_nScore, 2)))
{
m_nAddPlanet = - 10;
bool b = true;
Rectangle rcDest = new Rectangle();
while (b)
{
b = false;
rcDest = new Rectangle(640, r.Next(280), 87, 87);
for (int i = 0; i < m_arAsteroids.Count; ++i)
{
Asteroid arTemp = (Asteroid)m_arAsteroids[i];
if (rcDest.IntersectsWith(arTemp.rcAsteroid))
b = true;
}
}
Asteroid arAdd;
arAdd.rcAsteroid = rcDest;
arAdd.nBitmap = r.Next(8);
m_arAsteroids.Add(arAdd);
}
Tracking the planetsThe planets ( which are still called Asteroids in the code because of the initial design ) are defined in a public struct Asteroid
{
public Rectangle rcAsteroid;
public int nBitmap;
}
Because it's a for (int i = 0; i < m_arAsteroids.Count; ++i)
{
Asteroid arTemp = (Asteroid)m_arAsteroids[i];
arTemp.rcAsteroid.X -= 5;
m_arAsteroids[i] = arTemp;
if (arTemp.rcAsteroid.X < -87)
{
m_arAsteroids.RemoveAt(i);
--i;
m_nScore += 10;
}
e.Graphics.DrawImage(m_bmPlanets, arTemp.rcAsteroid,
87 * arTemp.nBitmap, 0, 87, 87,
GraphicsUnit.Pixel, iattrib);
}
Remember that incrementing the score adds to the speed in which planets are added, making the game increase in difficulty. The shipThe ship is pretty basic stuff - we create two variables to store the values in which to move the ship in the x and y direction, and set them to 1 or -1 when an arrow is pressed, and back to 0 when it is released. The code could easily be changed so that multiple presses accelerate/decelerate the ship. The ship's rect is stored, and changed by these values, then used to draw it. What's next ?The code still does not use any time synch code, because it still is just OK at full speed on my 700 MHz machine. The idea is next to check for collision, which we will make work in a pixel perfect manner, while hopefully retaining a playable speed, and to use C++ and DirectX next time we decide to write an action game. The game definitely slows down as more planets are added, and it is a little better (although it looks awful) if we turn off colour keying. Some speed increase may be possible if we were to use the
|
|||||||||||||||||||||||||||||||||||