Timer = Draw;
Height = 600;
Width = 500;
void Draw()
{
// Flappy
////////////////////
EllipseMode(EllipseModes.Corner);
Circle(Width / 2, 500 / 2, 40);
}
To make it easier to check if Flappy got through the "flight path", instead of drawing the circle from the center, draw the circle from the top corner.
Timer = Draw;
Height = 600;
Width = 500;
double yFlappy = 500 / 2;
void Draw()
{
// Flappy
////////////////////
if (KeyIsPressed == true)
{
if (KeyCode == KeyCodes.Up)
yFlappy = yFlappy - 5;
else if (KeyCode == KeyCodes.Down)
yFlappy = yFlappy + 5;
}
EllipseMode(EllipseModes.Corner);
Circle(Width / 2, yFlappy, 40);
}
If you try your program, you will notice that the previously drawn circles have not been erased. You can do this by calling the Background function at the beginning of the Draw function. Set the background color to a light shade of gray, e.g. 192.
double yFlappy = 500 / 2;
double xPillar = Width;
void Draw()
{
Background(192);
...
// Pillars
////////////////////
RectMode(RectModes.Corner);
Rect(xPillar, 0, 20, Height);
xPillar = xPillar - 10;
}
If you entered everything correctly, the pillar (well, the rectangle) should move from right to left and disappear from the canvas.
...
// Pillars
////////////////////
if(xPillar < 0)
{
xPillar = Width;
}
RectMode(RectModes.Corner);
Rect(xPillar, 0, 20, Height);
xPillar = xPillar - 10;
}
...
double xPillar = Width;
double yPillarBelow = Random(100, Height - 100);
void Draw()
{
...
// Pillars
////////////////////
if(xPillar < 0)
{
xPillar = Width;
yPillarBelow = Random(100, Height - 100);
}
RectMode(RectModes.Corner);
Rect(xPillar, 0, 20, yPillarBelow - 100);
Rect(xPillar, yPillarBelow, 20, 600 - yPillarBelow);
xPillar = xPillar - 5;
}
You can "trick" here. By setting the initial value for xPillar to the value -1, the first time the Draw function is executed, xPillar and yPillarBelow will be set to the correct start values. This means you don't need to set the start values for xPillar and yPillarBelow separately.
...
double xPillar = -1;
double yPillarBelow;
void Draw()
{
...
if(xPillar < 0)
{
xPillar = Width;
yPillarBelow = Random(100, Height - 100);
}
...
...
double xFlappy = Width / 2;
if(xFlappy >= xPillar && xFlappy <= xPillar + 5)
{
if(yFlappy >= yPillarBelow-100 &&
yFlappy <= yPillarBelow - 40)
correct = correct + 1;
else
wrong = wrong + 1;
}
xPillar = xPillar - 5;
}
The first if-statement checks if Flappy has reached the pillars. If this expression is true, you have to check in a new if-statement whether Flappy is inside the "flight path" and has hit the gap correctly. To do this, Flappy's y-coordinate, which you remember in yFlappy, must be between yPillarBelow and yPillarBelow – 100. Only the single coordinate point is considered. In addition, the height of Flappy must be taken into account, i.e. 40 pixels.
...
int correct;
int wrong;
void Draw()
{
...
// Hit test
////////////////////
double xFlappy = Width / 2;
if(xFlappy >= xPillar && xFlappy <= xPillar + 5)
{
if(yFlappy >= yPillarBelow-100 &&
yFlappy <= yPillarBelow-40)
correct = correct + 1;
else
wrong = wrong + 1;
}
Text($"good {correct} / bad {wrong}");
xPillar = xPillar - 5;
}
If you want, you can change the font size and font using the TextSize and TextFont functions. Your Flappy game should work now.
...
void Draw()
{
Background(192);
Image(@"C:\Images\Background.png", 0, 0);
...
When you start the program, the background is still static and doesn't move. To do this, the x-coordinate must change and the image must be "moved" to the left each time the Draw function is called. The x-coordinate must therefore be reduced and becomes negative as a result. To do this, create a variable x outside of the draw function and set it to 0. Each time you call the draw function, you decrease the x variable by a fixed amount, e.g. by -4.
...
double x; // Background
void Draw()
{
Background(192);
Image(@"C:\Images\Background.png", x, 0);
x = x - 4;
...
Now the background should move to the left. At some point, however, the picture ends and the light gray background appears completely (see figure).
...
void Draw()
{
Background(192);
Image(@"C:\Images\Background.png", x, 0);
x = x - 4;
if (x < -(1000 / 2))
x = 0; // start background again
...
...
void Draw()
{
...
// Flappy
////////////////////
if (KeyIsPressed == true)
{
if (KeyCode == KeyCodes.Up)
yFlappy = yFlappy - 5;
else if (KeyCode == KeyCodes.Down)
yFlappy = yFlappy + 5;
}
//EllipseMode(EllipseModes.Corner);
//Circle(Width / 2, yFlappy, 40);
Image(@"C:\Images\Flappy.png", Width / 2, yFlappy);
...
This completes the Flappy game and you can program additional features such as a start screen and so on. To further improve your programming code, you should assign speaking variables for constant values such as the width and height of Flappy or the speed of the background image. You can then use these instead of the constant values. This will make your programming code easier to understand. Have fun programming and of course playing.
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License