How to draw a filled rectangle in python turtle - python

import turtle
def penup():
for x in drawings:
x.penup()
penup()
def drawgrass():
for x in range(10):
grass.goto(300,300)
grass.color("green")
grass.begin_fill()
grass.forward(200)
grass.left(300)
grass.forward(200)
grass.left(300)
grass.end_fill()
penup()
drawgrass()
So I am creating this program that will make a landscape of some kind, and I am trying to make the grass. I was thinking of placing the cursor at 300,300, then drawing a huge rectange and then filling it making it green. So far I can not make the rectangle work, or make it cover the whole bottom half of the screen. Can someone help me please?

As in your other question, your penup() function makes no sense. Here's how I might go about filling half the screen with green:
from turtle import Screen, Turtle
def drawgrass():
grass.color("green")
width, height = screen.window_width(), screen.window_height()
grass.penup()
grass.goto(-width/2, 0)
grass.pendown()
grass.begin_fill()
for _ in range(2):
grass.forward(width)
grass.right(90)
grass.forward(height/2)
grass.right(90)
grass.end_fill()
screen = Screen()
grass = Turtle()
grass.speed('fastest') # because I have no patience
drawgrass()
grass.hideturtle()
screen.exitonclick()
But what I would really do is stamp it:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
def drawgrass():
grass.color('green')
grass.shape('square')
width, height = screen.window_width(), screen.window_height()
grass.penup()
grass.goto(0, -height/4)
grass.shapesize(height/2 / CURSOR_SIZE, width / CURSOR_SIZE)
grass.stamp()
screen = Screen()
grass = Turtle()
grass.hideturtle()
drawgrass()
screen.exitonclick()

Related

Why is my Python turtle screen asymmetrical?

I created a pong game where I noticed the paddles are not placed equally at the screen edges.
I created an 800 pixel wide screen, and placed paddles at xcor = 380 and xcor = -380 but on the screen left paddle shows some gap but right paddle doesn't. Is my screen unsymmetrical? How do I fix it?
screen.setup(width=800, height=600)
screen.bgcolor("black")
screen.title("PONG")
screen.tracer(0)
l_paddle = Paddle()
l_paddle.create_paddle((-380, 0))
r_paddle = Paddle()
r_paddle.create_paddle((380, 0))
screenshot of screen
When we specify a window size to setup(), we're talking about total pixels used on the screen. Since there is chrome around the window (edges, title bar, etc.) the actual area we have to work with is slightly smaller. Trying to place two turtles at exactly the left and right edge, ignoring chrome:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
WIDTH, HEIGHT = 600, 400
screen = Screen()
screen.setup(WIDTH, HEIGHT)
l_paddle = Turtle('square')
l_paddle.fillcolor('white')
l_paddle.setx(CURSOR_SIZE/2 - WIDTH/2)
r_paddle = Turtle('square')
r_paddle.fillcolor('white')
r_paddle.setx(WIDTH/2 - CURSOR_SIZE/2)
screen.exitonclick()
We get a result similar to yours:
If we compensate for the internal and external chrome elements:
from turtle import Screen, Turtle
CURSOR_SIZE = 20
BORDER_SIZE = 2 # inside the window
CHROME_SIZE = 9 # around the window
WIDTH, HEIGHT = 600, 400
screen = Screen()
screen.setup(WIDTH, HEIGHT)
l_paddle = Turtle('square')
l_paddle.fillcolor('white')
l_paddle.setx(CURSOR_SIZE/2 - WIDTH/2 + BORDER_SIZE)
r_paddle = Turtle('square')
r_paddle.fillcolor('white')
r_paddle.setx(WIDTH/2 - CURSOR_SIZE/2 - BORDER_SIZE - CHROME_SIZE)
screen.exitonclick()
We can get a more precise result:
The problem here is that the amount of chrome is system dependent but turtle doesn't tell us how much to compensate. You might be able to find out from the underlying tkinter code.
My recommendation is you estimate the best you can, assume it's not accurate on all systems, and stay away from the edges so it's less of an issue. The error can be greater in the Y dimension when the title bar is part of the chrome.

Why my python kept crashing when it tried to run the turtle.tracer() method in turtle module?

I was coding a Snake Game using turtle module but when I add this line into my code the turtle screen and python crashed:
turtle.tracer(0)
can somebody help me so I can complete the game? Thanks a lot
my code:
from turtle import Turtle, Screen, colormode
screen = Screen()
screen.bgcolor("black")
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle("square")
turtle.color("white")
turtle.penup()
turtle.goto(0-x, 0)
x += 20
segments.append(turtle)
game_is_on = True
screen.update()
while game_is_on:
for segment in segments:
segment.forward(20)
screen.exitonclick()
I think we need to know more about what you mean by 'crashed'. If you mean everything froze, that's the code you wrote. Once you introduce tracer() you need to provide an update() for every change you want the user to see. But you don't have any update() calls in your loop so everything visually remains as it was before the loop. If you want to see the segments move, you need to do something like:
from turtle import Turtle, Screen
screen = Screen()
screen.bgcolor('black')
screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.tracer(0)
x = 0
segments = []
for turtle in range(3):
turtle = Turtle('square')
turtle.color('white')
turtle.penup()
turtle.setx(x)
x -= 20
segments.append(turtle)
screen.update()
game_is_on = True
while game_is_on:
for segment in segments:
segment.forward(20)
screen.update()
screen.exitonclick() # never reached
If you mean by 'crashed' that Python quit back to the operating system, then you need to describe the environment under which you're running this code.

How to draw square in Turtle module without filling color?

