rric

Rückstoß_mit_Pfeiltasten

Dec 5th, 2023 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.68 KB | None | 0 0
  1. # Ufo-Spiel mit Meeresblick
  2. # Copyright (C) 2021, 2023  Roland Richter             [Mu: Pygame Zero]
  3.  
  4. import random
  5.  
  6. # Fenstergröße 800 x 500
  7. TITLE = "Rückstoß mit Pfeiltasten"
  8. WIDTH = 800
  9. HEIGHT = 500
  10.  
  11. # Bilder stammen von https://kenney.nl/assets/alien-ufo-pack;
  12. # Dateinamen wurden in kleinbuchstaben umgewandelt
  13. ufo = Actor("kenney_alien-ufo-pack/png/shippink_manned.png")
  14. dome = Actor("kenney_alien-ufo-pack/png/dome.png")
  15. ufo.pos = (WIDTH-40, 140)
  16. dome.pos = (WIDTH-40, 140-30)
  17.  
  18. beam = Actor("kenney_alien-ufo-pack/png/laseryellow2.png")
  19. game_over = False
  20.  
  21. speed = +3  # horizontale Geschwindigkeit: ⭠ negativ, positiv ⭢
  22. lift = 0  # vertikale Geschwindigkeit: ⭣ negativ, positiv ⭡
  23.  
  24. frame_count = 0
  25. total_time = 0
  26.  
  27.  
  28. def draw():
  29.     # Zähle die Anzahl der gezeichneten frames mit
  30.     global frame_count
  31.     frame_count += 1
  32.  
  33.     # Setze den Hintergrund auf "Light sky blue"
  34.     screen.fill("#87CEEB")
  35.  
  36.     # Zeichne eine Insel in "La Salle Green"
  37.     screen.draw.filled_circle((0.5*WIDTH, HEIGHT), 0.3*HEIGHT, "#087830")
  38.  
  39.     # Male das Meer quer über den unteren Teil des Fensters in "Dark cerulean"
  40.     screen.draw.filled_rect(Rect((0, 0.8*HEIGHT), (WIDTH, HEIGHT)), "#08457E")
  41.  
  42.     # Male eine weiße Wolke ...
  43.     screen.draw.filled_circle((0.75*WIDTH, 0.30*HEIGHT), 42, "white")
  44.     screen.draw.filled_circle((0.72*WIDTH, 0.31*HEIGHT), 42, "white")
  45.     screen.draw.filled_circle((0.77*WIDTH, 0.28*HEIGHT), 42, "white")
  46.     screen.draw.filled_circle((0.79*WIDTH, 0.32*HEIGHT), 42, "white")
  47.  
  48.     # ... und die gelbe Sonne
  49.     screen.draw.filled_circle((150, 70), 45, "yellow")
  50.  
  51.     # Schreibe Informationen in die linke obere Ecke
  52.     info = "time:" + str(round(total_time, 2))
  53.     screen.draw.text(info, (10, 10), color="black", fontsize=24)
  54.  
  55.     # Zeichne das UFO mit der Hülle
  56.     ufo.draw()
  57.     dome.draw()
  58.  
  59.     # Zeige Strahl an, solange UFO hoch steigt
  60.     if lift > 0:
  61.         beam.midtop = (ufo.x, ufo.bottom - 30)
  62.         beam.draw()
  63.  
  64.     if game_over:
  65.         screen.draw.text("GAME OVER!", (200, HEIGHT / 2), color="red", fontsize=80)
  66.  
  67.  
  68. def update(delta):
  69.     global speed, lift, game_over, total_time
  70.  
  71.     if game_over:
  72.         return
  73.  
  74.     total_time += delta
  75.  
  76.     # Falls das UFO über dem Boden schwebt, dann passe seine
  77.     # Position mit Geschwindigkeit ⭤ und Auftrieb ⭥ an ...
  78.     if ufo.y < HEIGHT - 70:
  79.         ufo.x += speed
  80.         lift -= 0.2
  81.         ufo.y -= round(lift)
  82.     # ... sonst ist das Spiel zu Ende!
  83.     else:
  84.         # crash Geräusch von Gemesil, CC0
  85.         # https://freesound.org/people/Gemesil/sounds/545692/
  86.         sounds.gemesil_crash.play()
  87.         game_over = True
  88.  
  89.     # Falls rechter bzw. linker Rand erreicht, beginne am linken bzw rechten Rand
  90.     if speed > 0 and ufo.x > WIDTH:
  91.         ufo.x = 0
  92.     if speed < 0 and ufo.x < 0:
  93.         ufo.x = WIDTH
  94.  
  95.     # Lass das Ufo wackeln; erzeuge dazu Zufallszahlen für den Winkel ...
  96.     if not game_over:
  97.         winkel = random.randint(-8, 8)
  98.         ufo.angle = winkel
  99.     else:
  100.         ufo.angle = -45
  101.  
  102.     # ... und für die vertikale Position des UFOs
  103.     auf_ab = random.randint(-2, 2)
  104.     ufo.y += auf_ab
  105.  
  106.     # Die Hülle des UFOs muss mitbewegt werden
  107.     dome.x = ufo.x
  108.     dome.y = ufo.y - 30
  109.     dome.angle = ufo.angle
  110.  
  111.  
  112. # Verändere Geschwindigkeit und Auftrieb mit ⭠, ⭢ und ⭡; siehe auch
  113. # https://pygame-zero.readthedocs.io/en/stable/hooks.html#buttons-and-keys
  114. def on_key_down(key):
  115.     global speed, lift, total_time
  116.  
  117.     if key == keys.LEFT:  # ⭠: flieg nach links
  118.         speed = -3
  119.     if key == keys.RIGHT:  # ⭢: flieg nach rechts
  120.         speed = +3
  121.     if key == keys.UP:  # ⭡: schwebe nach oben
  122.         lift = +6
  123.     # Starte Spiel mit Leertaste neu: das UFO wird
  124.     # in den Himmel teleportiert, Geschwindigkeiten auf 0 gesetzt
  125.     global game_over
  126.     if game_over and key == keys.SPACE:
  127.         ufo.y = 0
  128.         speed = lift = 0
  129.         total_time = 0
  130.         game_over = False
  131.  
  132.  
  133. # ----------------------------------------------------------------------
  134. # This program is free software: you can redistribute it and/or modify
  135. # it under the terms of the GNU General Public License as published by
  136. # the Free Software Foundation, either version 3 of the License, or
  137. # (at your option) any later version.
  138. #
  139. # This program is distributed in the hope that it will be useful,
  140. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  141. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  142. # GNU General Public License for more details.
  143. #
  144. # You should have received a copy of the GNU General Public License
  145. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  146.  
Add Comment
Please, Sign In to add comment