Advertisement
rric

schwebende_ufo_bits

Feb 6th, 2024 (edited)
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.40 KB | None | 0 0
  1. # Eine Menge an Bäumen ziehen vorbei ...
  2. # Diese Version lässt sich mit einem micro:bit steuern
  3. # Copyright 2023, 2024 Roland Richter                   [Mu: Pygame Zero]
  4.  
  5. import random
  6. import serial
  7. import serial.tools.list_ports
  8.  
  9. # Erzeuge ein rechteckiges 800×500 Fenster
  10. TITLE = "Schwebende UFO-Sterne"
  11. WIDTH = 800
  12. HEIGHT = 500
  13. frame_count = 0
  14.  
  15. horizon_y = 400
  16.  
  17. ufo = Actor("kenney_alien-ufo-pack/png/shippink_manned.png", (WIDTH//2, 140))
  18. dome = Actor("kenney_alien-ufo-pack/png/dome.png", (WIDTH//2, 140 - 30))
  19. star = None
  20. ufo_speed = 1
  21. ufo_lift = 0
  22. star_descent = 0
  23.  
  24. # Erzeuge 125 Bäume:
  25. # - horizontal alle 80 Pixel
  26. # - vertikal zwischen 350 und 450
  27. # Der Anchor-Punkt ist oben in der Mitte, wo der Stern befestigt werden soll.
  28. wood = []
  29. for num in range(125):
  30.     tree = Actor(
  31.         "kenney_platformer-art-winter/tiles/pinesapling.png",
  32.         anchor=("center", "top"),
  33.         midtop=(80 * num + WIDTH//2, random.randint(350, 450)),
  34.     )
  35.     wood.append(tree)
  36.  
  37. # Es gibt zwei Listen von Sternen:
  38. # - die Sterne, die an einem Baum befestigt wurden
  39. # - die Sterne, die hinunter gefallen und am Boden verstreut sind
  40. attached = []
  41. scattered = []
  42.  
  43. game_finished = False
  44.  
  45. # Holiday / Seasonal 4 by Diego Nava from https://mixkit.co/free-stock-music/tag/christmas/
  46. music.play("mixkit-holiday-seasonal-4-511.mp3")
  47. music.set_volume(0.4)
  48.  
  49. # USB VID/PID numbers for micro:bit, see
  50. # https://support.microbit.org/support/solutions/articles/19000035697-what-are-the-usb-vid-pid-numbers-for-micro-bit
  51. MICROBIT_VID = 0x0D28
  52. MICROBIT_PID = 0x0204
  53. MICROBIT_BAUD = 115200
  54.  
  55. def open_serial_port(vid, pid, baud):
  56.     """Scan all ports, and return the port with given parameters if possible."""
  57.     ports = serial.tools.list_ports.comports()
  58.     for p in ports:
  59.         print("  ", p, "vid:", p.vid, "pid:", p.pid, "serial:", p.serial_number)
  60.         if (p.vid == vid) and (p.pid == pid):
  61.             # Open the port with given parameters. Since readline() is used afterwards,
  62.             # a timeout has to be specified, see
  63.             # https://pyserial.readthedocs.io/en/latest/shortintro.html#shortintro-readline
  64.             microbit_port = serial.Serial(p.device, baud, timeout=0.005)
  65.             return microbit_port
  66.  
  67.     return None
  68.  
  69. # Entscheide, ob das Spiel mit Tastatur oder micr:bit gesteuert wird
  70. Keyboard, Microbit = range(2)
  71. input_device = Keyboard
  72.  
  73. print("scanning for micro:bit port ...")
  74. microbit_port = open_serial_port(MICROBIT_VID, MICROBIT_PID, MICROBIT_BAUD)
  75. if not microbit_port:
  76.     print("... not found.")
  77. else:
  78.     print("=> found micro:bit port.")
  79.     input_device = Microbit
  80.  
  81.  
  82. def draw():
  83.     screen.fill("skyblue2")
  84.  
  85.     # Male Schnee in den unteren Teil des Fensters
  86.     screen.draw.filled_rect(Rect((0, horizon_y), (WIDTH, HEIGHT - horizon_y)), "snow")
  87.  
  88.     # Male die Sonne
  89.     screen.draw.filled_circle((150, 70), 45, "yellow")
  90.  
  91.     # Male zuerst die am Boden verstreuten Sterne, ...
  92.     for scad in scattered:
  93.         scad.draw()
  94.  
  95.     # ... dann die Bäume, und dann erst ...
  96.     for tree in wood:
  97.         tree.draw()
  98.  
  99.     # ... die an Bäumen befestigten Sterne.
  100.     for attd in attached:
  101.         attd.draw()
  102.  
  103.     if star_descent:
  104.         star.draw()
  105.  
  106.     # Zeichne das UFO mit der Hülle
  107.     ufo.draw()
  108.     dome.draw()
  109.  
  110.     # PROBIERE Zeige in einer Ecke des Fensters Informationen an, z.B.
  111.     # - wie viele Sterne an einer Baumspitze befestigt sind;
  112.     # - wie viele Sterne das UFO insgesamt abgeworfen hat.
  113.     # TIPP verwende die Länge der Listen 'attached' und 'scattered'
  114.  
  115.     global frame_count
  116.     frame_count += 1
  117.  
  118.  
  119. def update():
  120.     global game_finished, wood, horizon_y, ufo_speed, ufo_lift, star, star_descent
  121.  
  122.     # Lass das Ufo wackeln; erzeuge dazu einen zufälligen Winkel
  123.     winkel = random.randint(-8, 8)
  124.     ufo.angle = winkel
  125.  
  126.     # Die Hülle des UFOs muss ebenfalls mitbewegt werden
  127.     dome.x = ufo.x
  128.     dome.y = ufo.y - 30
  129.     dome.angle = ufo.angle
  130.  
  131.     if ufo_speed != 0:
  132.         for tree in wood:
  133.             tree.x -= ufo_speed
  134.         for scad in scattered:
  135.             scad.x -= ufo_speed
  136.         for attd in attached:
  137.             attd.x -= ufo_speed
  138.  
  139.     if ufo_lift != 0:
  140.         horizon_y += ufo_lift
  141.         for tree in wood:
  142.             tree.y += ufo_lift
  143.         for scad in scattered:
  144.             scad.y += ufo_lift
  145.         for attd in attached:
  146.             attd.y += ufo_lift
  147.         ufo_lift = 0
  148.  
  149.     if star is not None:
  150.         star.y += star_descent
  151.         star_descent += 0.2
  152.  
  153.         # Falls der Stern zu tief nach unten gefallen ist, ...
  154.         if star.y >= horizon_y+90:
  155.             # ... bleibt er verstreut am Boden liegen;
  156.             star.angle = random.randint(45, 135)
  157.             scattered.append(star)
  158.             star = None
  159.             star_descent = 0
  160.             sounds.impactglass_light_003.play()
  161.         # falls der Stern noch hinunter fällt, ...
  162.         else:
  163.             # ... wird für jede Baumspitze die Distanz berechnet;
  164.             for tree in wood:
  165.                 # falls der Stern ganz nahe zu einer Baumspitze ist, ...
  166.                 if tree.distance_to(star) <= 9.0:
  167.                     # ... wird er jetzt an diesem Baum befestigt.
  168.                     star.center = tree.midtop
  169.                     attached.append(star)
  170.                     star = None
  171.                     star_descent = 0
  172.                     sounds.impactbell_heavy_000.play()
  173.                     break
  174.  
  175.     # Wenn der letzte Baum erreicht wird, ist das Spiel zu Ende!
  176.     if wood[-1].x <= WIDTH//2:
  177.         game_finished = True
  178.         ufo_speed = -3
  179.  
  180.     # Die Tastatur wird automatisch überprüft; micro:bit muss extra überprüft werden
  181.     if input_device == Microbit:
  182.         handle_microbit()
  183.  
  184.  
  185. # Verändere Geschwindigkeit und Auftrieb mit ⭠, ⭢ und ⭡; siehe auch
  186. # https://pygame-zero.readthedocs.io/en/stable/hooks.html#buttons-and-keys
  187. def on_key_down(key):
  188.     global game_finished, wood, horizon_y, ufo_speed, ufo_lift, star, star_descent
  189.  
  190.     if game_finished or input_device != Keyboard:
  191.         return
  192.  
  193.     if key == keys.LEFT:  # ⭠: flieg nach links
  194.         if ufo_speed >= -5:
  195.             ufo_speed -= 1
  196.     if key == keys.RIGHT:  # ⭢: flieg nach rechts
  197.         if ufo_speed <= 5:
  198.             ufo_speed += 1
  199.  
  200.     if key == keys.UP:  # ⭡: schwebe nach oben
  201.         if horizon_y <= 420:
  202.             ufo_lift = +3
  203.     if key == keys.DOWN:  # : schwebe nach unten
  204.         if horizon_y >= 360:
  205.             ufo_lift = -3
  206.  
  207.     if star is None and key == keys.SPACE:
  208.         star = Actor("kenney_platformer-pack-redux/png/items/star.png", (WIDTH//2, 140))
  209.         star_descent = 0.5
  210.  
  211.  
  212. # Verändere Geschwindigkeit und Auftrieb mit microbit
  213. def handle_microbit():
  214.     global game_finished, wood, horizon_y, ufo_speed, ufo_lift, star, star_descent
  215.  
  216.     if game_finished or input_device != Microbit:
  217.         return
  218.  
  219.     microbit_line = microbit_port.readline().decode("utf-8")
  220.  
  221.     if not microbit_line:
  222.         return
  223.  
  224.     button_a_state = microbit_line[0]
  225.     gyro_x_state = microbit_line[2]
  226.     gyro_y_state = microbit_line[3]
  227.  
  228.     if gyro_x_state == '<':  # ⭠: flieg nach links
  229.         if ufo_speed >= -5:
  230.             ufo_speed -= 0.1
  231.     if gyro_x_state == '>':  # ⭢: flieg nach rechts
  232.         if ufo_speed <= 5:
  233.             ufo_speed += 0.1
  234.  
  235.     if gyro_y_state == '^':  # ⭡: schwebe nach oben
  236.         if horizon_y <= 420:
  237.             ufo_lift = +3
  238.     if gyro_y_state == 'v':  # : schwebe nach unten
  239.         if horizon_y >= 360:
  240.             ufo_lift = -3
  241.  
  242.     if star is None and button_a_state == 'A':
  243.         star = Actor("kenney_platformer-pack-redux/png/items/star.png", (WIDTH//2, 140))
  244.         star_descent = 0.5
  245.  
  246.  
  247. # ----------------------------------------------------------------------
  248. # This program is free software: you can redistribute it and/or modify
  249. # it under the terms of the GNU General Public License as published by
  250. # the Free Software Foundation, either version 3 of the License, or
  251. # (at your option) any later version.
  252. #
  253. # This program is distributed in the hope that it will be useful,
  254. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  255. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  256. # GNU General Public License for more details.
  257. #
  258. # You should have received a copy of the GNU General Public License
  259. # along with this program.  If not, see <https://www.gnu.org/licenses/>.
  260.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement