Python turtle goes offscreen [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 days ago.
Improve this question
Im trying to draw a chessboard with the turtle but it goes offscreen. I need help.
import turtle
# Define board colors
LIGHT_COLOR = "#F0D9B5"
DARK_COLOR = "#B58863"
# Set up turtle
turtle.setup(1920, 1080)
board_turtle = turtle.Turtle()
board_turtle.speed(0)
board_turtle.penup()
board_turtle.goto(-180, 180)
turtle.tracer(False)
# The screen and window
screen = turtle.Screen()
screen.bgcolor("black")
def Draw():
# Draw the board
for i in range(8):
for j in range(8):
# Calculate square color
square_color = LIGHT_COLOR if (i + j) % 2 == 0 else DARK_COLOR
# Draw square
board_turtle.color(square_color)
board_turtle.begin_fill()
for _ in range(4):
board_turtle.forward(45 * 3)
board_turtle.right(90)
board_turtle.end_fill()
turtle.update()
# Move to next square
board_turtle.forward(45 * 3)
board_turtle.backward(360 * 3)
board_turtle.right(90)
board_turtle.forward(45 * 3)
board_turtle.left(90)
# Keep the turtle window open
turtle.done()
if __name__ == "__main__":
Draw()
I wanted it to draw on screen.

Related

How do i delete the last number that is being shown? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 months ago.
Improve this question
I am trying to make a game that has a cookie in the middle and if you click that cookie, your points increase
My problem is that once i display the points and increase it, the numbers overlap
import pygame
from sys import exit
pygame.init()
screen = pygame.display.set_mode((1000,500))
pygame.display.set_caption("Cookie Clicker")
clock = pygame.time.Clock()
screen.fill("White")
font1 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 25)
font2 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 30)
font3 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 35)
font4 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft. ttf", 40)
font5 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 45)
font6 = pygame.font.Font("c:/Users/oreni/OneDrive/Masaüstü/sprites/Minecraft.ttf", 50)
noc = 0
# Texts
nuofco = font4.render(str(noc), False, "Black")
nuofco_r = nuofco.get_rect(center = (237,80))
# Drawn Rects
cookie = pygame.draw.ellipse(screen, "Brown", (110,150,250,250))
pygame.draw.rect(screen, "Black", pygame.Rect(475,0,25,500))
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
if cookie.collidepoint(event.pos):
noc += 1
screen.blit(nuofco,nuofco_r)
pygame.display.update()
clock.tick(60)
After calculating the new number I think you need to redraw everything on the screen including the background. Because everything you draw on the screen is actually drawn on top of the previous one. And everything overlaps when the background is not drawn.
And also in the main loop, the usually recommended order is to take input, update variables, draw everything. It would be better for you if you first calculate everything that needs to be calculated and then draw what needs to be drawn.

Screen Won't Open When Drawing Checker Board Using Turtle

I am trying to draw a checker board using the Turtle library and am running into an error where the board window does not open. It was working at the beginning of my session about 30 minutes ago but, I changed some stuff and want to know why it changed.
Here is my code:
##This program draws a checkboard using the turtle library
import turtle
#below initiates the turtle pen and screen
penMain = turtle.Turtle()
turtleMain = turtle.Screen()
def turtleBoard():
for x in range(4):
penMain.forward(30)
penMain.left(90)
penMain.forward(30)
turtleMain.setup(600, 600)
penMain.speed(50)
for a in range(8):
penMain.up()
penMain.setpos(0, 30 * a)
penMain.down()
for x in range(8):
if (a + x)% 2 == 0:
squareColor = 'black'
else:
squareColor = 'white'
penMain.fillcolor(squareColor)
penMain.begin_fill()
turtleBoard()
penMain.end_fill()
I believe this code works besides my one error! Thank you all for your help in advance!
I can't say what changes you made to get your current code, but this code seems to be working:
##This program draws a checkboard using the turtle library
import turtle
#below initiates the turtle pen and screen
penMain = turtle.Turtle()
turtleMain = turtle.Screen()
def turtleBoard():
penMain.forward(30)
turtleMain.setup(600, 600)
penMain.speed(50)
for a in range(8):
for x in range(8):
penMain.up()
penMain.setpos(30 * x, 30 * a)
penMain.down()
penMain.begin_fill()
for xx in range(4):
penMain.forward(30)
penMain.left(90)
if a%2 == x%2:
squareColor = 'black'
else:
squareColor = 'white'
penMain.fillcolor(squareColor)
penMain.end_fill()
turtleBoard()
turtle.done()
Now that we've seen that your code can be made to work, let's consider stamping instead of drawing to make it work more simply and more quickly:
from turtle import Screen, Turtle
SQUARES_PER_EDGE = 8
SQUARE_SIZE = 30 # in pixels
OFFSET = SQUARE_SIZE * (SQUARES_PER_EDGE / 2) - SQUARE_SIZE/2 # center the board
CURSOR_SIZE = 20
def turtleBoard():
turtle.shape('square')
turtle.shapesize(SQUARE_SIZE / CURSOR_SIZE)
turtle.penup()
for y in range(SQUARES_PER_EDGE):
for x in range(SQUARES_PER_EDGE):
turtle.goto(x * SQUARE_SIZE - OFFSET, y * SQUARE_SIZE - OFFSET)
turtle.fillcolor('black' if y % 2 == x % 2 else 'white')
turtle.stamp()
screen = Screen()
screen.setup(600, 600)
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
turtleBoard()
screen.exitonclick()
I lined up the indent of the bottom 4 lines with the last 'else' statement and it worked. Thank you guys!

