Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Re-written version of fly_my_birds:
- # - use functions to paint the seascape, to draw the bird, and to
- # update its position
- # - use one tuple birdpos instead of two variables birdx and birdy
- # Copyright 2023 Roland Richter
- from __future__ import division, print_function
- # Define the background colour; it should be constant, i.e. never be modified.
- # https://peps.python.org/pep-0008/ states: "Constants are ... written in
- # all capital letters with underscores separating words."
- BG_COLOUR = "#87CEEB" # Light sky blue
- # setup() is called only once, at start-up
- def setup():
- size(800, 500)
- background(BG_COLOUR)
- # The birds position: let it start at the upper right edge;
- # birdpos is a pair of coordinates (x,y); Python calls this a tuple.
- global birdpos
- birdpos = (width - 40, 0)
- frameRate(8)
- # draw() is called repeatedly
- def draw():
- # If (and only if) the left mouse button is pressed, the
- # canvas is erased each time, resulting in a "flip book" effect.
- if mousePressed:
- background(BG_COLOUR)
- paint_seascape()
- global birdpos
- birdpos = update(birdpos)
- draw_bird(birdpos)
- # The following functions are commented with a so-called docstring,
- # see https://peps.python.org/pep-0257/
- def paint_seascape():
- """Paint a green island, the sea, and the sun."""
- stroke("#483C32") # "Dark lava"
- fill("#087830") # "La Salle Green"
- circle(0.5*width, height, 0.6*height)
- noStroke()
- fill("#08457E") # "Dark cerulean"
- rect(0, 0.8*height, width, height)
- # Draw the sun
- fill("#FFFF00") # Yellow
- circle(0.1*width, 0.2*height, 90)
- def update(pos):
- """Return an updated position.
- Arguments:
- pos -- the current position as a (x,y) tuple
- """
- # The first item of a tuple has index 0, the second has index 1.
- return (pos[0] - 5, pos[1] + 2)
- def draw_bird(pos):
- """Draw a bird at the given position.
- Arguments:
- pos -- the current position as a (x,y) tuple
- """
- # Unpack the x and y value of the position
- (birdx, birdy) = pos
- stroke("#000000") # Black
- fill("#F3E5AB") # Medium champagne
- # Test whether frameCount is even or odd, then draw the corresponding
- # bird. In this way, the sketch constantly switches back and forth
- # between the two variants.
- if frameCount % 2 == 0:
- triangle(birdx, birdy, birdx-70, birdy-18, birdx-55, birdy-5)
- triangle(birdx, birdy, birdx+70, birdy-18, birdx+55, birdy-5)
- else:
- triangle(birdx, birdy, birdx-70, birdy-5, birdx-55, birdy+8)
- triangle(birdx, birdy, birdx+70, birdy-5, birdx+55, birdy+8)
- # ----------------------------------------------------------------------
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <https://www.gnu.org/licenses/>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement