Advertisement
rric

dunkelblauer_meeresblick

Oct 17th, 2023 (edited)
815
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.87 KB | None | 0 0
  1. # Lerne, wie man mit Kreisen und Rechtecken malen kann
  2. # Copyright 2023 Roland Richter                        [Mu: Pygame Zero]
  3.  
  4. # Erzeuge ein rechteckiges 800×500 Fenster
  5. TITLE = "Dunkelblauer Meeresblick"
  6. WIDTH = 800
  7. HEIGHT = 500
  8.  
  9. frame_count = 0
  10.  
  11. # Die Funktion draw() wird von Pygame Zero aufgerufen, um das Fenster neu
  12. # zu malen; siehe https://pygame-zero.readthedocs.io/en/stable/hooks.html
  13. def draw():
  14.     # Ich möchte den Himmel in "skyblue2" einfärben. Die Funktion fill()
  15.     # malt das gesamte Fenster in einer Farbe aus -- perfekt! ... oder?
  16.     # ÄNDERE den Code so, dass die richtige Farbe verwendet wird.
  17.     # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  18.     screen.fill("steelblue2")
  19.     # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  20.  
  21.     # Male das Meer in "royalblue4" im unteren Teil des Fensters, und
  22.     # male dahinter eine Insel in "forestgreen"
  23.     # ÄNDERE den Code so, dass die Insel nicht mehr vor dem Meer liegt.
  24.     # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  25.     screen.draw.filled_rect(Rect((0, 400), (WIDTH, 100)), "royalblue4")
  26.     screen.draw.filled_circle((400, 500), 150, "forestgreen")
  27.     # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  28.  
  29.     global sun_pos
  30.     # Male eine gelbe Sonne ... Hm, du kannst die Sonne nicht sehen?
  31.     # ÄNDERE die Position so, dass eine gelbe Sonne sichtbar ist
  32.     # ↓---------↓---------↓---------↓---------↓---------↓---------↓
  33.     sun_pos = (850, 70)
  34.     screen.draw.filled_circle(sun_pos, 55, "orange")
  35.     # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  36.  
  37.     # Male zum Schluß noch eine weiße Wolke ...
  38.     screen.draw.filled_circle((0.75 * WIDTH, 0.30 * HEIGHT), 42, "white")
  39.     screen.draw.filled_circle((0.72 * WIDTH, 0.31 * HEIGHT), 42, "white")
  40.     screen.draw.filled_circle((0.77 * WIDTH, 0.28 * HEIGHT), 42, "white")
  41.     screen.draw.filled_circle((0.79 * WIDTH, 0.32 * HEIGHT), 42, "white")
  42.  
  43.     global frame_count
  44.     frame_count += 1
  45.  
  46.  
  47. # HALT! Änderungen unter dieser Zeile sind verboten!
  48. # ↑---------↑---------↑---------↑---------↑---------↑---------↑
  49.  
  50. # This dictionary is essentially a reveresed small part of
  51. # https://github.com/pygame/pygame/blob/main/src_py/colordict.py
  52. # Color (R, G, B) is converted to key 65536*R + 256*G + B
  53. color_names = {}
  54.  
  55. def rgb2int(r, g, b):
  56.     return 65536 * r + 256 * g + b
  57.  
  58. def fail_string(name, val, exp):
  59.     return "\n  " + name + " ist " + str(val) + ", sollte aber " + str(exp) + " sein"
  60.  
  61. def test_pixel(pos, xpct_int):
  62.     pxl = screen.surface.get_at_mapped(pos)
  63.     if not pxl == xpct_int:
  64.         print(
  65.             "  Pixel" + str(pos) + " ist " + color_names[pxl] + ",",
  66.             "sollte aber " + color_names[xpct_int] + " sein",
  67.         )
  68.  
  69. def update():
  70.     global color_names
  71.     # update() wird vor draw() aufgerufen; prüfe asserts erst *nach* dem ersten draw()
  72.     if frame_count == 0:
  73.         color_names[rgb2int(34, 139, 34)] = "forestgreen"
  74.         color_names[rgb2int(126, 192, 238)] = "skyblue2"
  75.         color_names[rgb2int(92, 172, 238)] = "steelblue2"
  76.         color_names[rgb2int(39, 64, 139)] = "royalblue4"
  77.         color_names[rgb2int(255, 165, 0)] = "orange"
  78.         color_names[rgb2int(255, 255, 0)] = "yellow"
  79.         color_names[rgb2int(255, 255, 255)] = "white"
  80.         return
  81.  
  82.     sf_size = screen.surface.get_size()
  83.     assert sf_size == (800, 500)
  84.  
  85.     if not (sun_pos[0] >= 55 and sun_pos[0] <= 745):
  86.         print(fail_string("Der x-Wert von sun_pos", sun_pos[0], "zwischen 55 und 745"))
  87.     if not (sun_pos[1] >= 55 and sun_pos[1] <= 345):
  88.         print(fail_string("Der y-Wert von sun_pos", sun_pos[0], "zwischen 55 und 345"))
  89.  
  90.     test_pixel((400, 5), rgb2int(126, 192, 238))  # skyblue2
  91.     test_pixel((400, 450), rgb2int(39, 64, 139))  # royalblue4
  92.     test_pixel((400, 390), rgb2int(34, 139, 34))  # forestgreen
  93.  
  94.     if (sun_pos[0] >= 55 and sun_pos[0] <= 745) and (
  95.         sun_pos[1] >= 55 and sun_pos[1] <= 345
  96.     ):
  97.         test_pixel(sun_pos, rgb2int(255, 255, 0))  # yellow
  98.  
  99.  
  100. # ----------------------------------------------------------------------
  101. # This program is free software: you can redistribute it and/or modify
  102. # it under the terms of the GNU General Public License as published by
  103. # the Free Software Foundation, either version 3 of the License, or
  104. # (at your option) any later version.
  105. #
  106. # This program is distributed in the hope that it will be useful,
  107. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  108. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  109. # GNU General Public License for more details.
  110. #
  111. # You should have received a copy of the GNU General Public License
  112. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  113. # Write your code here :-)
  114.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement