9. Zeichnen mit Funktionen
We learned how to draw in How to Draw with Your Computer. In Übung 2: Zeichne ein Bild we applied that to create our own drawing. We learned how to create functions in Funktionen erstellen. In this chapter, we’ll combine all that knowledge to create our own drawing functions.
We’ll do that by expanding on the code you wrote for Übung 2: Zeichne ein Bild.
To show you how, I’m going to start with a similar program and convert it to using functions.
Hier ist erst einmal das originale Programm:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) arcade.start_render() # Draw the ground arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) # Draw a snow person # Snow arcade.draw_circle_filled(300, 200, 60, arcade.color.WHITE) arcade.draw_circle_filled(300, 280, 50, arcade.color.WHITE) arcade.draw_circle_filled(300, 340, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(285, 350, 5, arcade.color.BLACK) arcade.draw_circle_filled(315, 350, 5, arcade.color.BLACK) # Finish and run arcade.finish_render() arcade.run() |
Ok, it isn’t very fancy, but that will make this process easier to follow.
9.1. Erstelle die Funktion main
After copying the code into Lab 3, we’ll create a main()
function.
Put everything in it, and call the main
function.
Are those too many lines to indent? You can indent groups of lines by selecting them, and then hitting „tab“. If you want to un-indent a group of lines, hit shift-tab.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) self.clear() # Draw the ground arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) # Draw a snow person # Snow arcade.draw_circle_filled(300, 200, 60, arcade.color.WHITE) arcade.draw_circle_filled(300, 280, 50, arcade.color.WHITE) arcade.draw_circle_filled(300, 340, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(285, 350, 5, arcade.color.BLACK) arcade.draw_circle_filled(315, 350, 5, arcade.color.BLACK) # Finish and run arcade.finish_render() arcade.run() # Call the main function to get the program started. main() |
After this, run your program and make sure it still works before proceeding. If it doesn’t work, stop and get help. Continuing will only make the problem harder.
9.2. Erstelle die Zeichenfunktion
Next, pick an item to move to a function. Start with an easy one if you have it.
9.2.1. Grass Function
I chose grass to start withbecause it was only one line of code, and I wasn’t going to ever try to position it with an x, y coordinate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) self.clear() draw_grass() # Draw a snow person # Snow arcade.draw_circle_filled(300, 200, 60, arcade.color.WHITE) arcade.draw_circle_filled(300, 280, 50, arcade.color.WHITE) arcade.draw_circle_filled(300, 340, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(285, 350, 5, arcade.color.BLACK) arcade.draw_circle_filled(315, 350, 5, arcade.color.BLACK) # Finish and run arcade.finish_render() arcade.run() # Call the main function to get the program started. main() |
Test, and make sure it is working.
9.2.2. Snow Person Function
Now let’s take the more complex snow person and put it in a function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) def draw_snow_person(): """ Draw a snow person """ # Snow arcade.draw_circle_filled(300, 200, 60, arcade.color.WHITE) arcade.draw_circle_filled(300, 280, 50, arcade.color.WHITE) arcade.draw_circle_filled(300, 340, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(285, 350, 5, arcade.color.BLACK) arcade.draw_circle_filled(315, 350, 5, arcade.color.BLACK) def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) self.clear() draw_grass() draw_snow_person() # Finish and run arcade.finish_render() arcade.run() # Call the main function to get the program started. main() |
Aber dies zeichnet den Schneemann nur an einer bestimmten Stelle. Ich will viele Schneemänner an beliebigen Orten zeichnen!
Lass und x und y hinzufügen, um dieses Ziel zu erreichen:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) def draw_snow_person(x, y): """ Draw a snow person """ # Draw a point at x, y for reference arcade.draw_point(x, y, arcade.color.RED, 5) # Snow arcade.draw_circle_filled(300 + x, 200 + y, 60, arcade.color.WHITE) arcade.draw_circle_filled(300 + x, 280 + y, 50, arcade.color.WHITE) arcade.draw_circle_filled(300 + x, 340 + y, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(285 + x, 350 + y, 5, arcade.color.BLACK) arcade.draw_circle_filled(315 + x, 350 + y, 5, arcade.color.BLACK) def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) self.clear() draw_grass() draw_snow_person(50, 50) # Finish and run arcade.finish_render() arcade.run() # Call the main function to get the program started. main() |
Aber das ist nicht perfekt. Wie du bemerken wirst, habe ich einen Punkt zu x und y addiert. Der Schneemann wird abseits von diesem Punkt gezeichnet, weil ich ursprünglich versucht habe ihn auf 0, 0 bezogen zu zeichnen. Ich muss den Schneemann auf dem Punkt neu zentrieren.
Wir müssen die Form neu auf den Punkt, an dem wir zeichnen, zentrieren. Üblicherweise wirst du von allen x- und y-Werten den gleichen Betrag abziehen.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) def draw_snow_person(x, y): """ Draw a snow person """ # Draw a point at x, y for reference arcade.draw_point(x, y, arcade.color.RED, 5) # Snow arcade.draw_circle_filled(x, 60 + y, 60, arcade.color.WHITE) arcade.draw_circle_filled(x, 140 + y, 50, arcade.color.WHITE) arcade.draw_circle_filled(x, 200 + y, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(x - 15, 210 + y, 5, arcade.color.BLACK) arcade.draw_circle_filled(x + 15, 210 + y, 5, arcade.color.BLACK) def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) self.clear() draw_grass() draw_snow_person(150, 140) draw_snow_person(450, 180) # Finish and run arcade.finish_render() arcade.run() # Call the main function to get the program started. main() |
9.3. Wie man eine Zeichenfunktion animiert
Wir können unsere erste Zeichnung animieren, wenn wir wollen. Hier sind die nötigen Schritte.
9.3.1. Erstelle eine on_draw
-Methode
Bis jetzt zeichnet unser Programm unser Bild nur einmal. Wir müssen den Zeichen-Code von unserer main
- in eine on_draw
-Funktion verschieben. Dann müssen wir dem Rechner sagen, das immer und immer wieder zu zeichnen.
Von unserem letzten Beispiel fortgeführt, wird unser Programm so aussehen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT / 3, arcade.color.AIR_SUPERIORITY_BLUE) def draw_snow_person(x, y): """ Draw a snow person """ # Draw a point at x, y for reference arcade.draw_point(x, y, arcade.color.RED, 5) # Snow arcade.draw_circle_filled(x, 60 + y, 60, arcade.color.WHITE) arcade.draw_circle_filled(x, 140 + y, 50, arcade.color.WHITE) arcade.draw_circle_filled(x, 200 + y, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(x - 15, 210 + y, 5, arcade.color.BLACK) arcade.draw_circle_filled(x + 15, 210 + y, 5, arcade.color.BLACK) def on_draw(delta_time): """ Draw everything """ draw_grass() draw_snow_person(150, 140) draw_snow_person(450, 180) def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) # Call on_draw every 60th of a second. arcade.schedule(on_draw, 1/60) arcade.run() # Call the main function to get the program started. main() |
Mach dies mit deinem eigenen Programm. Es wird sich nichts bewegen, aber es sollte immer noch laufen.
9.3.2. Füge Variablen hinzu, die festlegen wo wir unser Element zeichnen
Als nächstes erstellen wir eine Variable innerhalb der on_draw
-Funktion. Dies Variable wird unseren x-Wert enthalten. Jedes Mal, wenn wir on_draw
aufrufen, ändern wir x, so dass er sich nach rechts bewegt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | import arcade SCREEN_WIDTH = 800 SCREEN_HEIGHT = 600 def draw_grass(): """ Draw the ground """ arcade.draw_lrbt_rectangle_filled(0, SCREEN_WIDTH, SCREEN_HEIGHT / 3, 0, arcade.color.AIR_SUPERIORITY_BLUE) def draw_snow_person(x, y): """ Draw a snow person """ # Draw a point at x, y for reference arcade.draw_point(x, y, arcade.color.RED, 5) # Snow arcade.draw_circle_filled(x, 60 + y, 60, arcade.color.WHITE) arcade.draw_circle_filled(x, 140 + y, 50, arcade.color.WHITE) arcade.draw_circle_filled(x, 200 + y, 40, arcade.color.WHITE) # Eyes arcade.draw_circle_filled(x - 15, 210 + y, 5, arcade.color.BLACK) arcade.draw_circle_filled(x + 15, 210 + y, 5, arcade.color.BLACK) def on_draw(delta_time): """ Draw everything """ self.clear() draw_grass() draw_snow_person(on_draw.snow_person1_x, 140) draw_snow_person(450, 180) # Add one to the x value, making the snow person move right # Negative numbers move left. Larger numbers move faster. on_draw.snow_person1_x += 1 # Create a value that our on_draw.snow_person1_x will start at. on_draw.snow_person1_x = 150 def main(): arcade.open_window(SCREEN_WIDTH, SCREEN_HEIGHT, "Drawing with Functions") arcade.set_background_color(arcade.color.DARK_BLUE) # Call on_draw every 60th of a second. arcade.schedule(on_draw, 1/60) arcade.run() # Call the main function to get the program started. main() |
Für weitere Information sie dir das Bouncing Rectangle Example an.