Advertisement
rric

iterate_code_versions

Oct 30th, 2023 (edited)
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.44 KB | None | 0 0
  1. # Re-written version of fly_my_birds:
  2. # - use functions to paint the seascape, to draw the bird, and to
  3. #     update its position
  4. # - use one tuple birdpos instead of two variables birdx and birdy
  5. # Copyright 2023 Roland Richter
  6.  
  7. from __future__ import division, print_function
  8.  
  9. # Define the background colour; it should be constant, i.e. never be modified.
  10. # https://peps.python.org/pep-0008/ states: "Constants are ... written in
  11. # all capital letters with underscores separating words."
  12. BG_COLOUR = "#87CEEB"   # Light sky blue
  13.  
  14. # setup() is called only once, at start-up
  15. def setup():
  16.     size(800, 500)
  17.     background(BG_COLOUR)
  18.    
  19.     # The birds position: let it start at the upper right edge;
  20.     # birdpos is a pair of coordinates (x,y); Python calls this a tuple.
  21.     global birdpos
  22.     birdpos = (width - 40, 0)
  23.     frameRate(8)
  24.  
  25.  
  26. # draw() is called repeatedly
  27. def draw():
  28.     # If (and only if) the left mouse button is pressed, the
  29.     # canvas is erased each time, resulting in a "flip book" effect.
  30.     if mousePressed:
  31.         background(BG_COLOUR)
  32.  
  33.     paint_seascape()
  34.    
  35.     global birdpos
  36.     birdpos = update(birdpos)
  37.    
  38.     draw_bird(birdpos)
  39.  
  40.  
  41. # The following functions are commented with a so-called docstring,
  42. # see https://peps.python.org/pep-0257/
  43.  
  44. def paint_seascape():
  45.     """Paint a green island, the sea, and the sun."""
  46.     stroke("#483C32")  # "Dark lava"
  47.     fill("#087830")    # "La Salle Green"
  48.     circle(0.5*width, height, 0.6*height)
  49.  
  50.     noStroke()
  51.     fill("#08457E") # "Dark cerulean"
  52.     rect(0, 0.8*height, width, height)
  53.  
  54.     # Draw the sun
  55.     fill("#FFFF00")  # Yellow
  56.     circle(0.1*width, 0.2*height, 90)
  57.  
  58.  
  59. def update(pos):
  60.     """Return an updated position.
  61.        
  62.    Arguments:
  63.    pos -- the current position as a (x,y) tuple
  64.    """
  65.     # The first item of a tuple has index 0, the second has index 1.
  66.     return (pos[0] - 5, pos[1] + 2)
  67.  
  68.  
  69. def draw_bird(pos):
  70.     """Draw a bird at the given position.
  71.        
  72.    Arguments:
  73.    pos -- the current position as a (x,y) tuple
  74.    """
  75.     # Unpack the x and y value of the position
  76.     (birdx, birdy) = pos
  77.    
  78.     stroke("#000000")   # Black
  79.     fill("#F3E5AB")     # Medium champagne
  80.    
  81.     # Test whether frameCount is even or odd, then draw the corresponding
  82.     # bird. In this way, the sketch constantly switches back and forth
  83.     # between the two variants.
  84.     if frameCount % 2 == 0:
  85.         triangle(birdx, birdy, birdx-70, birdy-18, birdx-55, birdy-5)
  86.         triangle(birdx, birdy, birdx+70, birdy-18, birdx+55, birdy-5)
  87.     else:
  88.         triangle(birdx, birdy, birdx-70, birdy-5, birdx-55, birdy+8)
  89.         triangle(birdx, birdy, birdx+70, birdy-5, birdx+55, birdy+8)
  90.    
  91. # ----------------------------------------------------------------------
  92. # This program is free software: you can redistribute it and/or modify
  93. # it under the terms of the GNU General Public License as published by
  94. # the Free Software Foundation, either version 3 of the License, or
  95. # (at your option) any later version.
  96. #
  97. # This program is distributed in the hope that it will be useful,
  98. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  99. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  100. # GNU General Public License for more details.
  101. #
  102. # You should have received a copy of the GNU General Public License
  103. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  104.    
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement