Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added documentation for Turtle module with code example #39

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions Beginner_Projects/Turtle/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
**Turtle Graphics**

## Overview
The Turtle module in Python is a beginner-friendly graphics library that allows users to create complex shapes, patterns, and designs by giving simple commands to a "turtle" on the screen. It's a great way to learn the basics of programming, while making coding visually engaging and fun.

## Installation
The Turtle module comes pre-installed with Python, so there's no need for additional installations. However, you can ensure it’s available by running:

pip install PythonTurtle


## Features:
1) Direction:
Turtle can move a specified number of steps forward and backward.
- Move forward by x steps:
```sh
forward(20)
```
- Move backward by x steps:
```sh
backward(20)
```
2) Turn:
Turtle can turn by given degree in either direction (right or left).
```sh
right(45)
```
3) Shape:
You can decide the shape of turtle using shape() function.
Available shapes are: turtle, circle, square, triangle, arrow, classic.
```sh
shape("arrow")
```
4) Color:
You can control the color of turtle's drawing and appearance.
- To set drawing color:
```sh
color('red')
```
- To set pen color:
```sh
pencolor('green')
```
- To set background color:
```sh
bgcolor('black')
```
5) Speed:
You can control the turtle's speed with speed().
```sh
speed(10)
```
6) Turtle position:
- Send turtle back to starting point.
```sh
home()
```
Mainly used when turtle go off-screen.
- Get turtle's current position co-ordinates.
```sh
pos()
```
- Clear all previous drawing, i.e. to get a clear screen.
```sh
clearscreen()
```
7) Pen Control:
You can lift the pen up and down to control when the turtle draws.
- Lift the pen (Stop drawing):
When user want to move turtle without drawing anything.
```sh
up()
```
- Put pen down (Start drawing):
When user want to start drawing again.
```sh
down()
```
8) Width:
You can set the width of the turtle’s drawing line with width(x), where x is an integer value.
```sh
width(x)
```


## General Turtle Graphics Code Syntax

1) Import the Turtle Module.
2) Set up the screen.
3) Create a function to draw desired shape or pattern.
4) Take user input (if required) and pass it to the function.
5) Run the program.

Don’t forget to use **turtle.done()** to keep the window open after the drawing is complete.

## Example: Spiral Pattern

For refernece, you can check out this [Spiral Patten example](turtle_spiral.py) created using turtle module.

## Further Reading
For more detailed usage of the Turtle module, visit the official [Python documentation](https://docs.python.org/3/library/turtle.html).


22 changes: 22 additions & 0 deletions Beginner_Projects/Turtle/rainbow_spiral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import turtle

# defining colors
colors = ['red', 'yellow', 'green', 'purple', 'blue', 'orange']

# setup turtle pen
t= turtle.Pen()

# changes the speed of the turtle
t.speed(10)

# changes the background color
turtle.bgcolor("black")

# make spiral_web
for x in range(200):
t.pencolor(colors[x%6]) # setting color
t.width(x/100 + 1) # setting width
t.forward(x) # moving forward
t.left(59) # moving left

turtle.done()
30 changes: 30 additions & 0 deletions Beginner_Projects/Turtle/turtle_spiral.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import turtle as t

# Setup the display window where turtle will draw
window = t.Screen()
# Configure Screen size
window.setup(800,800)
#set the speed of turtle
t.speed(50)
# Set background color
t.bgcolor('black')

# Define a function to draw spiral with total steps and list of colors
def spiral(steps,color_list):
for step in range(steps):
for c in color_list:
t.width(step/50 ) # Set pattern width
t.color(c) # Set's turtle color
t.forward(step) # Move the turtle forward by "steps"
t.left(30) # Turn the turtle 30 degree to left

if __name__ == '__main__':
print("Sprial printing!!")
total_steps = int(input("enter no. of steps: "))
# split(',') denotes split input of colors string into list
color_list = input("enter the list of colors separated by commas: ").split(',')
spiral(total_steps,color_list)
t.done()



19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,17 @@ The PyVerse repository is organized as follows:
│ │ ├── main.py
│ │ └── screenshots
│ │ └── tkinter-working.gif
│ ├── QR Generator
│ │ ├── README.md
│ │ └── generate_qrcode.py
│ └── Stock App
│ ├── Stock App
│ │ ├── Readme.md
│ │ ├── Templates
│ │ │ ├── base.html
│ │ │ ├── financials.html
│ │ │ └── index.html
│ │ └── server.py
│ └── Turtle
│ ├── Readme.md
│ ├── Templates
│ │ ├── base.html
│ │ ├── financials.html
│ │ └── index.html
│ └── server.py
│ ├── rainbow_spiral.py
│ └── turtle_spiral.py
├── Blockchain_Development
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
Expand Down
19 changes: 10 additions & 9 deletions repo_structure.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@
│ │ ├── main.py
│ │ └── screenshots
│ │ └── tkinter-working.gif
│ ├── QR Generator
│ │ ├── README.md
│ │ └── generate_qrcode.py
│ └── Stock App
│ ├── Stock App
│ │ ├── Readme.md
│ │ ├── Templates
│ │ │ ├── base.html
│ │ │ ├── financials.html
│ │ │ └── index.html
│ │ └── server.py
│ └── Turtle
│ ├── Readme.md
│ ├── Templates
│ │ ├── base.html
│ │ ├── financials.html
│ │ └── index.html
│ └── server.py
│ ├── rainbow_spiral.py
│ └── turtle_spiral.py
├── Blockchain_Development
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
Expand Down
Loading