I am trying to draw a square in turtle without any thing filled in it. I would manually draw it with a loop, but I am trying to write a program where if the square is touched, something would happen. If I drew it with a loop, even if I touch the border, it would not work, because the actual turtle isn't there.
We can do this by dedicating one turtle as the button. We need to change its shape, size and fill color and assign it a button event handler:
from turtle import Screen, Turtle
def thanks(x, y):
marker.write("Thank you!", align='center', font=('Arial', 24, 'bold'))
marker = Turtle()
marker.hideturtle()
marker.penup()
marker.sety(-200)
button = Turtle()
button.shape('square')
button.fillcolor('white')
button.shapesize(3)
button.onclick(thanks)
screen = Screen()
screen.mainloop()
Click on the square in the middle of the screen and it will thank you for doing so.
I am trying to draw a square in turtle without any thing filled in it.
The above solution does use a filled turtle, it just happens to be white fill. We can't draw within the square that is this button. If that's the need, then we can draw the square with a loop, but move our onclick() from the turtle to the screen. We'll need to make the onclick() itself determine if it's in the right place:
from turtle import Screen, Turtle
BUTTON_POSITION = (100, 100)
BUTTON_SIZE = 60
def thanks(x, y):
bx, by = BUTTON_POSITION
if bx < x < bx + BUTTON_SIZE and by < y < by + BUTTON_SIZE:
turtle.write("Thank you!", align='center', font=('Arial', 24, 'bold'))
turtle = Turtle()
turtle.hideturtle()
# draw the button
turtle.penup()
turtle.goto(BUTTON_POSITION)
turtle.pendown()
for _ in range(4):
turtle.forward(BUTTON_SIZE)
turtle.left(90)
# decorate our button a bit
turtle.penup()
turtle.forward(BUTTON_SIZE/2)
turtle.left(90)
turtle.forward(BUTTON_SIZE/2)
turtle.dot()
# position turtle for writing later
turtle.goto(0, -200)
screen = Screen()
screen.onclick(thanks)
screen.mainloop()

Entire screen not moving with the turtle

I am making a game in python using the turtle module in python.
I want the screen to move with the turtle. Is there any way to do it? Thanks
The problem is that the screen isn't moving with the turtle ... Does
anyone know why?
I know why. First, #martineau is correct, as usual +1, about passing the wrong range of values to yview_moveto(). But there's another piece to this puzzle: tkinter and turtle do not use the same coordinate system! You need to correct for the coordinate system difference, then turn the value into a percentage.
Here's a stripped down example based on your code and desired behavior. It keeps the ball in the middle of the window but you can tell from the numbers, and the vertical scroll bar, it's falling. Tap the the up arrow to slow it down -- tap it again to stop the motion. Tap it once more to start rising. Or use the down arrow to reverse your movement again:
from turtle import Screen, Turtle
WIDTH, DEPTH = 300, 10_000
CURSOR_SIZE = 20
vy = -10
def rise():
global vy
vy += 5
def fall():
global vy
vy -= 5
def move():
global vy
if abs(ball.ycor()) < DEPTH/2:
ball.forward(vy)
canvas.yview_moveto((DEPTH/2 - ball.ycor() - WIDTH/2 + CURSOR_SIZE) / DEPTH)
screen.update()
screen.ontimer(move)
screen = Screen()
screen.setup(WIDTH, WIDTH) # visible window
screen.screensize(WIDTH, DEPTH) # area window peeps into
screen.tracer(False)
canvas = screen.getcanvas()
marker = Turtle()
marker.hideturtle()
marker.penup()
marker.setx(-WIDTH/4) # so we can see we're moving
for y in range(-DEPTH//2, DEPTH//2, 100):
marker.sety(y)
marker.write(y, align='right')
ball = Turtle('circle')
ball.speed('fastest')
ball.setheading(90)
ball.penup()
screen.onkey(rise, 'Up')
screen.onkey(fall, 'Down')
screen.listen()
move()
screen.mainloop()

Setting a specific area bgcolor for python's 'turtle import'

I have searched the web for info but all I can find is what I already know:
from turtle import *
bgcolour('gold')
now what I want is to have something that will make half the screen one colour and half another split by specific corner coodinates.
e.g.
from turtle import *
bgcolour('gold')
(-200, -150 to -200, 150)
so that it will set only in a specific area.
You'll have to implement it yourself, e.g. like this:
from turtle import *
def left_bgcolor(c):
showturtle()
penup()
w = window_width()
h = window_height()
penwidth = 10
# set a big pen width so the turtle is visible while drawing
width(penwidth)
# adjust position so we're inside the window border
goto(-w/2 + penwidth/2, -h/2+penwidth)
setheading(90) # north
pendown()
color(c, c) # set both drawing and fill color to be the same color
begin_fill()
forward(h - penwidth)
right(90)
forward(w/2 - penwidth)
right(90)
forward(h - penwidth) # could be optimized..
right(90)
forward(w/2 - penwidth)
right(90)
end_fill()
penup()
goto(0,0) # end by setting the turtle in the middle of the screen
width(1)
color('black', 'white')
setheading(90)
screen = Screen()
left_bgcolor('orange')
This is yet another situation where stamping is simpler than drawing. I've also used a separate turtle instance so the default turtle is left as-is for the user:
import turtle
STAMP_UNIT = 20
def left_bgcolor(color):
squirtle = turtle.Turtle(shape="square", visible=False)
squirtle.penup()
screen = turtle.Screen()
width = screen.window_width() / 2
height = screen.window_height()
squirtle.setx(-width / 2)
squirtle.shapesize(height / STAMP_UNIT, width / STAMP_UNIT)
squirtle.color(color)
squirtle.stamp()
left_bgcolor('gold')
turtle.circle(100)
turtle.exitonclick()

Categories