Monday, September 28, 2020

Tutorial: Rounded Rectangle or Square with Python Turtle

https://pythonturtle.academy/tutorial-round-rectangle-or-square-with-python-turtle/

 

In this tutorial we are going to show how to draw rectangles or squares with round corners.

Round Rectangle

We will start by drawing the bottom straight-line with blue color. The following is the code snippet:

turtle.up()
turtle.goto(-200,-150)
turtle.color('blue')
turtle.down()
turtle.dot()
turtle.fd(400)
turtle.dot()

It should draw the following shape. The blue dots were drawn to show the starting and end points. They will be removed later.

We are going to draw the round corners with 90 degree arc of a circle in red color. The radius of the arc can be any number. Smaller radius generates smaller round corners. The following is the code snippet for drawing the round corner:

turtle.up()
turtle.goto(-200,-150)
turtle.color('blue')
turtle.down()
turtle.dot()
turtle.fd(400)
turtle.dot()
turtle.color('red')
turtle.circle(100,90)
turtle.dot()

It should draw a shape like this:

The next step will be easier. Just repeat the step above for the next side. The following is the code snippet:

turtle.up()
turtle.goto(-200,-150)
turtle.color('blue')
turtle.down()
turtle.dot()
turtle.fd(400)
turtle.dot()
turtle.color('red')
turtle.circle(100,90)
turtle.dot()
turtle.color('blue')
turtle.fd(200)
turtle.dot()
turtle.color('red')
turtle.circle(100,90)
turtle.dot()

It draws a shape like the following:

To finish the whole round rectangle, we just need to repeat the process one more time. The following is the complete code snippet with dots and colors removed:

turtle.up()
turtle.goto(-200,-150)
turtle.down()
for _ in range(2):
    turtle.fd(400)
    turtle.circle(100,90)
    turtle.fd(200)
    turtle.circle(100,90)

You may want to generalize this by creating a function that draws round rectangles with any corner size. The following is the complete code:

import turtle

turtle.speed(0)
turtle.hideturtle()
turtle.setup(1000,1000)
turtle.title('Rounded Rectangle - PythonTurtle.Academy')
turtle.speed(0)
turtle.up()
turtle.hideturtle()

def round_rectangle(center_x,center_y,width,height,cornersize):
    turtle.up()
    turtle.goto(center_x-width/2+cornersize,center_y-height/2)
    turtle.down()
    for _ in range(2):
        turtle.fd(width-2*cornersize)
        turtle.circle(cornersize,90)
        turtle.fd(height-2*cornersize)
        turtle.circle(cornersize,90)

round_rectangle(0,0,200,300,20)    
round_rectangle(-100,300,400,200,100)    
round_rectangle(200,-300,300,300,150)
round_rectangle(-200,-300,200,200,50)        

It draws the following shape:

Round Rectangle or Square with Python Turtle

Tuesday, August 11, 2020

Python turtle set start position


I'll give you my solution for starting at the top left of the window and you can adjust it to your needs:
 
 
from turtle import Turtle, Screen

TURTLE_SIZE = 20

screen = Screen()

yertle = Turtle(shape="turtle", visible=False)
yertle.penup()
yertle.goto(TURTLE_SIZE/2 - screen.window_width()/2, screen.window_height()/2 - TURTLE_SIZE/2)
yertle.pendown()
yertle.showturtle()

screen.mainloop()


The turtle's first appearance should be at the top left of the window.



Python turtle, change start position:
import turtle
a = turtle.Turtle()      #instantiate a new turtle object called 'a'
a.hideturtle()           #make the turtle invisible
a.penup()                #don't draw when turtle moves
a.goto(-200, -200)       #move the turtle to a location
a.showturtle()           #make the turtle visible
a.pendown()              #draw when the turtle moves
a.goto(50, 50)           #move the turtle to a new location
The turtle becomes visible and starts drawing at position -200, -200 and goes to 50, 50.

Lab Assignment

CCS CS20, Spring 2018

