# Default ignored files | |||||
/shelf/ | |||||
/workspace.xml | |||||
# Editor-based HTTP Client requests | |||||
/httpRequests/ | |||||
# Datasource local storage ignored files | |||||
/dataSources/ | |||||
/dataSources.local.xml |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<module type="PYTHON_MODULE" version="4"> | |||||
<component name="NewModuleRootManager"> | |||||
<content url="file://$MODULE_DIR$"> | |||||
<excludeFolder url="file://$MODULE_DIR$/.venv" /> | |||||
</content> | |||||
<orderEntry type="inheritedJdk" /> | |||||
<orderEntry type="sourceFolder" forTests="false" /> | |||||
</component> | |||||
</module> |
<component name="InspectionProjectProfileManager"> | |||||
<profile version="1.0"> | |||||
<option name="myName" value="Project Default" /> | |||||
<inspection_tool class="PyPep8Inspection" enabled="true" level="WEAK WARNING" enabled_by_default="true"> | |||||
<option name="ignoredErrors"> | |||||
<list> | |||||
<option value="E302" /> | |||||
<option value="E111" /> | |||||
</list> | |||||
</option> | |||||
</inspection_tool> | |||||
</profile> | |||||
</component> |
<component name="InspectionProjectProfileManager"> | |||||
<settings> | |||||
<option name="USE_PROJECT_PROFILE" value="false" /> | |||||
<version value="1.0" /> | |||||
</settings> | |||||
</component> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="Black"> | |||||
<option name="sdkName" value="Python 3.12 (Solutions)" /> | |||||
</component> | |||||
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.12 (Solutions)" project-jdk-type="Python SDK" /> | |||||
</project> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="ProjectModuleManager"> | |||||
<modules> | |||||
<module fileurl="file://$PROJECT_DIR$/.idea/Solutions.iml" filepath="$PROJECT_DIR$/.idea/Solutions.iml" /> | |||||
</modules> | |||||
</component> | |||||
</project> |
<?xml version="1.0" encoding="UTF-8"?> | |||||
<project version="4"> | |||||
<component name="VcsDirectoryMappings"> | |||||
<mapping directory="$PROJECT_DIR$" vcs="Git" /> | |||||
</component> | |||||
</project> |
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() |
def take_if_there(): | |||||
if object_here(): | |||||
take() | |||||
for _ in range(9): | |||||
take_if_there() | |||||
move() | |||||
take_if_there() |
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() |
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. | |||||
################################################################ |
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. | |||||
################################################################ |
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() |
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() |
while front_is_clear(): | |||||
move() | |||||
################################################################ | |||||
# WARNING: Do not change this comment. | |||||
# Library Code is below. | |||||
################################################################ | |||||
def turn_around(): | |||||
turn_left() | |||||
turn_left() |
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() |
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() | |||||
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() |
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() |
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() | |||||
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() |
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() |
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() | |||||
# Sophia läuft 100 Schritte nach vorne | |||||
from turtle import Turtle | |||||
sophia = Turtle() | |||||
sophia.forward(100) | |||||
sophia.screen.mainloop() | |||||
# 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() |
# 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() |
# 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() |
# 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() |
# 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() |
# 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() |
# 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() |
from turtle import Turtle | |||||
sophia = Turtle() | |||||
sophia.write('Hello, World!', align='center', font=('Arial', 24, 'bold')) | |||||
sophia.screen.mainloop() |
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() |
# 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() |
# 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() |
# 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() |
# 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() |
# 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() |
# 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() | |||||
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() |
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() |
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() |
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() |
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() |
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() |
# This is a comment | |||||
print('Hello World!') |
for _ in range(100): | |||||
# print('Ich darf Sophia nicht umdrehen.') | |||||
print('Ich darf Sophia nicht umdrehen.', end=' ') |
# 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) |
# 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)) |
# 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}') |
# 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}") |
# 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)) |
# 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) |
# 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() |
# 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}") |
# Ausgabe eines Countdowns von 10 bis 0 | |||||
start = 10 | |||||
while start >= 0: | |||||
print(start) | |||||
start -= 1 | |||||
# 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!') | |||||
# 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() |
# Ausgabe von Quadratzahlen | |||||
MAX_NUM = 10 | |||||
i = 0 | |||||
while i <= MAX_NUM: | |||||
print(f"{i} quadriert ist {i*i}") | |||||
i += 1 |
# 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}") |
# 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}.") |
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() | |||||
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() |
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() |
# 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) | |||||
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() |
# 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) | |||||
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() |
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() |