From 45c41601421e44a0a74330d79bbce485a3765777 Mon Sep 17 00:00:00 2001 From: Oliver Hofmann Date: Mon, 23 Sep 2024 13:22:12 +0200 Subject: [PATCH] Init --- .idea/.gitignore | 8 ++ .idea/Solutions.iml | 10 ++ .idea/inspectionProfiles/Project_Default.xml | 13 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/misc.xml | 7 ++ .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + 00_Reeborg/lessons/Endlos.py | 13 ++ 00_Reeborg/lessons/Erdbeerpflücker.py | 9 ++ 00_Reeborg/lessons/Erdbeerzeile.py | 13 ++ 00_Reeborg/lessons/Good Morning.py | 24 ++++ 00_Reeborg/lessons/Laufen.py | 15 +++ 00_Reeborg/lessons/Robin Hood (fehlerhaft).py | 19 +++ 00_Reeborg/lessons/Robin Hood.py | 19 +++ 00_Reeborg/lessons/Wand.py | 10 ++ 00_Reeborg/projects/adventure.py | 14 +++ 00_Reeborg/projects/bob.py | 26 ++++ 00_Reeborg/projects/donkey.py | 36 ++++++ 00_Reeborg/projects/double_it.py | 42 +++++++ 00_Reeborg/projects/meter.py | 53 ++++++++ 00_Reeborg/projects/party.py | 51 ++++++++ 00_Reeborg/projects/pyramide.py | 70 +++++++++++ 00_Reeborg/projects/tulip.py | 19 +++ 01_Turtle/lessons/01_simple_line.py | 10 ++ 01_Turtle/lessons/02_dotted_line.py | 14 +++ 01_Turtle/lessons/03_colored_line.py | 22 ++++ 01_Turtle/lessons/04_RGB_color.py | 12 ++ 01_Turtle/lessons/05_square.py | 14 +++ 01_Turtle/lessons/06_sized_square.py | 14 +++ 01_Turtle/lessons/07_filled_square.py | 21 ++++ 01_Turtle/lessons/07_filled_square_methods.py | 23 ++++ 01_Turtle/lessons/08_hello_world.py | 7 ++ 01_Turtle/lessons/09_h_letter.py | 18 +++ 01_Turtle/projects/ampel.py | 48 +++++++ 01_Turtle/projects/flags.py | 41 ++++++ 01_Turtle/projects/germany.py | 36 ++++++ 01_Turtle/projects/heart.py | 22 ++++ 01_Turtle/projects/hello.py | 66 ++++++++++ 01_Turtle/projects/mandala.py | 20 +++ 01_Turtle/projects/rotating_square.py | 15 +++ 01_Turtle/projects/santas_house.py | 27 ++++ 01_Turtle/projects/skyscraper.py | 50 ++++++++ 01_Turtle/projects/torte.py | 31 +++++ 01_Turtle/projects/yinyang.py | 70 +++++++++++ 01_Turtle/projects/zielscheibe.py | 26 ++++ 02_Console/lessons/01_hello_world.py | 3 + 02_Console/lessons/02_lines.py | 3 + 02_Console/lessons/03_vars.py | 18 +++ 02_Console/lessons/04_type_conversion.py | 32 +++++ 02_Console/lessons/05_expressions.py | 29 +++++ 02_Console/lessons/06_average.py | 17 +++ 02_Console/lessons/07_return.py | 11 ++ 02_Console/lessons/08_magic_numbers.py | 6 + 02_Console/projects/big_money.py | 24 ++++ 02_Console/projects/calculator.py | 9 ++ 02_Console/projects/countdown.py | 6 + 02_Console/projects/even_numbers.py | 13 ++ 02_Console/projects/money.py | 15 +++ 02_Console/projects/squares.py | 8 ++ 02_Console/projects/temperatur.py | 6 + 02_Console/projects/what_time.py | 9 ++ 03_Games/assignments/analog_clock.py | 118 ++++++++++++++++++ 03_Games/assignments/billard.py | 112 +++++++++++++++++ 03_Games/assignments/connect_the_clicks.py | 70 +++++++++++ 03_Games/lessons/01_sequenzen.py | 39 ++++++ 03_Games/lessons/02_black_window_game.py | 58 +++++++++ 03_Games/lessons/03_scope.py | 34 +++++ 03_Games/lessons/04_walker.py | 72 +++++++++++ 03_Games/lessons/05_walker_improved.py | 96 ++++++++++++++ 69 files changed, 1906 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/Solutions.iml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 00_Reeborg/lessons/Endlos.py create mode 100644 00_Reeborg/lessons/Erdbeerpflücker.py create mode 100644 00_Reeborg/lessons/Erdbeerzeile.py create mode 100644 00_Reeborg/lessons/Good Morning.py create mode 100644 00_Reeborg/lessons/Laufen.py create mode 100644 00_Reeborg/lessons/Robin Hood (fehlerhaft).py create mode 100644 00_Reeborg/lessons/Robin Hood.py create mode 100644 00_Reeborg/lessons/Wand.py create mode 100644 00_Reeborg/projects/adventure.py create mode 100644 00_Reeborg/projects/bob.py create mode 100644 00_Reeborg/projects/donkey.py create mode 100644 00_Reeborg/projects/double_it.py create mode 100644 00_Reeborg/projects/meter.py create mode 100644 00_Reeborg/projects/party.py create mode 100644 00_Reeborg/projects/pyramide.py create mode 100644 00_Reeborg/projects/tulip.py create mode 100644 01_Turtle/lessons/01_simple_line.py create mode 100644 01_Turtle/lessons/02_dotted_line.py create mode 100644 01_Turtle/lessons/03_colored_line.py create mode 100644 01_Turtle/lessons/04_RGB_color.py create mode 100644 01_Turtle/lessons/05_square.py create mode 100644 01_Turtle/lessons/06_sized_square.py create mode 100644 01_Turtle/lessons/07_filled_square.py create mode 100644 01_Turtle/lessons/07_filled_square_methods.py create mode 100644 01_Turtle/lessons/08_hello_world.py create mode 100644 01_Turtle/lessons/09_h_letter.py create mode 100644 01_Turtle/projects/ampel.py create mode 100644 01_Turtle/projects/flags.py create mode 100644 01_Turtle/projects/germany.py create mode 100644 01_Turtle/projects/heart.py create mode 100644 01_Turtle/projects/hello.py create mode 100644 01_Turtle/projects/mandala.py create mode 100644 01_Turtle/projects/rotating_square.py create mode 100644 01_Turtle/projects/santas_house.py create mode 100644 01_Turtle/projects/skyscraper.py create mode 100644 01_Turtle/projects/torte.py create mode 100644 01_Turtle/projects/yinyang.py create mode 100644 01_Turtle/projects/zielscheibe.py create mode 100644 02_Console/lessons/01_hello_world.py create mode 100644 02_Console/lessons/02_lines.py create mode 100644 02_Console/lessons/03_vars.py create mode 100644 02_Console/lessons/04_type_conversion.py create mode 100644 02_Console/lessons/05_expressions.py create mode 100644 02_Console/lessons/06_average.py create mode 100644 02_Console/lessons/07_return.py create mode 100644 02_Console/lessons/08_magic_numbers.py create mode 100644 02_Console/projects/big_money.py create mode 100644 02_Console/projects/calculator.py create mode 100644 02_Console/projects/countdown.py create mode 100644 02_Console/projects/even_numbers.py create mode 100644 02_Console/projects/money.py create mode 100644 02_Console/projects/squares.py create mode 100644 02_Console/projects/temperatur.py create mode 100644 02_Console/projects/what_time.py create mode 100644 03_Games/assignments/analog_clock.py create mode 100644 03_Games/assignments/billard.py create mode 100644 03_Games/assignments/connect_the_clicks.py create mode 100644 03_Games/lessons/01_sequenzen.py create mode 100644 03_Games/lessons/02_black_window_game.py create mode 100644 03_Games/lessons/03_scope.py create mode 100644 03_Games/lessons/04_walker.py create mode 100644 03_Games/lessons/05_walker_improved.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/Solutions.iml b/.idea/Solutions.iml new file mode 100644 index 0000000..2c80e12 --- /dev/null +++ b/.idea/Solutions.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 0000000..23b1c8c --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,13 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..3fd7f67 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,7 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..1f3df03 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/00_Reeborg/lessons/Endlos.py b/00_Reeborg/lessons/Endlos.py new file mode 100644 index 0000000..b02e807 --- /dev/null +++ b/00_Reeborg/lessons/Endlos.py @@ -0,0 +1,13 @@ +while front_is_clear(): + move() + turn_left() + turn_left() + + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_around(): + turn_left() + turn_left() \ No newline at end of file diff --git a/00_Reeborg/lessons/Erdbeerpflücker.py b/00_Reeborg/lessons/Erdbeerpflücker.py new file mode 100644 index 0000000..b8f47c1 --- /dev/null +++ b/00_Reeborg/lessons/Erdbeerpflücker.py @@ -0,0 +1,9 @@ +def take_if_there(): + if object_here(): + take() + + +for _ in range(9): + take_if_there() + move() +take_if_there() diff --git a/00_Reeborg/lessons/Erdbeerzeile.py b/00_Reeborg/lessons/Erdbeerzeile.py new file mode 100644 index 0000000..297993d --- /dev/null +++ b/00_Reeborg/lessons/Erdbeerzeile.py @@ -0,0 +1,13 @@ +for i in range(10): + put() + move() +put() + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_right(): + turn_left() + turn_left() + turn_left() \ No newline at end of file diff --git a/00_Reeborg/lessons/Good Morning.py b/00_Reeborg/lessons/Good Morning.py new file mode 100644 index 0000000..3e4e29a --- /dev/null +++ b/00_Reeborg/lessons/Good Morning.py @@ -0,0 +1,24 @@ +def turn_right(): + turn_left() + turn_left() + turn_left() + +move() +move() +turn_left() +move() +move() +turn_right() +move() +take() +turn_left() +turn_left() +move() +move() +turn_left() +move() +put() +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ diff --git a/00_Reeborg/lessons/Laufen.py b/00_Reeborg/lessons/Laufen.py new file mode 100644 index 0000000..2ca8949 --- /dev/null +++ b/00_Reeborg/lessons/Laufen.py @@ -0,0 +1,15 @@ +move() +move() +take() +turn_left() +move() +turn_left() +turn_left() +turn_left() +move() +move() +put() +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ diff --git a/00_Reeborg/lessons/Robin Hood (fehlerhaft).py b/00_Reeborg/lessons/Robin Hood (fehlerhaft).py new file mode 100644 index 0000000..1cce21d --- /dev/null +++ b/00_Reeborg/lessons/Robin Hood (fehlerhaft).py @@ -0,0 +1,19 @@ +def do_robin(): + if object_here(): + take() + if not object_here(): + put() + +while front_is_clear(): + do_robin() + move() +do_robin() + + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_around(): + turn_left() + turn_left() \ No newline at end of file diff --git a/00_Reeborg/lessons/Robin Hood.py b/00_Reeborg/lessons/Robin Hood.py new file mode 100644 index 0000000..842ce7b --- /dev/null +++ b/00_Reeborg/lessons/Robin Hood.py @@ -0,0 +1,19 @@ +def do_robin(): + if object_here(): + take() + else: + put() + +while front_is_clear(): + do_robin() + move() +do_robin() + + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_around(): + turn_left() + turn_left() \ No newline at end of file diff --git a/00_Reeborg/lessons/Wand.py b/00_Reeborg/lessons/Wand.py new file mode 100644 index 0000000..72e47d4 --- /dev/null +++ b/00_Reeborg/lessons/Wand.py @@ -0,0 +1,10 @@ +while front_is_clear(): + move() + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_around(): + turn_left() + turn_left() \ No newline at end of file diff --git a/00_Reeborg/projects/adventure.py b/00_Reeborg/projects/adventure.py new file mode 100644 index 0000000..7bead4c --- /dev/null +++ b/00_Reeborg/projects/adventure.py @@ -0,0 +1,14 @@ +from library import turn_right + +while wall_on_right(): + move() +turn_left() +turn_left() +move() +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_right(): + for _ in range(3): + turn_left() \ No newline at end of file diff --git a/00_Reeborg/projects/bob.py b/00_Reeborg/projects/bob.py new file mode 100644 index 0000000..ca6879c --- /dev/null +++ b/00_Reeborg/projects/bob.py @@ -0,0 +1,26 @@ +def repair_stone(): + if object_here(): + pass + else: + put() + +def repair_pillar(): + turn_left() + while front_is_clear(): + repair_stone() + move() + repair_stone() + turn_left() + turn_left() + while front_is_clear(): + move() + turn_left() + move() + +think(0) +for _ in range(3): + move() + move() + repair_pillar() + + \ No newline at end of file diff --git a/00_Reeborg/projects/donkey.py b/00_Reeborg/projects/donkey.py new file mode 100644 index 0000000..657e36b --- /dev/null +++ b/00_Reeborg/projects/donkey.py @@ -0,0 +1,36 @@ +def turn_around(): + turn_left() + turn_left() + +def turn_right(): + turn_left() + turn_left() + turn_left() + +def collect_star(): + if object_here(): + take() + +def collect_all_in_row(): + while front_is_clear(): + collect_star() + move() + collect_star() + +def move_up(): + turn_around() + while wall_on_right(): + move() + turn_right() + move() + turn_left() + while front_is_clear(): + move() + turn_around() + + +think(3) +for _ in range(8): + collect_all_in_row() + move_up() +collect_all_in_row() diff --git a/00_Reeborg/projects/double_it.py b/00_Reeborg/projects/double_it.py new file mode 100644 index 0000000..567b842 --- /dev/null +++ b/00_Reeborg/projects/double_it.py @@ -0,0 +1,42 @@ +def turn_around(): + turn_left() + turn_left() + +def turn_right(): + turn_left() + turn_left() + turn_left() + +def double_star(): + take() + move() + put() + put() + turn_around() + move() + turn_around() + +def double_all_stars(): + while object_here(): + double_star() + +def move_star(): + take() + move() + put() + turn_around() + move() + turn_around() + +def move_all_stars(): + while object_here(): + move_star() + + +move() +double_all_stars() +move() +turn_around() +move_all_stars() +move() +move() diff --git a/00_Reeborg/projects/meter.py b/00_Reeborg/projects/meter.py new file mode 100644 index 0000000..aa44c2c --- /dev/null +++ b/00_Reeborg/projects/meter.py @@ -0,0 +1,53 @@ +def turn_around(): + turn_left() + turn_left() + +def turn_right(): + turn_left() + turn_left() + turn_left() + +def set_stars(): + move() + while wall_on_right(): + put() + move() + +def move_to_start(): + turn_right() + move() + turn_right() + while right_is_clear(): + move() + while wall_on_right(): + move() + turn_right() + move() + turn_right() + move() + +def move_to_last_star(): + while object_here(): + move() + turn_around() + move() + turn_around() + +def move_to_end(): + while front_is_clear(): + move() + +def count_stars(): + move_to_start() + while object_here(): + move_to_last_star() + if object_here(): + take() + move_to_end() + put() + move_to_start() + + +set_stars() +count_stars() + diff --git a/00_Reeborg/projects/party.py b/00_Reeborg/projects/party.py new file mode 100644 index 0000000..07270fe --- /dev/null +++ b/00_Reeborg/projects/party.py @@ -0,0 +1,51 @@ +from library import turn_around, turn_right, move_to_wall + + +def clean_room(): + turn_left() + while front_is_clear(): + turn_right() + clean_row() + move_to_next_row() + turn_right() + clean_row() + + +def clean_row(): + while front_is_clear(): + clean_cell() + move() + clean_cell() + turn_around() + move_to_wall() + turn_around() + + +def clean_cell(): + if object_here(): + take() + + +def move_to_next_row(): + turn_left() + move() + +think(30) +clean_room() + + +################################################################ +# WARNING: Do not change this comment. +# Library Code is below. +################################################################ +def turn_right(): + for _ in range(3): + turn_left() + +def turn_around(): + turn_left() + turn_left() + +def move_to_wall(): + while front_is_clear(): + move() diff --git a/00_Reeborg/projects/pyramide.py b/00_Reeborg/projects/pyramide.py new file mode 100644 index 0000000..58627c1 --- /dev/null +++ b/00_Reeborg/projects/pyramide.py @@ -0,0 +1,70 @@ +def turn_right(): + turn_left() + turn_left() + turn_left() + + +def turn_around(): + turn_left() + turn_left() + + +def set_stone_above(): + turn_left() + move() + put() + turn_right() + move() + turn_right() + move() + turn_left() + + +def build_first_row(): + while front_is_clear(): + put() + move() + put() + turn_around() + while front_is_clear(): + move() + turn_around() + + +def build_second_row(): + move() + while front_is_clear(): + set_stone_above() + turn_left() + move() + turn_left() + move() + while object_here(): + move() + turn_around() + move() + + +def build_next_row(): + move() + while object_here(): + set_stone_above() + turn_left() + move() + turn_left() + move() + if object_here(): + take() + move() + while object_here(): + move() + turn_around() + move() + + +think(3) +set_trace_style("invisible") +build_first_row() +build_second_row() +while object_here(): + build_next_row() diff --git a/00_Reeborg/projects/tulip.py b/00_Reeborg/projects/tulip.py new file mode 100644 index 0000000..948ebda --- /dev/null +++ b/00_Reeborg/projects/tulip.py @@ -0,0 +1,19 @@ +def plant_a_tulip(): + move() + turn_left() + turn_left() + turn_left() + move() + put() + turn_left() + turn_left() + move() + turn_left() + turn_left() + turn_left() + move() + +while front_is_clear(): + plant_a_tulip() + + \ No newline at end of file diff --git a/01_Turtle/lessons/01_simple_line.py b/01_Turtle/lessons/01_simple_line.py new file mode 100644 index 0000000..1e53c75 --- /dev/null +++ b/01_Turtle/lessons/01_simple_line.py @@ -0,0 +1,10 @@ +# Sophia läuft 100 Schritte nach vorne + +from turtle import Turtle + +sophia = Turtle() +sophia.forward(100) + +sophia.screen.mainloop() + + diff --git a/01_Turtle/lessons/02_dotted_line.py b/01_Turtle/lessons/02_dotted_line.py new file mode 100644 index 0000000..4acfdbd --- /dev/null +++ b/01_Turtle/lessons/02_dotted_line.py @@ -0,0 +1,14 @@ +# Sophia zeichnet eine gestrichelte Linie + +from turtle import Turtle + +sophia = Turtle() +sophia.hideturtle() + +for _ in range(10): + sophia.penup() + sophia.forward(5) + sophia.pendown() + sophia.forward(5) + +sophia.screen.mainloop() diff --git a/01_Turtle/lessons/03_colored_line.py b/01_Turtle/lessons/03_colored_line.py new file mode 100644 index 0000000..fbb937e --- /dev/null +++ b/01_Turtle/lessons/03_colored_line.py @@ -0,0 +1,22 @@ +# Sophia zeichnet eine farbige Linie + +from turtle import Turtle + +sophia = Turtle() + +sophia.pensize(5) + +sophia.pencolor('green') +sophia.forward(30) +sophia.pencolor('blue') +sophia.forward(30) +sophia.pencolor('red') +sophia.forward(30) +sophia.pencolor('white') +sophia.forward(30) +sophia.pencolor('black') +sophia.forward(30) +sophia.pencolor('#c72426') +sophia.forward(30) + +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/lessons/04_RGB_color.py b/01_Turtle/lessons/04_RGB_color.py new file mode 100644 index 0000000..50fab14 --- /dev/null +++ b/01_Turtle/lessons/04_RGB_color.py @@ -0,0 +1,12 @@ +# Nutzung von RGP-Farben in Turtle-Grafiken + +from turtle import Turtle + +sophia = Turtle() + +sophia.pensize(50) +sophia.screen.colormode(255) # RGB-Farben aktivieren +sophia.pencolor(199, 36, 38) +sophia.forward(50) + +sophia.screen.mainloop() diff --git a/01_Turtle/lessons/05_square.py b/01_Turtle/lessons/05_square.py new file mode 100644 index 0000000..b567072 --- /dev/null +++ b/01_Turtle/lessons/05_square.py @@ -0,0 +1,14 @@ +# Zeichnen eines Quadrats mit der Turtle +from turtle import Turtle + +sophia = Turtle() + +# Sophia zeichnet ein Quadrat +def draw_square(): + for _ in range(4): + sophia.forward(100) + sophia.right(90) + +draw_square() + +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/lessons/06_sized_square.py b/01_Turtle/lessons/06_sized_square.py new file mode 100644 index 0000000..0c86084 --- /dev/null +++ b/01_Turtle/lessons/06_sized_square.py @@ -0,0 +1,14 @@ +# Zeichnen eines Quadrats variabler Größe mit der Turtle +from turtle import Turtle + +sophia = Turtle() + +# Eine Turtle zeichnet ein Quadrat +def draw_square(turtle, length): + for _ in range(4): + turtle.forward(length) + turtle.right(90) + +draw_square(sophia, 100) + +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/lessons/07_filled_square.py b/01_Turtle/lessons/07_filled_square.py new file mode 100644 index 0000000..231ea6f --- /dev/null +++ b/01_Turtle/lessons/07_filled_square.py @@ -0,0 +1,21 @@ +# Zeichnen eines gefüllten Quadrats mit der Turtle +from turtle import Turtle + +# Eine Turtle zeichnet ein Quadrat +def draw_square(turtle, length): + for _ in range(4): + turtle.forward(length) + turtle.right(90) + +# Eine Turtle zeichnet ein gefülltes Quadrat +def draw_filled_square(turtle, length): + turtle.begin_fill() + draw_square(turtle, length) + turtle.end_fill() + + +sophia = Turtle() +sophia.fillcolor('red') +draw_filled_square(sophia, 100) + +sophia.screen.mainloop() diff --git a/01_Turtle/lessons/07_filled_square_methods.py b/01_Turtle/lessons/07_filled_square_methods.py new file mode 100644 index 0000000..52902eb --- /dev/null +++ b/01_Turtle/lessons/07_filled_square_methods.py @@ -0,0 +1,23 @@ +# Eine Turtle erhält eine Methode zum Zeichnen eines gefüllten Quadrats +from turtle import Turtle + + +# Eine Turtle zeichnet ein Quadrat +def draw_square(turtle, length): + for _ in range(4): + turtle.forward(length) + turtle.right(90) + +# +def draw_filled_square(turtle, length): + turtle.begin_fill() + draw_square(turtle, length) + turtle.end_fill() + +# Eintragen der Methode draw_filled_square +Turtle.draw_filled_square = draw_filled_square +sophia = Turtle() +sophia.fillcolor('red') +sophia.draw_filled_square(100) + +sophia.screen.mainloop() diff --git a/01_Turtle/lessons/08_hello_world.py b/01_Turtle/lessons/08_hello_world.py new file mode 100644 index 0000000..ff8c25f --- /dev/null +++ b/01_Turtle/lessons/08_hello_world.py @@ -0,0 +1,7 @@ +from turtle import Turtle + +sophia = Turtle() + +sophia.write('Hello, World!', align='center', font=('Arial', 24, 'bold')) + +sophia.screen.mainloop() diff --git a/01_Turtle/lessons/09_h_letter.py b/01_Turtle/lessons/09_h_letter.py new file mode 100644 index 0000000..2e34450 --- /dev/null +++ b/01_Turtle/lessons/09_h_letter.py @@ -0,0 +1,18 @@ +from turtle import Turtle + +sophia = Turtle() + +def draw_h(turtle): + turtle.left(90) + turtle.forward(100) + turtle.backward(50) + turtle.right(90) + turtle.forward(50) + turtle.left(90) + turtle.forward(50) + turtle.backward(100) + turtle.right(90) + +draw_h(sophia) + +sophia.screen.mainloop() diff --git a/01_Turtle/projects/ampel.py b/01_Turtle/projects/ampel.py new file mode 100644 index 0000000..582e7df --- /dev/null +++ b/01_Turtle/projects/ampel.py @@ -0,0 +1,48 @@ +# Zeichnen einer Ampel + +from turtle import Turtle + +def draw_rectangle(turtle, width, height): + for _ in range(2): + turtle.forward(width) + turtle.left(90) + turtle.forward(height) + turtle.left(90) + +def draw_case(turtle): + turtle.pencolor('black') + turtle.fillcolor('black') + turtle.pendown() + turtle.begin_fill() + draw_rectangle(turtle, 70, 190) + turtle.end_fill() + turtle.penup() + +def draw_light(turtle, color): + turtle.pencolor(color) + turtle.fillcolor(color) + turtle.pendown() + turtle.begin_fill() + turtle.right(90) + turtle.circle(25) + turtle.end_fill() + turtle.penup() + turtle.left(90) + +def draw_traffic_light(turtle): + draw_case(turtle) + turtle.forward(35) + turtle.left(90) + turtle.forward(10) + draw_light(turtle, 'green') + turtle.forward(60) + draw_light(turtle, 'yellow') + turtle.forward(60) + draw_light(turtle, 'red') + + + +zoe = Turtle() +draw_traffic_light(zoe) +zoe.hideturtle() +zoe.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/flags.py b/01_Turtle/projects/flags.py new file mode 100644 index 0000000..26b0b02 --- /dev/null +++ b/01_Turtle/projects/flags.py @@ -0,0 +1,41 @@ +# Zeichnen von Flaggen mit der Turtle +from turtle import Turtle + +def draw_flag(turtle, color1, color2, color3): + turtle.pencolor('black') + turtle.pendown() + draw_rectangle(turtle, 600, 300) + turtle.penup() + draw_segment(turtle, color1) + draw_segment(turtle, color2) + draw_segment(turtle, color3) + +def draw_segment(turtle, color): + turtle.fillcolor(color) + draw_filled_rectangle(turtle, 200, 300) + turtle.forward(200) + + +def draw_filled_rectangle(turtle, width, height): + turtle.begin_fill() + draw_rectangle(turtle, width, height) + turtle.end_fill() + +def draw_rectangle(turtle, width, height): + for _ in range(2): + turtle.forward(width) + turtle.left(90) + turtle.forward(height) + turtle.left(90) + + +Turtle.draw_flag = draw_flag +sophia = Turtle() +sophia.draw_flag('green', 'white', 'red') +sophia.backward(600) +sophia.right(90) +sophia.forward(400) +sophia.left(90) +sophia.draw_flag('blue', 'white', 'red') +sophia.hideturtle() +sophia.screen.mainloop() diff --git a/01_Turtle/projects/germany.py b/01_Turtle/projects/germany.py new file mode 100644 index 0000000..cefa3ce --- /dev/null +++ b/01_Turtle/projects/germany.py @@ -0,0 +1,36 @@ +# Zeichnen der Flagge Deutschlands mit der Turtle + +from turtle import * + +def draw_rectangle(width, height): + begin_fill() + forward(width) + right(90) + forward(height) + right(90) + forward(width) + right(90) + forward(height) + right(90) + end_fill() + +def next_shape(): + penup() + right(90) + forward(50) + left(90) + pendown() + +pencolor('Black') +fillcolor('Black') +draw_rectangle(200, 50) +next_shape() +pencolor('Red') +fillcolor('Red') +draw_rectangle(200,50) +next_shape() +pencolor('Yellow') +fillcolor('Yellow') +draw_rectangle(200,50) + +done() diff --git a/01_Turtle/projects/heart.py b/01_Turtle/projects/heart.py new file mode 100644 index 0000000..5c3a080 --- /dev/null +++ b/01_Turtle/projects/heart.py @@ -0,0 +1,22 @@ +# Zeichnen +from turtle import Turtle + +def draw_heart(turtle): + turtle.left(40) + turtle.forward(178) + turtle.circle(90,200) + turtle.right(120) + turtle.circle(90,200) + turtle.forward(178) + + +sophia = Turtle() +sophia.screen.bgcolor('Black') +sophia.pencolor('Red') +sophia.fillcolor('Red') +sophia.begin_fill() +draw_heart(sophia) +sophia.end_fill() + +sophia.hideturtle() +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/hello.py b/01_Turtle/projects/hello.py new file mode 100644 index 0000000..a6b407c --- /dev/null +++ b/01_Turtle/projects/hello.py @@ -0,0 +1,66 @@ +# Zeichnen des Wortes "Hello" mit der Turtle + +from turtle import Turtle + +def space(turtle): + turtle.penup() + turtle.forward(30) + turtle.pendown() + +def draw_h(turtle): + turtle.left(90) + turtle.forward(100) + turtle.backward(50) + turtle.right(90) + turtle.forward(50) + turtle.left(90) + turtle.forward(50) + turtle.backward(100) + turtle.right(90) + +def draw_e(turtle): + turtle.left(90) + turtle.forward(100) + turtle.right(90) + turtle.forward(50) + turtle.backward(50) + turtle.right(90) + turtle.forward(50) + turtle.left(90) + turtle.forward(30) + turtle.backward(30) + turtle.right(90) + turtle.forward(50) + turtle.left(90) + turtle.forward(50) + +def draw_l(turtle): + turtle.left(90) + turtle.forward(100) + turtle.backward(100) + turtle.right(90) + turtle.forward(50) + +def draw_o(turtle): + turtle.penup() + turtle.forward(40) + turtle.pendown() + turtle.circle(50) + turtle.penup() + turtle.forward(40) + turtle.pendown() + +sophia = Turtle() +sophia.speed(0) +draw_h(sophia) +space(sophia) +draw_e(sophia) +space(sophia) +draw_l(sophia) +space(sophia) +draw_l(sophia) +space(sophia) +draw_o(sophia) + + +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/mandala.py b/01_Turtle/projects/mandala.py new file mode 100644 index 0000000..b97719f --- /dev/null +++ b/01_Turtle/projects/mandala.py @@ -0,0 +1,20 @@ +# Zeichnet ein Mandala mit der Turtle-Grafik + +from turtle import Turtle + +sophia = Turtle() +sophia.speed(0) + +for _ in range(36): + sophia.pencolor("Blue") + sophia.circle(100) + sophia.pencolor("Red") + sophia.forward(200) + sophia.left(120) + sophia.color("Orange") + sophia.forward(100) + sophia.left(70) + sophia.forward(15) + +sophia.screen.mainloop() + diff --git a/01_Turtle/projects/rotating_square.py b/01_Turtle/projects/rotating_square.py new file mode 100644 index 0000000..90b3494 --- /dev/null +++ b/01_Turtle/projects/rotating_square.py @@ -0,0 +1,15 @@ +from turtle import Turtle + +def draw_square(turtle, length): + for _ in range(4): + turtle.forward(length) + turtle.right(90) + +def draw_mandala(turtle, length): + for _ in range(18): + draw_square(turtle, length) + turtle.right(20) + +sophia = Turtle() +draw_mandala(sophia, 100) +sophia.screen.mainloop() diff --git a/01_Turtle/projects/santas_house.py b/01_Turtle/projects/santas_house.py new file mode 100644 index 0000000..d7f2310 --- /dev/null +++ b/01_Turtle/projects/santas_house.py @@ -0,0 +1,27 @@ +import math +from turtle import Turtle + +def draw_santas_house(turtle, length): + turtle.left(90) + turtle.forward(length) + turtle.right(90) + turtle.forward(length) + turtle.right(90 + 45) + turtle.forward(1.4142 * length) + turtle.left(90 + 45) + turtle.forward(length) + turtle.left(90 + 45) + turtle.forward(1.4142 * length) + turtle.right(45 + 30) + turtle.forward(length) + turtle.right(120) + turtle.forward(length) + turtle.right(30) + turtle.forward(length) + + + +Turtle.draw_santas_house = draw_santas_house +sophia = Turtle() +sophia.draw_santas_house(200) +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/skyscraper.py b/01_Turtle/projects/skyscraper.py new file mode 100644 index 0000000..0a5e67c --- /dev/null +++ b/01_Turtle/projects/skyscraper.py @@ -0,0 +1,50 @@ +from turtle import Turtle + +def draw_building(turtle): + turtle.fillcolor('black') + turtle.pencolor('black') + draw_filled_rectangle(turtle,100, 210) + turtle.left(90) + for _ in range(5): + draw_floor(turtle) + +def draw_floor(turtle): + turtle.penup() + turtle.forward(10) + turtle.right(90) + turtle.forward(10) + turtle.pendown() + for _ in range(3): + draw_window(turtle) + next_window(turtle) + turtle.penup() + turtle.backward(100) + turtle.left(90) + turtle.forward(30) + +def draw_window(turtle): + turtle.fillcolor('yellow') + turtle.pencolor('yellow') + draw_filled_rectangle(turtle, 20, 30) + +def next_window(turtle): + turtle.penup() + turtle.forward(30) + turtle.pendown() + + +def draw_filled_rectangle(turtle, width, height): + turtle.begin_fill() + for _ in range(2): + turtle.forward(width) + turtle.left(90) + turtle.forward(height) + turtle.left(90) + turtle.end_fill() + + +sophia = Turtle() +sophia.screen.bgcolor('blue') +draw_building(sophia) +sophia.hideturtle() +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/torte.py b/01_Turtle/projects/torte.py new file mode 100644 index 0000000..d95d66b --- /dev/null +++ b/01_Turtle/projects/torte.py @@ -0,0 +1,31 @@ +from turtle import Turtle + +def draw_segment(turtle, degree, length): + turtle.pendown() + turtle.begin_fill() + turtle.circle(length, degree) + turtle.left(90) + turtle.forward(length) + turtle.right(degree) + turtle.backward(length) + turtle.end_fill() + turtle.penup() + turtle.forward(length) + turtle.left(degree) + turtle.backward(length) + turtle.right(90) + + +Turtle.draw_segment = draw_segment +sophia = Turtle() +sophia.pencolor('red') +sophia.fillcolor('red') +sophia.draw_segment(240, 100) +sophia.pencolor('blue') +sophia.fillcolor('blue') +sophia.draw_segment(90, 100) +sophia.pencolor('green') +sophia.fillcolor('green') +sophia.draw_segment(30, 100) +sophia.hideturtle() +sophia.screen.mainloop() diff --git a/01_Turtle/projects/yinyang.py b/01_Turtle/projects/yinyang.py new file mode 100644 index 0000000..c9ea5fa --- /dev/null +++ b/01_Turtle/projects/yinyang.py @@ -0,0 +1,70 @@ +from turtle import Turtle + + + +sophia = Turtle() +zoe = Turtle() + +sophia.screen.bgcolor('grey') + +sophia.pencolor('black') +sophia.fillcolor('black') +zoe.pencolor('white') +zoe.fillcolor('white') + + +zoe.begin_fill() +zoe.left(90) +zoe.penup() +zoe.forward(300) +zoe.left(90) +zoe.circle(150, 180) +zoe.end_fill() + +sophia.begin_fill() +sophia.circle(150, 180) +sophia.penup() +sophia.left(90) +sophia.forward(300) +sophia.left(90) +sophia.end_fill() + +zoe.penup() +zoe.left(90) +zoe.forward(150) +zoe.right(90) +zoe.pendown() +zoe.begin_fill() +zoe.circle(75,180) +zoe.left(90) +zoe.forward(150) +zoe.end_fill() + +sophia.pendown() +sophia.begin_fill() +sophia.left(90) +sophia.forward(150) +sophia.left(90) +sophia.circle(75,180) +sophia.end_fill() + +zoe.penup() +zoe.forward(85) +zoe.left(90) +zoe.pendown() +zoe.begin_fill() +zoe.circle(10) +zoe.end_fill() + +sophia.penup() +sophia.left(90) +sophia.forward(235) +sophia.left(90) +sophia.pendown() +sophia.begin_fill() +sophia.circle(10) +sophia.end_fill() + +sophia.hideturtle() +zoe.hideturtle() +sophia.screen.mainloop() \ No newline at end of file diff --git a/01_Turtle/projects/zielscheibe.py b/01_Turtle/projects/zielscheibe.py new file mode 100644 index 0000000..41f51db --- /dev/null +++ b/01_Turtle/projects/zielscheibe.py @@ -0,0 +1,26 @@ +from turtle import Turtle + + +def draw_archery_target(turtle): + draw_circle(turtle, 30, 'red') + draw_circle(turtle, 20, 'white') + draw_circle(turtle, 10, 'red') + + +def draw_circle(turtle, radius, color): + turtle.fillcolor(color) + turtle.pencolor(color) + turtle.begin_fill() + turtle.pendown() + turtle.circle(radius) + turtle.penup() + turtle.end_fill() + turtle.left(90) + turtle.forward(10) + turtle.right(90) + + +sophia = Turtle() +draw_archery_target(sophia) +sophia.hideturtle() +sophia.screen.mainloop() \ No newline at end of file diff --git a/02_Console/lessons/01_hello_world.py b/02_Console/lessons/01_hello_world.py new file mode 100644 index 0000000..18fd421 --- /dev/null +++ b/02_Console/lessons/01_hello_world.py @@ -0,0 +1,3 @@ + +# This is a comment +print('Hello World!') \ No newline at end of file diff --git a/02_Console/lessons/02_lines.py b/02_Console/lessons/02_lines.py new file mode 100644 index 0000000..15bbe76 --- /dev/null +++ b/02_Console/lessons/02_lines.py @@ -0,0 +1,3 @@ +for _ in range(100): +# print('Ich darf Sophia nicht umdrehen.') + print('Ich darf Sophia nicht umdrehen.', end=' ') \ No newline at end of file diff --git a/02_Console/lessons/03_vars.py b/02_Console/lessons/03_vars.py new file mode 100644 index 0000000..a62f506 --- /dev/null +++ b/02_Console/lessons/03_vars.py @@ -0,0 +1,18 @@ +# Werte und Typen + +x = 17 +print(type(x)) + +y = '17' +print(type(y)) + +z = 17.0 +print(type(z)) + + +# Eingabe mit Ignorierung der Rückgabe +input('Gib Deinen Namen ein: ') + +# Eingabe mit Speicherung der Rückgabe +user_name = input('Gib Deinen Namen ein: ') +print('Hallo', user_name) \ No newline at end of file diff --git a/02_Console/lessons/04_type_conversion.py b/02_Console/lessons/04_type_conversion.py new file mode 100644 index 0000000..35e4831 --- /dev/null +++ b/02_Console/lessons/04_type_conversion.py @@ -0,0 +1,32 @@ +# Typen und ihre Konvertierung +def typfehler(): + p = '10' + for _ in range(p): + print('no way!') + +print(int("10")) +#print(int("10.6")) +#print(int("A")) +print(int(10.0)) +print(int(10.3)) +print(int(10.7)) + +print(str(10.7)) + +print(float(10)) +print(float("10")) +print(float("10.7")) +#print(float("A")) + +# Range erwartet Ganzzahlen +p = '10' +for _ in range(int(p)): + print('way!') + +# Ganzzahlige Division // vs Gleitkomma-Division / +print(3 // 2) +print(3 / 2) + +# Boolescher Typ +t = True +print(type(t)) diff --git a/02_Console/lessons/05_expressions.py b/02_Console/lessons/05_expressions.py new file mode 100644 index 0000000..d6527ae --- /dev/null +++ b/02_Console/lessons/05_expressions.py @@ -0,0 +1,29 @@ +# Thema: Ausdrücke + +# Ausführungsreihenfolge +print(3 * 4 - 8 // 2) +print(3 * (4 - 8) // 2) + +# Operatoren bei unterschiedlichen Typen +print(4.0 + 2) +print (4 * 2.0) +print(4 / 2) + +# Formatierte Ausgabe +print(f'Das Ergebnis ist {3+4}') + +s = 'Haus' +z = 7 +print(f'{s:10} ist ein Gebäude') +print(f'{z:010} ist eine Zahl') + +euro = 20 / 3 +print(f'Das kostet {euro:.2f} €.') + + +# Mehrere Ausdrücke in einem f-String +eingabe = 5 +print(f'Das Quadrat von {eingabe} ist {eingabe*eingabe:.2f}') + + +print(f'{3+4} {3*4} {3-4} {3/4} {3//4} {3%4} {3**4}') \ No newline at end of file diff --git a/02_Console/lessons/06_average.py b/02_Console/lessons/06_average.py new file mode 100644 index 0000000..507d3dc --- /dev/null +++ b/02_Console/lessons/06_average.py @@ -0,0 +1,17 @@ +# Berechnen des Durchschnitts einer Liste von Zahlen + +sum = 0.0 +count = 0 + +while True: + input_text = input("Geben Sie eine Zahl ein (0 bricht ab): ") + number = float(input_text) + if number == 0: + break + sum = sum + number + count = count + 1 + +if count == 0: + print("Keine Zahlen eingegeben") +else: + print(f"Der Durchschnitt ist {sum / count:.3f}") diff --git a/02_Console/lessons/07_return.py b/02_Console/lessons/07_return.py new file mode 100644 index 0000000..cad8e4d --- /dev/null +++ b/02_Console/lessons/07_return.py @@ -0,0 +1,11 @@ +# Funktion mit einer Rückgabe +def mal_drei(x): + return x * 3 + +print(mal_drei(5)) + + +# Jede Funktion in Python gibt einen Wert zurück. +r = print("Hello World!") +print(r) +print(type(r)) diff --git a/02_Console/lessons/08_magic_numbers.py b/02_Console/lessons/08_magic_numbers.py new file mode 100644 index 0000000..72903ec --- /dev/null +++ b/02_Console/lessons/08_magic_numbers.py @@ -0,0 +1,6 @@ +# Berechnung einer Kreisfläche + +PI = 3.14159 +radius = float(input("Bitte den Radius eingeben: ")) +area = PI * radius * radius +print("Der Flächeninhalt beträgt", area) diff --git a/02_Console/projects/big_money.py b/02_Console/projects/big_money.py new file mode 100644 index 0000000..d393fd1 --- /dev/null +++ b/02_Console/projects/big_money.py @@ -0,0 +1,24 @@ +# Ausgabe von Geldbeträgen in einer speziellen Formatierung +def main(): + for money in [100123345, 100123305, 5]: + print(format_money_string(money)) + + +# Funktion zur Formatierung von Geldbeträgen +def format_money_string(cents): + euros = cents // 100 + cents = cents % 100 + thousands = euros // 1000 + euros = euros % 1000 + millions = thousands // 1000 + thousands = thousands % 1000 + if millions > 0: + return f'{millions}.{thousands:03}.{euros:03},{cents:02}' + elif thousands > 0: + return f'{thousands}.{euros:03},{cents:02}' + else: + return f'{euros},{cents:02}' + + +# Start des Programms +main() diff --git a/02_Console/projects/calculator.py b/02_Console/projects/calculator.py new file mode 100644 index 0000000..ebe1bc1 --- /dev/null +++ b/02_Console/projects/calculator.py @@ -0,0 +1,9 @@ +# Rechenmaschine + +while True: + repeat = input("Soll noch eine Addition durchgeführt werden? (j/n): ") + if repeat == 'n': + break + zahl1 = float(input("Erste Zahl: ")) + zahl2 = float(input("Zweite Zahl: ")) + print(f"Die Summe beträgt {zahl1 + zahl2}") \ No newline at end of file diff --git a/02_Console/projects/countdown.py b/02_Console/projects/countdown.py new file mode 100644 index 0000000..a3c4c74 --- /dev/null +++ b/02_Console/projects/countdown.py @@ -0,0 +1,6 @@ +# Ausgabe eines Countdowns von 10 bis 0 +start = 10 +while start >= 0: + print(start) + start -= 1 + diff --git a/02_Console/projects/even_numbers.py b/02_Console/projects/even_numbers.py new file mode 100644 index 0000000..f81c379 --- /dev/null +++ b/02_Console/projects/even_numbers.py @@ -0,0 +1,13 @@ +# Ermittlung, ob das Farbband gewechselt werden muss +# (immer an geraden Tagen) + +# Eingabe +day = int(input('Welchen Kalendartag haben wir heute (1-31): ')) + +# Berechnung +if day % 2 == 0: + print('Wechsel das Farbband!') +else: + print('Alles gut!') + + diff --git a/02_Console/projects/money.py b/02_Console/projects/money.py new file mode 100644 index 0000000..6a102a9 --- /dev/null +++ b/02_Console/projects/money.py @@ -0,0 +1,15 @@ +# Ausgabe von Geldbeträgen in einer speziellen Formatierung +def main(): + for money in [120, 90, 100, 102, 2]: + print(format_money_string(money)) + + +# Funktion zur Formatierung von Geldbeträgen +def format_money_string(money): + euros = money // 100 + cents = money % 100 + return f'{euros},{cents:02}' + + +# Start des Programms +main() diff --git a/02_Console/projects/squares.py b/02_Console/projects/squares.py new file mode 100644 index 0000000..8101616 --- /dev/null +++ b/02_Console/projects/squares.py @@ -0,0 +1,8 @@ +# Ausgabe von Quadratzahlen + +MAX_NUM = 10 + +i = 0 +while i <= MAX_NUM: + print(f"{i} quadriert ist {i*i}") + i += 1 \ No newline at end of file diff --git a/02_Console/projects/temperatur.py b/02_Console/projects/temperatur.py new file mode 100644 index 0000000..1f42b64 --- /dev/null +++ b/02_Console/projects/temperatur.py @@ -0,0 +1,6 @@ +# Temperaturumrechnung + +f = float(input("Geben Sie die Temperatur in Fahrenheit ein: ")) +c = (f - 32) * 5 / 9 + +print(f"Die Temperatur in Celsius beträgt {c:.1f}") \ No newline at end of file diff --git a/02_Console/projects/what_time.py b/02_Console/projects/what_time.py new file mode 100644 index 0000000..62df7d7 --- /dev/null +++ b/02_Console/projects/what_time.py @@ -0,0 +1,9 @@ +# Berechnung der Uhrzeit für Menschen + +sec = int(input("Wie viele Sekunden sind seit Mitternacht vergangen? ")) + +hours = sec // 3600 +minutes = (sec % 3600) // 60 +seconds = sec % 60 + +print (f"Es ist {hours:02}:{minutes:02}:{seconds:02}.") \ No newline at end of file diff --git a/03_Games/assignments/analog_clock.py b/03_Games/assignments/analog_clock.py new file mode 100644 index 0000000..def71a1 --- /dev/null +++ b/03_Games/assignments/analog_clock.py @@ -0,0 +1,118 @@ +import pygame +import math +import datetime + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +DISTANCE = 10 +CIRCLE_RADIUS = HEIGHT//2 - DISTANCE +NUMBER_RADIUS = CIRCLE_RADIUS * 0.9 +HOURS_LENGTH = NUMBER_RADIUS * 0.6 +MINUTES_LENGTH = NUMBER_RADIUS * 0.8 +SECONDS_LENGTH = NUMBER_RADIUS * 0.9 +FONT_SIZE = 20 +FONT_NAME = None # None = default font +BLACK = (0, 0, 0) +WHITE = (255, 255, 255) +RED = (255, 0, 0) +FPS = 1 + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + global clock + global font + pygame.init() + clock = pygame.time.Clock() + font = pygame.font.Font(FONT_NAME, FONT_SIZE) + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + clock.tick(FPS) + + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Event-Behandlung +def event_handling(): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + return True + + +# Aktualisierung des Spiels +def update_game(): + global hours_angle + global minuten_angle + global seconds_angle + now = datetime.datetime.now() + hours = now.hour % 12 + minutes = now.minute + seconds = now.second + hours_angle = (hours * 60 + minutes) / 720 * 2 * math.pi + minuten_angle = minutes / 60 * 2 * math.pi + seconds_angle = seconds / 60 * 2 * math.pi + return True + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(BLACK) + position = (WIDTH//2, HEIGHT//2) + draw_dial(screen, position) + draw_hands(screen, position) + pygame.display.flip() + + +# Zeichnen des Ziffernblatts +def draw_dial(screen, position): + global font + # Rand + pygame.draw.circle(screen, WHITE, position, CIRCLE_RADIUS, 1) + # Ziffern + for i in range(12): + angle = i * (2 * math.pi) / 12 + y = math.cos(angle) * NUMBER_RADIUS * -1 + x = math.sin(angle) * NUMBER_RADIUS + text = font.render(str(i or 12), True, WHITE) + text_rect = text.get_rect(center=(WIDTH // 2 + x, HEIGHT // 2 + y)) + screen.blit(text, text_rect) + + +# Zeichnen der Zeiger +def draw_hands(screen, position): + end_pos_hours = (position[0] + math.sin(hours_angle) * HOURS_LENGTH, position[1] + math.cos(hours_angle) * HOURS_LENGTH * -1) + pygame.draw.line(screen, WHITE, position, end_pos_hours, 4) + + end_pos_minutes = (position[0] + math.sin(minuten_angle) * MINUTES_LENGTH, position[1] + math.cos(minuten_angle) * MINUTES_LENGTH * -1) + pygame.draw.line(screen, WHITE, position, end_pos_minutes, 2) + + end_pos_seconds = (position[0] + math.sin(seconds_angle) * SECONDS_LENGTH, position[1] + math.cos(seconds_angle) * SECONDS_LENGTH * -1) + pygame.draw.line(screen, RED, position, end_pos_seconds, 1) + + +# Start des Programms +main() + + diff --git a/03_Games/assignments/billard.py b/03_Games/assignments/billard.py new file mode 100644 index 0000000..a7cf900 --- /dev/null +++ b/03_Games/assignments/billard.py @@ -0,0 +1,112 @@ +import pygame + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +FPS = 30 +BLACK = (0, 0, 0) +GREEN = (0, 255, 0) +RED = (255, 0, 0) +CIRCLE_RADIUS = 5 +MAX_SPEED = 30 +FRICTION = 0.97 + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + global position + global speed + global clock + global pushed + position = (WIDTH // 2, HEIGHT // 2) + speed = (0.0, 0.0) + clock = pygame.time.Clock() + pushed = False + pygame.init() + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + clock.tick(FPS) + + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Event-Behandlung +def event_handling(): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + if not pushed and event.type == pygame.MOUSEBUTTONUP: + button_pushed() + return True + + +def button_pushed(): + global speed, pushed + mouse_pos = pygame.mouse.get_pos() + delta_x = mouse_pos[0] - position[0] + delta_y = mouse_pos[1] - position[1] + speed = (MAX_SPEED * delta_x / WIDTH, MAX_SPEED * delta_y / HEIGHT) + pushed = True + + +# Aktualisierung des Spiels +def update_game(): + if pushed: + calc_new_position() + calc_new_speed() + return True + + +def calc_new_speed(): + global speed, pushed + speed = (speed[0] * FRICTION, speed[1] * FRICTION) + if abs(speed[0]) < 0.1 and abs(speed[1]) < 0.1: + pushed = False + speed = (0.0, 0.0) + + +def calc_new_position(): + global speed, position + position = (position[0] + speed[0], position[1] + speed[1]) + if position[0] < CIRCLE_RADIUS or position[0] + CIRCLE_RADIUS >= WIDTH: + speed = (-speed[0], speed[1]) + position = (position[0] + speed[0], position[1] + speed[1]) + if position[1] < CIRCLE_RADIUS or position[1] + CIRCLE_RADIUS >= HEIGHT: + speed = (speed[0], -speed[1]) + position = (position[0] + speed[0], position[1] + speed[1]) + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(GREEN) + pygame.draw.circle(screen, BLACK, position, CIRCLE_RADIUS) + if not pushed: + mouse_pos = pygame.mouse.get_pos() + if mouse_pos[0] > 0 and mouse_pos[0] < WIDTH-1 and mouse_pos[1] > 0 and mouse_pos[1] < HEIGHT-1: + pygame.draw.line(screen, RED, position, mouse_pos, 2) + pygame.display.flip() + + + +# Start des Programms +main() diff --git a/03_Games/assignments/connect_the_clicks.py b/03_Games/assignments/connect_the_clicks.py new file mode 100644 index 0000000..2a14406 --- /dev/null +++ b/03_Games/assignments/connect_the_clicks.py @@ -0,0 +1,70 @@ +import pygame + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +BLACK = (0, 0, 0) +RED = (255, 0, 0) + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + global clicks + clicks = [] + pygame.init() + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Event-Behandlung +def event_handling(): + global clicks + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + if event.type == pygame.MOUSEBUTTONUP: + pos = pygame.mouse.get_pos() + clicks.append(pos) + return True + + +# Aktualisierung des Spiels +def update_game(): + return True + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(BLACK) + last_pos = None + for pos in clicks: + if last_pos is not None: + pygame.draw.line(screen, RED, last_pos, pos, 2) + last_pos = pos + pygame.display.flip() + + +# Start des Programms +main() \ No newline at end of file diff --git a/03_Games/lessons/01_sequenzen.py b/03_Games/lessons/01_sequenzen.py new file mode 100644 index 0000000..ffbda01 --- /dev/null +++ b/03_Games/lessons/01_sequenzen.py @@ -0,0 +1,39 @@ +# Listen-Literal mit 3 Elementen +l = [3, "Hello", 5.0] +print(l) + +# Zugriff auf das erste Element der Liste +l = [3, "Hello", 5.0] +print(f"Das erste Element der Liste ist {l[0]}") + +# Ändern des zweiten Elements der Liste +l = [3, "Hello", 5.0] +l[1] = "Hallo" +print(f"Das zweite Element der Liste ist jetzt {l[1]}") + +# Iteration über die Elemente einer Liste +liste = [3, "Hello", 5.0] +for element in liste: + print(element) + +# Iteration über ein Intervall +for zahl in range(3): + print(zahl) + +# Typen von Listen und Intervallen +print(type([3, "Hello", 5.0])) +print(type(range(3))) + +# Iteration über die Zeichen eines Strings +for c in "Hallo": + print(c) + +# Konvertierung eines Strings in eine Liste +print(list("Hallo")) + +# Tupel-Literal mit 3 Elementen, Zugriff, Iteration +tupel = (3, "Hello", 5.0) +print(tupel[2]) +for element in tupel: + print(element) + diff --git a/03_Games/lessons/02_black_window_game.py b/03_Games/lessons/02_black_window_game.py new file mode 100644 index 0000000..c571752 --- /dev/null +++ b/03_Games/lessons/02_black_window_game.py @@ -0,0 +1,58 @@ +import pygame + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +BLACK = (0, 0, 0) + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + pygame.init() + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Behandlung der Events +def event_handling(): + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + return True + + +# Aktualisierung des Spiels +def update_game(): + return True + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(BLACK) + pygame.display.flip() + + +# Start des Programms +main() diff --git a/03_Games/lessons/03_scope.py b/03_Games/lessons/03_scope.py new file mode 100644 index 0000000..5e50d80 --- /dev/null +++ b/03_Games/lessons/03_scope.py @@ -0,0 +1,34 @@ +# Gültigkeitsbereich von Variablen + + +# 1. Beispiel: Änderungen an lokalen Variablen beeinflussen globale Variablen nicht + +a = 1 # Globale Variable + +def f(): + a = 2 # Lokale Variable + +f() +print(a) + + +# 2. Beispiel: Das Verdecken der globalen Variable durch eine lokale Variable +# kann durch die Verwendung des Schlüsselworts 'global' verhindert werden + +def f(): + global a + a = 2 + +f() +print(a) + + +# 3. Beispiel: Eine globale Variable kann in einer Funktion erstellt werden + +def f(): + global b + b = 3 + +f() +print(b) + diff --git a/03_Games/lessons/04_walker.py b/03_Games/lessons/04_walker.py new file mode 100644 index 0000000..67f74f1 --- /dev/null +++ b/03_Games/lessons/04_walker.py @@ -0,0 +1,72 @@ +import pygame + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +BLACK = (0, 0, 0) +RED = (255, 0, 0) +CIRCLE_RADIUS = 10 + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + global position + position = (WIDTH // 2, HEIGHT // 2) + pygame.init() + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Behandlung der Events +def event_handling(): + global position + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + if event.type == pygame.KEYDOWN: + if event.key == pygame.K_UP: + position = (position[0], position[1] - 1) + if event.key == pygame.K_DOWN: + position = (position[0], position[1] + 1) + if event.key == pygame.K_LEFT: + position = (position[0] - 1, position[1]) + if event.key == pygame.K_RIGHT: + position = (position[0] + 1, position[1]) + return True + + +# Aktualisierung des Spiels +def update_game(): + return True + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(BLACK) + pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS) + pygame.display.flip() + + +# Start des Programms +main() diff --git a/03_Games/lessons/05_walker_improved.py b/03_Games/lessons/05_walker_improved.py new file mode 100644 index 0000000..770c247 --- /dev/null +++ b/03_Games/lessons/05_walker_improved.py @@ -0,0 +1,96 @@ +import pygame + +# Festlegung der Konstanten +WIDTH = 320 +HEIGHT = 240 +SIZE = (WIDTH, HEIGHT) +FPS = 30 +BLACK = (0, 0, 0) +RED = (255, 0, 0) +CIRCLE_RADIUS = 10 +KEY_UP = 0 +KEY_DOWN = 1 +KEY_LEFT = 2 +KEY_RIGHT = 3 +SPEED = 3 + + +# Hauptfunktion mit Standardstruktur eines Pygame +def main(): + screen = init_game() + game_loop(screen) + exit_game() + + +# Initialisierung von Pygame +def init_game(): + global position + global keys_pressed + global clock + position = (WIDTH // 2, HEIGHT // 2) + keys_pressed = [False, False, False, False] + clock = pygame.time.Clock() + pygame.init() + return pygame.display.set_mode(SIZE) + + +# Game-Loop +def game_loop(screen): + while True: + if event_handling() == False: + break + if update_game() == False: + break + draw_game(screen) + clock.tick(FPS) + + +# Beenden von Pygame +def exit_game(): + pygame.quit() + + +# Behandlung der Events +def event_handling(): + global keys_pressed + for event in pygame.event.get(): + if event.type == pygame.QUIT: + return False + + if event.type == pygame.KEYDOWN or event.type == pygame.KEYUP: + new_value = (event.type == pygame.KEYDOWN) + if event.key == pygame.K_UP: + keys_pressed[KEY_UP] = new_value + if event.key == pygame.K_DOWN: + keys_pressed[KEY_DOWN] = new_value + if event.key == pygame.K_LEFT: + keys_pressed[KEY_LEFT] = new_value + if event.key == pygame.K_RIGHT: + keys_pressed[KEY_RIGHT] = new_value + + return True + + +# Aktualisierung des Spiels +def update_game(): + global position + if keys_pressed[KEY_UP]: + position = (position[0], position[1] - SPEED) + if keys_pressed[KEY_DOWN]: + position = (position[0], position[1] + SPEED) + if keys_pressed[KEY_LEFT]: + position = (position[0] - SPEED, position[1]) + if keys_pressed[KEY_RIGHT]: + position = (position[0] + SPEED, position[1]) + return True + + +# Zeichnen des Spiels +def draw_game(screen): + screen.fill(BLACK) + pygame.draw.circle(screen, RED, position, CIRCLE_RADIUS) + pygame.display.flip() + + +# Start des Programms +main()