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.
def draw():
pass
c = Canvas()
c.timer(draw)
The program should work so far. The draw function is called again and again at short intervals. Since the Ddraw function is not yet drawing, the painting area is of course still empty.
c = Canvas()
c.timer(draw)
# Background
x = 0
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 outside of the draw function.
def draw():
global x
background.move(x, 0)
x = x - 4
if x < -background.width() / 2:
x = 0 # Background from start position
c = Canvas()
c.timer(draw)
# Background
x = 0
background = c.image("C:\Images\Background.png")
c.height(background.height())
c.width(background.width() / 2)
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
yFlappy = background.height() / 2
xFlappy = background.width() / 2 / 2
flappy = c.image("C:\Images\Flappy.png", xFlappy, yFlappy)
When you start your program, Flappy should appear as desired. To move it, you use the keyCode function. Again, remember that this function 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.
def draw():
global x, xFlappy, yFlappy
...
# Flappy
###############################
if c.keyIsPressed() == True:
if c.keyCode() == UP:
yFlappy = yFlappy - 5
elif c.keyCode() == DOWN:
yFlappy = yFlappy + 5
flappy.move(xFlappy, yFlappy)
...
flappy = c.image("C:\Images\Flappy.png", xFlappy, yFlappy)
# Pillars
xPillar = -1
yPillarBelow = 0
c.rectMode(CORNER)
pillarAbove = c.rect()
pillarBelow = c.rect()
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.
def draw():
global x, xFlappy, yFlappy, xPillar, yPillar, yPillarBelow
...
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.
def draw():
global x, xFlappy, yFlappy, xPillar, yPillar, yPillarBelow, correct, wrong
...
xPillar = xPillar - 5
# Hit test
#################################
if xFlappy >= xPillar and xFlappy <= xPillar + 5:
if yFlappy >= yPillarBelow-100 and yFlappy <= yPillarBelow-flappy.height():
correct = correct + 1
else:
wrong = wrong + 1
...
pillarBelow = c.rect()
wrong = 0
correct = 0
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.
def draw():
...
# Hit test
#################################
if xFlappy >= xPillar and xFlappy <= xPillar + 5:
...
text.text(f"good {correct} / bad {wrong}")
...
wrong = 0
correct = 0
text = c.text("");
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