Simple Python Turtle Graphic and Code: Bubbles

Опубликовано: 03 Ноябрь 2024
на канале: Basic Python Turtle Art
117
0

Multi-colored bubbles that are generated with the "randint" function of the "random" module. The bubbles are assigned randomly selected radius for size, starting point for position, and RGB values for color. The Python Turtle code is surprisingly short at 14 lines.

Feel free to copy the simple Python Turtle code that is given below. Don't hesitate to ask questions about the code if you have any. Enjoy! Please comment, like, or subscribe :)

Incidentally, for manually colorable graphics and variations, please visit my author site at https://www.amazon.com/author/basicpy...

import turtle
from random import randint #Requirement to use selection of random integers from a given range
t = turtle.Turtle() #Definitions and Initializations
screen = turtle.Screen()
t.pensize(10)
screen.colormode(255) #Requirement for use of values 0 to 255 for red, green, and blue in (r, g, b)
for i in range(40): #Loop to draw 40 bubbles with randomly selected location, color, and size
t.penup()
t.goto(randint(-250, 250), randint(-250, 150)) #Trail-less movement of turtle to lower point of bubble to be drawn
t.color(randint(0, 255), randint(0, 255), randint(0, 255)) #Random selection of r, g, b values for bubble color
radius = randint(10, 50) #Random selection of radius for bubble size
t.pendown()
t.circle(radius) #Drawing of a bubble with randomly selected location, color, and size
screen.exitonclick()