Skip to content

Commit 3a68cc4

Browse files
authored
Update readme.md
1 parent 9a3110d commit 3a68cc4

File tree

1 file changed

+83
-2
lines changed
  • Python Projects/Intermediate Games/SketchBook

1 file changed

+83
-2
lines changed
Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,88 @@
11
# Live Demo
2+
https://user-images.githubusercontent.com/67270567/226549542-cd786152-fcb2-43f9-ba1d-6d6994775f33.mp4
3+
Press the Play button to See the Output
24

5+
# Line by Line Explanation of the Code
6+
```python
7+
from turtle import Turtle, Screen
8+
```
9+
1. **Importing Libraries**
310

4-
https://user-images.githubusercontent.com/67270567/226549542-cd786152-fcb2-43f9-ba1d-6d6994775f33.mp4
11+
The code starts by importing the necessary libraries `Turtle` and `Screen` from the turtle module. The `Turtle` class is used to create a turtle object that can move around the screen, while the `Screen` class is used to create the window that the turtle will be drawn on.
512

13+
```python
14+
orbit = Turtle()
15+
window = Screen()
16+
```
17+
2. **Creating Objects**
618

7-
Press the Play button to See the Output
19+
Two objects are created, `orbit` and `window`, using the `Turtle()` and `Screen()` functions respectively. The orbit object is assigned to the `turtle`, and the window object is assigned to the `window`.
20+
21+
```python
22+
orbit.pensize(10)
23+
orbit.pencolor("red")
24+
orbit.shape("turtle")
25+
```
26+
3. **Setting Up the Turtle**
27+
28+
Several properties of the turtle object are set using the following lines of code:
29+
30+
- `orbit.pensize(10)` sets the width of the turtle's pen to 10 pixels.
31+
- `orbit.pencolor("red")` sets the color of the turtle's pen to red.
32+
- `orbit.shape("turtle")` sets the shape of the turtle to the default turtle shape.
33+
34+
```python
35+
def move_forward():
36+
orbit.forward(10)
37+
38+
def move_backward():
39+
orbit.backward(10)
40+
41+
def move_left():
42+
new_heading = orbit.heading()+10
43+
orbit.setheading(new_heading)
44+
45+
def move_right():
46+
new_heading = orbit.heading() - 10
47+
orbit.setheading(new_heading)
48+
```
49+
4. **Defining Movement Functions**
50+
51+
Four functions are defined to control the turtle's movement:
52+
53+
- `move_forward()` moves the turtle forward by 10 pixels.
54+
- `move_backward()` moves the turtle backward by 10 pixels.
55+
- `move_left()` turns the turtle left by 10 degrees.
56+
- `move_right()` turns the turtle right by 10 degrees.
57+
58+
```python
59+
def clear():
60+
orbit.clear()
61+
orbit.penup()
62+
orbit.home()
63+
orbit.pendown()
64+
```
65+
5. **Defining Clear Function**
66+
67+
Another function is defined to clear the screen and reset the turtle's position to the center of the screen. The clear() function clears the turtle's trail, lifts the turtle's pen up, moves the turtle to the center of the screen, and puts the pen back down.
68+
69+
```python
70+
#Event Listener
71+
window.listen()
72+
window.onkey(move_forward,"w")
73+
window.onkey(move_backward,"s")
74+
window.onkey(move_left,"a")
75+
window.onkey(move_right,"d")
76+
window.onkey(clear,"c")
77+
window.exitonclick()
78+
```
79+
6. **Setting up Event Listener**
80+
81+
The code sets up an event listener using the `listen()` method on the window object. This allows the program to respond to user input. Four key bindings are defined using the `onkey()` method:
82+
83+
84+
- When the `w` key is pressed, the `move_forward()` function is called.
85+
- When the `s` key is pressed, the `move_backward()` function is called.
86+
- When the `a` key is pressed, the `move_left()` function is called.
87+
- When the `d` key is pressed, the `move_right()` function is called.
88+
- When the `c` key is pressed, the `clear()` function is called.

0 commit comments

Comments
 (0)