example 1: Before: Wanted output: example 2: Any form of randomization generation, y'know?
Python Turtle: For my spare time project, I need my code to generate the "land" and "beach" circle areas by random, like a real life island would look like, with plenty of curves and rough edges. I also need the area of the drawing circles to be three times as big, when you previously run the current code below. I have also included an image for what the land should look like.
(link from previous question): https://www.bartleby.com/questions-and-answers/python-how-do-i-do-specifically-do-the-following-to-my-code-1.-have-a-gui-asking-for-the-amount-of-l/9917ac48-57dd-48bb-8522-bf1cb006377d
The code so far:
import turtle
import random
import tkinter as tk
from tkinter import simpledialog
# Define colors
ocean = "#000066"
sand = "#ffff66"
grass = "#00cc00"
lake = "#0066ff"
mountain_color = "#808080" # Gray color for mountains
# Store mountain positions
mountain_positions = []
def draw_circle(radius, line_color, fill_color):
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
my_turtle.circle(radius)
my_turtle.end_fill()
def draw_triangle(size, line_color, fill_color):
my_turtle.color(line_color)
my_turtle.fillcolor(fill_color)
my_turtle.begin_fill()
for _ in range(3):
my_turtle.forward(size)
my_turtle.left(120)
my_turtle.end_fill()
def move_turtle(x, y):
my_turtle.penup()
my_turtle.goto(x, y)
my_turtle.pendown()
def draw_lake():
x = random.randint(-100, 100)
y = random.randint(-50, 100)
move_turtle(x, y)
draw_circle(40, lake, lake) # Increased radius
def draw_mountain():
x = random.randint(-100, 100)
y = random.randint(-50, 100)
mountain_positions.append((x, y))
def draw_river():
move_turtle(-100, 100)
my_turtle.setheading(0)
my_turtle.color(ocean)
my_turtle.pendown()
my_turtle.forward(200)
my_turtle.penup()
def get_user_input():
root = tk.Tk()
root.withdraw()
num_lakes = simpledialog.askinteger("Input", "How many lakes?", minvalue=1, maxvalue=10)
num_mountains = simpledialog.askinteger("Input", "How many mountains?", minvalue=1, maxvalue=10)
num_rivers = simpledialog.askinteger("Input", "How many rivers?", minvalue=1, maxvalue=10)
return num_lakes, num_mountains, num_rivers
# Get user input for number of lakes, mountains, and rivers
num_lakes, num_mountains, num_rivers = get_user_input()
# Initialize screen
screen = turtle.Screen()
screen.bgcolor(ocean)
screen.title("Island Generator")
my_turtle = turtle.Turtle()
my_turtle.pensize(4)
my_turtle.shape("circle")
# Draw the sand area
draw_circle(150, sand, sand)
# Draw the grass area
move_turtle(0, 40)
draw_circle(120, grass, grass)
# Draw lakes as bigger random dots
for _ in range(num_lakes):
draw_lake()
# Draw mountains
for _ in range(num_mountains):
draw_mountain()
# Draw mountains as triangles
for pos in mountain_positions:
move_turtle(pos[0], pos[1])
draw_triangle(45, mountain_color, mountain_color) # Increased size
# Draw rivers
for _ in range(num_rivers):
draw_river()
turtle.done()
Step by step
Solved in 2 steps