Python Turtle, change visible part

On the canvas,I draw two dots,right one is at (100,0),left one is at (-1000,0).After initializing the program,the orginal screen location(visible part) is near the right dot,just like pic1 show
pic 1:[1]: https://i.stack.imgur.com/KtPRN.png
And now I wanna move the the screen(visible part) to the left dot using coordinate so that i can see it(pic2).What should I do?
pic 2:https://i.stack.imgur.com/Rtfrv.png
def drawDot(x):
penup()
goto(x, 0)
pendown()
dot('pink')
write(x)
b = -1000 #left dot(-1000,0)
a = 100 #right dot(100,0)
speed(0)
delay(0)
tracer(0, 0)
hideturtle()
screensize(500,500)
color('red')
bgcolor('black')
drawDot(a)
drawDot(b)
done()
I believe the following does what you describe. When the window opens, it's centered on (0, 0) and point a is visible off to the right and point b isn't visible at all. When you click on the window, it scrolls so that the window is centered on point b:
from turtle import Screen, Turtle
WINDOW_WIDTH, WINDOW_HEIGHT = 500, 500
CANVAS_WIDTH, CANVAS_HEIGHT = 3000, 1000
def drawDot(x):
turtle.penup()
turtle.setx(x)
turtle.dot('pink')
turtle.write(x)
def scrollToDot(x, y): # unused arguments
canvas = screen.getcanvas()
# tkinter has a different coordinate system
# we have to describe left edge of scrolled
# window as percentage in its coordinates:
screen_center = CANVAS_WIDTH / 2
dot_center = screen_center + b
left_edge = dot_center - screen.window_width() / 2
canvas.xview_moveto(left_edge / CANVAS_WIDTH) # percentage
a = 100 # right dot(100, 0)
b = -1000 # left dot(-1000, 0)
screen = Screen()
screen.setup(WINDOW_WIDTH, WINDOW_HEIGHT) # What we see
screen.screensize(CANVAS_WIDTH, CANVAS_HEIGHT) # What there is
screen.bgcolor('black')
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
turtle.color('red')
drawDot(a)
drawDot(b)
screen.onclick(scrollToDot)
screen.mainloop()
To do this, we have to access the tkinter Canvas underpinnings of turtle. However, the Canvas coordinate system is different than turtle's, so we have to make an adjustment as noted in the code comments.

How to alternate turns in a turtle based game

print("Finished Loading!")
print("x's turn")
xturn = True
if xturn == True:
usl.onclick(x1)
usc.onclick(yo)
usr.onclick(x3)
csr.onclick(x4)
csl.onclick(x5)
cs.onclick(x6)
dsl.onclick(x7)
dsc.onclick(x8)
dsr.onclick(x9)
else:
print("o's Turn")
usl.onclick(o1)
I am creating A Tic-tac-toe game for a class and have the playing mechanics finished(When someone clicks a box makes an x/o) but I am now struggling with turns. The excerpt above is what I would mostly likely need to motify. I was wondering if anyone knew how you can use events in turtle graphics to change a varible or if anyone knows a good system for alternating turns that would be Great!
I took this question as a challenge to write as little turtle code as possible to implement a working tic-tac-toe interface. The code below alternates between X's & O's turns and won't let a square be reused. It doesn't attempt to score the game nor itself play the game, it's just a minimal interface to build your smarts onto:
from turtle import Turtle, Screen
UNIT_SIZE = 80
HALF_GRID_SIZE = 3 * UNIT_SIZE / 2
FONT_SIZE = 48 # adjust to taste
x_turn = True
def choosen(turtle):
global x_turn
turtle.onclick(None) # taken so no longer responds
x, y = turtle.position()
magic_marker.goto(x, y - FONT_SIZE / 2)
magic_marker.write("X" if x_turn else "O", font=("Arial", FONT_SIZE, "bold"), align="center")
x_turn = not x_turn
magic_marker = Turtle(visible=False)
magic_marker.speed("fastest")
magic_marker.penup()
for row in range(-UNIT_SIZE, UNIT_SIZE + 1, UNIT_SIZE):
if row >= 0:
magic_marker.goto(-HALF_GRID_SIZE, row - UNIT_SIZE / 2)
magic_marker.pendown()
magic_marker.setx(HALF_GRID_SIZE)
magic_marker.penup()
for column in range(-UNIT_SIZE, UNIT_SIZE + 1, UNIT_SIZE):
if column >= 0:
magic_marker.goto(column - UNIT_SIZE / 2, -HALF_GRID_SIZE)
magic_marker.pendown()
magic_marker.sety(HALF_GRID_SIZE)
magic_marker.penup()
turtle = Turtle(shape="square", visible=False)
turtle.shapesize(0.8 * (UNIT_SIZE / 20)) # 0.8 = a safety margin
turtle.color("white")
turtle.penup()
turtle.goto(column, row)
turtle.showturtle() # still white on white but not clickable if not visible
turtle.onclick(lambda x, y, t = turtle: choosen(t))
screen = Screen()
screen.mainloop()
It uses click events and turns control over to the main loop so other events can also be active.