num ready? description assigned due
lab00 true Getting Started Mon 04/02 08:00AM Fri 04/06 05:00PM
lab01 true FtoC and CtoF with test cases Mon 04/02 08:00AM Fri 04/13 05:00PM
lab02 true More practice with writing functions test cases Tue 04/10 12:30PM Tue 04/17 05:00PM
lab03 true Even more functions with test cases Tue 04/17 12:30PM Tue 04/24 12:30PM
lab04 true input, scripts, command line arguments Fri 08/25 11:00AM Wed 08/30 04:50PM
lab05 true Accumulator Pattern, more TDD practice Mon 04/30 08:00AM Mon 05/07 09:00PM
lab06 true min/max, index vs. value Tue 05/01 12:30PM Thu 05/10 12:30PM
lab07 true Turtle Graphics: Basic shapes Thu 05/03 12:30PM Thu 05/10 12:30PM
lab08 true 2D Lists and Nested for Loops Tue 05/08 08:00AM Fri 05/18 11:59PM
lab09 true Recursion Thu 05/10 01:00PM Thu 05/24 11:59PM
lab10 true Review for Final (optional) Tue 06/05 12:30PM Fri 06/08 05:00PM
project01 true Project-01 Turtle Graphics: Scene from a forest Thu 05/10 12:30PM Sun 05/20 11:59PM
num ready? description assigned due
h00 true Perkovic Ch1 (Introduction to CS) Tue 04/17 12:30PM Wed 04/18 12:00PM
h01 true Perkovic 2.1, 2.2 (Expr, Vars, Assignment, Strings) Tue 04/17 12:30PM Thu 04/19 12:00PM
h02 true Perkovic 2.3 (Lists, Tuples) Tue 04/17 12:30PM Fri 04/20 12:00PM
h03 true Perkovic 2.4-2.5 (Objects, Classes, types, libraries) Tue 04/17 12:30PM Sat 04/21 12:00PM
h04 true Perkovic 3.1 (Python Programs), 3.2 (Execution Control Structures) Tue 04/17 12:30PM Sun 04/22 12:00PM
h05 true Perkovic 3.3-3.5 (Functions) Tue 04/17 12:30PM Mon 04/23 12:00PM
h06 true Perkovic 4.1-4.2 (Strings, Formatting) Tue 04/17 12:30PM Tue 04/24 12:00PM
h07 true Perkovic 4.3 (Files), 4.4 (Errors and Exceptions) Thu 05/24 12:30PM Thu 05/31 12:30PM
h08 true Perkovic 5.1-5.2 (decision control,accumulators,nested loops) Thu 05/24 12:30PM Thu 05/31 12:30PM
h09 true Perkovic 5.3-5.6 (2-d lists, more on loops) Thu 05/24 12:30PM Thu 05/31 12:30PM
h10 true Perkovic 6.3 (Character Encodings)- 6.4 (Random) Thu 05/24 12:30PM Thu 05/31 12:30PM
h11 true Review (tracing functions) Thu 05/24 12:30PM Thu 05/31 12:30PM
h12 true Perkovic 6.1 (Dictionaries) and 6.2 (Sets) Thu 05/24 12:30PM Thu 05/31 12:30PM
h13 true Perkovic 10.1 (Introduction to Recursion - up to Practice Problem 10.3) Thu 05/24 12:30PM Thu 05/31 12:30PM
h14 true Number Conversions Thu 05/24 12:30PM Thu 05/31 12:30PM
ic00 true CCS CS20 S18 Pair Partner Picking Planning Tue 04/03 12:30PM Tue 04/03 02:20PM
ic01 true Planning your tree for project01 Tue 05/15 12:30PM Tue 05/15 12:30PM
num date description
Lecture 1 Mon 04/03 Orientation to the course
Lecture 2 Wed 04/05 Orientation to the course
Lecture 5 Mon 04/17 work on lab03, catch up on homeworks
Lecture 6Wed 04/19Working with JSON files

Sunday, August 9, 2020

TOPICS

https://techwithtim.net/tutorials/

TOPICS

Turtle in python- Trying to get the turtle to move to the mouse click position and print its coordinates



Printing every time you move





If you want to print every time you move, you should define your own function which will do that as well as tell the turtle to go somewhere. I think this will work because turtle is a singleton.
def gotoandprint(x, y):
    gotoresult = turtle.goto(x, y)
    print(turtle.xcor(), turtle.ycor())
    return gotoresult

screen.onscreenclick(gotoandprint)



import turtle
from turtle import Screen, Turtle

screen = Screen()
t = Turtle("turtle")
t.speed(-1)

def dragging(x, y):  # These parameters will be the mouse position
    t.ondrag(None)
    t.setheading(t.towards(x, y))
    t.goto(x, y)
    t.ondrag(dragging)

def clickRight():
    t.clear()

def main():  # This will run the program
    turtle.listen()
    
    t.ondrag(dragging)  # When we drag the turtle object call dragging
    turtle.onscreenclick(clickRight, 3)

    screen.mainloop()  # This will continue running main() 

main()

Introduction to Keras and TensorFlow for Training Deep Learning Classifiers

 ### Introduction to Keras and TensorFlow for Training Deep Learning Classifiers **Keras and TensorFlow** are powerful tools in the realm of...