Canvas c = new Canvas();
You can take a look at this by starting your program several times. With each call, an instance and thus a new painting surface is created on which you can draw.
Canvas c = new Canvas();
c.Timer = Draw;
void Draw()
{
}
The program should work so far. The Draw function is called again and again at short intervals. Since the Draw function is not yet drawing, the painting area is of course still empty.
Canvas c = new Canvas();
c.Timer = Draw;
// Background
double x;
CanvasElement background = c.Image(@"C:\Images\Background.png");
The Image method returns an instance of the CanvasElement class. So you get a single object returned, which you can use to move the image or change its size, for example. From the first Flappy version you already know that the x-coordinate has to change in order for the background to move from right to left. You can therefore create a variable x of the double data type outside of the draw function.
...
CanvasElement background = c.Image(@"C:\Images\Background.png");
c.Height = background.Height;
c.Width = background.Width / 2;
void Draw()
{
background.Move(x, 0);
x = x - 4;
if (x < -background.Width / 2)
x = 0; // Background from start position
}
Each time the Draw function is called, you decrease the x variable by a fixed amount, say -4. You can now use the Move method to move the background image using the background instance variable. Since the background image disappears on the left edge after several runs, you must check again whether x is smaller than the width of the image. Also remember that the image must be duplicated as in the first flappy game version, i.e. appear twice in the image file and the beginning and end of the image must be identical. If the variable x is then at the negative value of half the width of the background, it must be set to 0 again.
...
c.Width = background.Width / 2;
// Flappy
double yFlappy = background.Height / 2;
double xFlappy = background.Width / 2 / 2;
CanvasElement flappy = c.Image(@"C:\Images\Flappy.png", xFlappy, yFlappy);
void Draw()
{
...
When you start your program, Flappy should appear as desired. To move it, you use the KeyCode variable. Again, remember that this variable belongs to the Canvas class instance, so you access the variable using the dot notation. When the cursor up or down key is pressed, Flappy should move 5 pixels in the corresponding direction. To do this, decrease or increase the variable yFlappy. Flappy can then use the Move method to change its position.
...
void Draw()
{
...
// Flappy
////////////////////
if (c.KeyIsPressed == true)
{
if (c.KeyCode == KeyCodes.Up)
yFlappy = yFlappy - 5;
else if (c.KeyCode == KeyCodes.Down)
yFlappy = yFlappy + 5;
}
flappy.Move(xFlappy, yFlappy);
}
...
// Pillars
double xPillar = -1;
double yPillarBelow;
c.RectMode(RectModes.Corner);
CanvasElement pillarAbove = c.Rect();
CanvasElement pillarBelow = c.Rect();
void Draw()
{
...
To create the rectangles, no size and position are specified yet, so the position and size are set to 0. In the Draw function you now change the position and size of the rectangles. To do this, you set the xPillar and yPillarBelow variables if the x-coordinate has become negative. Set xPillar back to the position on the right side, i.e. to the width of the canvas. For yPillarBelow you determine a random number between 100 and the height of the canvas minus 100.
...
flappy.Move(xFlappy, yFlappy);
// Pillars
////////////////////
if(xPillar < 0)
{
xPillar = c.Width;
yPillarBelow = Random(100, c.Height - 100);
}
pillarAbove.Move(xPillar, 0);
pillarAbove.Resize(20, yPillarBelow-100);
pillarBelow.Move(xPillar, yPillarBelow);
pillarBelow.Resize(20, 600 - yPillarBelow);
xPillar = xPillar - 5;
Then the two pillars can be moved using Move and the size can be changed using Resize. First you pass the width and then the height of the rectangle to resize. To move the pillars to the left, you reduce the x-coordinate xPillar by 5.
...
xPillar = xPillar - 5;
// Hit test
////////////////////
if(xFlappy >= xPillar && xFlappy <= xPillar + 5)
{
if(yFlappy >= yPillarBelow-100 &&
yFlappy <= yPillarBelow-flappy.Height)
correct = correct + 1;
else
wrong = wrong + 1;
}
}
To make the correct and wrong variables appear on the canvas, call the Text method of the Canvas class. As with calling the Image or Rect method, this returns an instance of the CanvasElement class, which you can use to set the text.
...
CanvasElement pillarBelow = c.Rect();
// result
int correct;
int wrong;
CanvasElement text = c.Text("");
void Draw()
{
...
// Hit test
////////////////////
if(xFlappy >= xPillar && xFlappy <= xPillar + 5)
{
...
text.Text($"good {correct} / bad {wrong}");
}
}
You have now programmed the Flappy game with objects. You have not yet created your own classes, but used the Canvas and CanvasElement classes for this. If you compare the scope with the first solution, you will find that the object-oriented program contains a few more lines of code.This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License