Drawing concentric squares

When going through the book "How to think like...", I got stuck with exercise 4.9.2.
The question is: "Write a program to draw this. Assume the innermost square is 20 units per side, and each successive square is 20 units bigger, per side, than the one inside it"
The following code represents how far I got till now:
import turtle
wn = turtle.Screen()
wn.bgcolor("lightgreen")
tess = turtle.Turtle()
def draw_square(t,size):
for i in range(4):
tess.forward(size)
tess.left(90)
size = 20
for j in range(3):
tess.pensize(3)
draw_square(tess,size)
size = size + 20
tess.penup()
tess.goto(-20, -20)
tess.pendown()
wn.mainloop()
Can someone be so kind and show me the right direction please?
Thanks!
Swen
The problem is here:
tess.goto(-20, -20)
You have two issues. First, if each square is 20 units bigger and you offset each square by (-20, -20) all squares would share one corner. Instead you want to offset the corner of the square by (-10, -10) so that the inner square is offset by 10 units on all sides.
The second issue is that .goto(x, y) sets an absolute position, not an offset. To move to an offset you need to calculate a new absolute position based on the offset:
tess.goto(tess.xcor()-10, tess.ycor()-10)
Or
tess.goto(tess.pos() + (-10, -10))
Sometimes when you're stuck, a good approach with turtle graphics is to think outside the square. If we consider the desired result as badly drawn concentric circles then the problem reduces to:
from turtle import Turtle, Screen
HYPOTENUSE = (2 ** 0.5) / 2
screen = Screen()
screen.bgcolor("lightgreen")
tess = Turtle()
tess.pencolor("red")
tess.setheading(45)
tess.width(3)
for radius in range(20, 20 * 5 + 1, 20):
tess.penup()
tess.goto(radius/2, -radius/2)
tess.pendown()
tess.circle(radius * HYPOTENUSE, steps=4)
screen.exitonclick()
OUTPUT
import turtle
def drawSquare (t, size):
for i in range (4):
t.forward(size)
t.left(90)
def main():
wn = turtle.Screen()
wn.bgcolor('black')
pat = turtle.Turtle()
pat.pensize(2)
pat.speed(10)
pat.color('blue')
space = -10
for i in range(20, 105, 20):
drawSquare(pat,i)
pat.up()
pat.goto(space, space)
pat.down()
space = space - 10
wn.exitonclick()
main()
import turtle
def draw_sqr(name,size):
for i in range(4):
name.forward(size)
name.left(90)
name.penup()
name.backward(10)
name.right(90)
name.forward(10)
name.left(90)
name.pendown()
window = turtle.Screen()
window.bgcolor('lightgreen')
window.title("conc_sqr")
x = turtle.Turtle()
x.color('hotpink')
x.pensize(3)
for i in range(5):
draw_sqr(x,20 + 20*i)
window.mainloop()
def draw_rectangle(animal, width, height):
for _ in range(2):
animal.forward(width)
animal.left(90)
animal.forward(height)
animal.left(90)
def draw_square(animal, size):
draw_rectangle(animal, size, size)
def make_window(back_color,title):
window = turtle.Screen()
window.bgcolor(back_color)
window.title(title)
return window
def make_turtle(color, size):
animal = turtle.Turtle()
animal.color(color)
animal.pensize(size)
return animal
import turtle
window = make_window("lightgreen","Squares")
tess = make_turtle("hotpink",3)
size_sqr=20
adjustment = 10
for _ in range(5):
draw_square(tess,size_sqr)
size_sqr += 20
tess.penup()
tess.backward(20/2)
tess.right(90)
tess.forward(20/2)
tess.left(90)
tess.pendown()
import turtle
def drawsquare(t,sz): #(turtle, size)
for i in range(4):
t.fd(sz)
t.right(90)
def main():
wn = turtle.Screen()
your_turtle = turtle.Turtle()
your_turtle.pensize(3)
wn.bgcolor("light green")
your_turtle.pencolor("hot pink")
for i in range(1,6):
drawsquare(your_turtle,20*i)
your_turtle.up()
your_turtle.left(135)
your_turtle.fd(14.142) #the distance between each square
your_turtle.right(135)
your_turtle.down()
main()
wn.exitonclick()

Categories

Resources