Python3-Turtle: Pen color not changing - python

I have a function that draws a brick wall, with the option to set a certain pen color before the wall is drawn, however it seems that the brick is drawn as green no matter what I do:
def draw_brick(length, width):
t.color("green")
t.begin_fill()
for i in range(2):
t.forward(length)
t.right(90)
t.forward(width)
t.right(90)
t.end_fill()
def draw_row(row_type, bricks):
if row_type == "A":
for i in range(bricks):
draw_brick(50, 30)
t.penup()
t.forward(70)
t.pendown()
elif row_type == "B":
draw_brick(12, 30)
t.penup()
t.forward(35)
t.pendown()
for i in range(bricks - 1):
draw_brick(50, 30)
t.penup()
t.forward(70)
t.pendown()
t.penup()
t.pendown()
draw_brick(12, 30)
def draw_brick_wall(rows, brick, top_row, new_color):
t.pencolor(new_color)
if top_row == "A":
drawing_A = True
else:
drawing_A = False
for i in range(rows):
next_position = (t.xcor(), t.ycor() - 40)
if drawing_A:
draw_row("A", brick)
else:
draw_row("B", brick)
drawing_A = not (drawing_A)
move_no_trails(next_position[0], next_position[1])
# resets turtle to postion (x, y)
def move_no_trails(x, y):
t.penup()
t.goto(x, y)
t.pendown()
For some reason, though, the pen color of the turtle doesn't change. What could I do to remedy this?

Once I add a compatible draw_brick() function, your code works fine for me:
from turtle import Screen, Turtle
def draw_brick(width, height):
for _ in range(2):
turtle.forward(width)
turtle.left(90)
turtle.forward(height)
turtle.left(90)
def draw_row(row_type, bricks):
if row_type == "A":
for _ in range(bricks):
draw_brick(50, 30)
turtle.penup()
turtle.forward(70)
turtle.pendown()
elif row_type == "B":
draw_brick(12, 30)
turtle.penup()
turtle.forward(35)
turtle.pendown()
for _ in range(bricks-1):
draw_brick(50, 30)
turtle.penup()
turtle.forward(70)
turtle.pendown()
draw_brick(12, 30)
# resets turtle to postion (x, y)
def move_no_trails(x, y):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
def draw_brick_wall(rows, brick, top_row, new_color):
turtle.pencolor(new_color)
drawing_A = top_row == "A"
for _ in range(rows):
next_x, next_y = turtle.xcor(), turtle.ycor() - 40
if drawing_A:
draw_row("A", brick)
else:
draw_row("B", brick)
drawing_A = not drawing_A
move_no_trails(next_x, next_y)
screen = Screen()
turtle = Turtle()
turtle.speed('fastest') # because I have no patience
draw_brick_wall(1, 5, "A", "red")
draw_brick_wall(1, 5, "B", "blue")
draw_brick_wall(1, 5, "A", "orange")
draw_brick_wall(1, 5, "B", "green")
screen.exitonclick()
If, however, your draw_brick() function is drawing a filled brick, then you'll need to change the call to pencolor(new_color) in draw_brick_wall() to either fillcolor(new_color) to set the fill color or color(new_color) to set both the pen and fill colors.

Related

Creating function to check if win conditions are satisfied for turtle tictactoe game

I've created a game of Tic-Tac-Toe that can be played using the turtle module. It draws the board, then using class methods draws the circles or X's in the spot designated by a simple numbering system and coordinates.
I'm trying to finish this project up by testing for if a win condition has been satisfied by a player, and if yes, it draws a line through them and finishes the game.
Trouble is, I don't know where to start. I had the idea of adding the values of the position to a list for each player (for example, if the circle player puts a circle in position 1, a 1 gets added to the circles list) then creating if statements to check for certain conditions. However, I quickly got lost and now I'm not sure what to do. Here's what my code looks like
#Class that allows for drawing the circles and X's
class CircleOrSquare():
def __init__(self):
pass
def circle(self, x, y): #Draws from the bottom middle of the circle
import turtle
turtle.color('red')
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
turtle.circle(10)
def cross(self, x, y): #Draws from the bottom middle of the cross
import turtle
turtle.color('blue')
#position x,y at center base of cross
turtle.penup()
turtle.goto(x, y)
x = x-10 #Change x to the bottom left leg
#Draw the X
turtle.goto(x, y)
turtle.pendown()
turtle.goto(x+20, y+20)
turtle.penup()
turtle.goto(x, y+20)
turtle.pendown()
turtle.goto(x+20, y)
def main():
import turtle
canvas = turtle.Screen()
canvas.setup(200, 200)
canvas.bgcolor('light grey')
turtle.hideturtle()
#Vertical lines
turtle.penup()
turtle.goto(-25, 50)
turtle.pendown()
turtle.goto(-25, -50)
turtle.penup()
turtle.goto(25, 50)
turtle.pendown()
turtle.goto(25, -50)
turtle.penup()
#Horizontal lines
turtle.goto(-50, 20)
turtle.pendown()
turtle.goto(50, 20)
turtle.penup()
turtle.goto(-50, -20)
turtle.pendown()
turtle.goto(50, -20)
turtle.penup()
#Tic-Tac-Toe
turtle.goto(-28, 75)
turtle.write("Tic-Tac-Toe")
positions = {1: [-40, 30], 2: [0, 30], 3: [40, 30], 4: [-40, -10], 5: [0, -10],
6: [40, -10], 7: [-40, -45], 8: [0, -45], 9: [40, -45]}
screen = turtle.Screen()
for i in range(9):
move = screen.textinput("Next Move", "Starting from the top left"
", enter 1-9 for your move: ")
if i % 2 == 0:
answer = CircleOrSquare()
answer.circle(positions[int(move)][0], positions[int(move)][-1])
else:
answer = CircleOrSquare()
answer.cross(positions[int(move)][0], positions[int(move)][-1])
turtle.exitonclick()
main()

Labeling a game board in python using turtle graphics

I'm currently making a 5x5 grid for a snakes and ladder but can't figure out how to display the numbers on the grid in this formation;
21,22,23,24,25
20,19,18,17,16
11,12,13,14,15
10, 9, 8, 7, 6
1 , 2, 3, 4, 5
The code I've wrote makes the 5x5 grid, I'm juts note sure how to add the labels of if I should take a
different approach with the code.
thanks for any help.
import turtle
t = turtle.Turtle()
speed = turtle.speed()
t.speed(10)
x=250
y=250
t.penup()
t.goto(-x,y)
t.pendown()
turtle.hideturtle()
turtle.ht()
for a in range(5):
t.penup()
t.goto(-x,-y)
t.pendown()
y=y-100
for b in range(5):
for n in range(5):
t.speed(10)
t.fd(100)
if n!=4:
t.right(90) here
Here's a solution that uses much of your existing logic, tweaking it a bit, and adds the numbers:
from turtle import Screen, Turtle
FONT_SIZE = 18
FONT = ('Arial', FONT_SIZE, 'bold')
BOXES = 5
BOX_SIZE = 100
screen = Screen()
turtle = Turtle()
turtle.hideturtle()
turtle.speed('fastest')
x = -BOX_SIZE * BOXES / 2
y = -BOX_SIZE * BOXES / 2
for i in range(BOXES):
turtle.penup()
turtle.goto(x, y)
turtle.pendown()
for j in range(BOXES):
for _ in range(4):
turtle.forward(BOX_SIZE)
turtle.left(90)
dx, dy = turtle.position()
turtle.penup()
turtle.goto(dx + BOX_SIZE/2, dy + BOX_SIZE/2 - FONT_SIZE / 2)
turtle.write(j + BOXES * i + 1, align='center', font=FONT)
turtle.setposition(dx, dy)
turtle.pendown()
turtle.forward(BOX_SIZE)
y += BOX_SIZE
screen.exitonclick()
Centering-wise, the vertical font correction (FONT_SIZE / 2) is an approximation that works fine on my system but if you want an accurate solution for all systems, see this answer

Adding user checks to Python turtle tic-tac-toe game

import turtle
#1)draw board
sc= turtle.Screen()
sc.setup(300,300)
import turtle
sc= turtle.Screen()
sc.setup(300,300)
turtle.speed(0)
turtle.pensize(10)
turtle.bgcolor("black")
turtle.pencolor("white")
turtle.penup()
turtle.goto(-150,50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150,-50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-50,150)
turtle.setheading(-90)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50,150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
#2) X's and O's
turtle.pencolor("yellow")
def kreuz(x,y):
turtle.penup()
turtle.goto(x+30,y-45)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x+60,y-40)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def kreis(x,y):
turtle.penup()
turtle.goto(x+50,y-80)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
AlleTeile=["","","","","","","","",""]
nächsterZug="X"
def zeichneTeile (AlleTeile):
x = -150
y = 150
for einTeil in AlleTeile:
if einTeil=="X":
kreuz(x,y)
elif einTeil=="O":
kreis(x,y)
else:
print("leeres feld")
x=x+100
if x > 50:
x=-150
y=y-100
zeichneTeile(AlleTeile)
def geklickt(x,y):
global nächsterZug,AlleTeile
senkrecht= (x+150) // 100
waagrecht= (-y+150)// 100
Bereich= senkrecht+waagrecht*3
Bereich=int(Bereich)
print("Du klicktest auf Bereich-Nummer", Bereich)
AlleTeile[Bereich]=nächsterZug
if nächsterZug =="X":
nächsterZug="O"
else:
nächsterZug="X"
zeichneTeile(AlleTeile)
turtle.onscreenclick(geklickt)
turtle.mainloop()
I want to create this tic-tac-toe game using turtle in Python but I am stuck. The problem is that I keep drawing noughts and crosses on the game board after all 9 fields are full with noughts and crosses. How can I code this so that after 9 turns (9 fields) it is no longer possible to keep on drawing?
Since unused squares consist of an empty string in AlleTeile, this is trivial to fix since empty strings are False in a boolean context. At the top of geklickt(), after the global statement:
if all(AlleTeile):
print("All taken!")
return
We can reuse the same trick later in that function to prevent overwritting of existing squares. After the Bereich calculation, we can do something like:
print("Du klicktest auf Bereich-Nummer", Bereich, end="")
if AlleTeile[Bereich]:
print(" -- already taken!")
return
else:
print()
Here's my complete rework with the above changes, some turtle idoms, code cleanup and English translation courtesy of Google (and common sense):
from turtle import Screen, Turtle
# X's and O's
def cross(x, y):
turtle.penup()
turtle.goto(x + 30, y - 35)
turtle.setheading(-45)
turtle.pendown()
turtle.forward(50)
turtle.penup()
turtle.goto(x + 60, y - 30)
turtle.setheading(-130)
turtle.pendown()
turtle.forward(50)
turtle.penup()
def nought(x, y):
turtle.penup()
turtle.goto(x + 50, y - 70)
turtle.setheading(0)
turtle.pendown()
turtle.circle(20)
turtle.penup()
# Playing the game
def drawSquares():
screen.tracer(False) # because this routine is a graphics bottleneck
x, y = -150, 150
for square in AllSquares:
if square == "X":
cross(x, y)
elif square == "O":
nought(x, y)
x += 100
if x > 50:
x = -150
y -= 100
screen.tracer(True)
def clicked(x, y):
global turn
if all(AllSquares):
print("All taken!")
return
vertical = (x + 150) // 100
horizontal = (150 - y) // 100
Area = int(vertical + horizontal * 3)
print("You clicked area number", Area, end="")
if AllSquares[Area]:
print(" -- already taken!")
return
else:
print()
AllSquares[Area] = turn
if turn == "X":
turn = "O"
else:
turn = "X"
drawSquares()
# Draw the board
screen = Screen()
screen.setup(330, 330)
screen.bgcolor("black")
turtle = Turtle(visible=False)
turtle.pensize(10)
turtle.color("white")
turtle.speed('fastest')
turtle.penup()
turtle.goto(-150, 50)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(-150, -50)
turtle.pendown()
turtle.forward(300)
turtle.setheading(-90)
turtle.penup()
turtle.goto(-50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
turtle.goto(50, 150)
turtle.pendown()
turtle.forward(300)
turtle.penup()
# Start the game
turtle.color("yellow")
AllSquares = ["", "", "", "", "", "", "", "", ""]
turn = "X"
drawSquares()
screen.onscreenclick(clicked)
screen.mainloop()

Changing bar graph colors in Python

I am new to Python and I am struggling with a lesson in our book where we have to change the colors in a bar graph. I am not sure what I am doing wrong. There is no error message the colors are just printing black.
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
if xs is 48:
tess.color("blue")
if xs is 117:
tess.color("yellow")
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()
This is what I have so far.
Thanks for the help!
I think you need to have a color assigned before you begin_fill(). Because the computer is like: "Well, I'll fill the box, but I don't have a color", so it uses the default of black.
The if statement you provided doesn't make sense, so I moved it up in the function and modified it where you can examine the height within the function. Instead of using if/else, I'd recommend dictionaries.
This new code works:
import turtle
tess = turtle.Turtle()
def draw_bar(t, height):
# Select Color
if height is 48:
color = "blue"
elif height is 117:
color = "yellow"
else:
color = "black"
# Assign color
t.color(color)
# The rest of your code
t.begin_fill()
t.left(90)
t.forward(height)
t.write(" "+ str(height))
t.right(90)
t.forward(40)
t.right(90)
t.forward(height)
t.penup()
t.left(90)
t.end_fill()
t.forward(10)
t.pendown()
tess.pensize(3)
wn = turtle.Screen()
wn.bgcolor("lightgreen")
xs = [48,117,200,240,160,260,220]
for a in xs:
draw_bar(tess, a)
wn.mainloop()

How do I run both these turtle scripts one after another

import random
import turtle
spiral = turtle.Turtle()
turtle.bgcolor("black")
spiral.color("cadet blue")
randomfive = random.randint(5,15)
spiral.pensize(randomfive)
randomfour = random.randint(20,75)
spiral.speed (randomfour)
randomthree = random.randint(200,500)
randomtwo = random.randint(5,20)
random = random.randint(10,360)
spiral.pendown()
for i in range(randomthree):
spiral.forward(i * randomtwo)
spiral.right(random)
def christmas ():
turtle.bgcolor("black")
for i in range(2):
turtle.speed (9999999)
coordone = random.randint (-300,300)
coordtwo = random.randint (-300,300)
right = random.randint (1,360)
turtle.penup()
turtle.goto (coordone,coordtwo)
turtle.pendown()
turtle.right(right)
for y in range (10):
turtle.speed(9999)
turtle.right(36)
for x in range (50):
turtle.color("red")
turtle.speed(99999)
turtle.pensize(x/5)
turtle.forward (x-(x-1))
turtle.penup()
turtle.goto (coordone,coordtwo)
turtle.pendown()
turtle.right(18)
for z in range (10):
turtle.speed(9999)
turtle.right(36)
for w in range (50):
turtle.color("green")
turtle.speed(99999)
turtle.pensize(w/3)
turtle.forward (w-(w-1))
turtle.penup()
turtle.goto (coordone,coordtwo)
turtle.pendown()
christmas()
When I try this python says "AttributeError: 'int' object has no attribute 'randint'" (By the way, this is the whole code). Please help with this issue. The name of this is droplets.py so that wouldn't be the issue.
import turtle
import random
def spiral():
turtle.bgcolor("black")
spiral = turtle.Turtle()
spiral.color("cadet blue")
randomfive = random.randint(5, 15)
spiral.pensize(randomfive)
randomfour = random.randint(0, 11)
spiral.speed(randomfour)
randomthree = random.randint(200, 500)
randomtwo = random.randint(5, 20)
randomone = random.randint(10, 360)
spiral.pendown()
for i in range(randomthree):
spiral.forward(i * randomtwo)
spiral.right(randomone)
spiral.penup()
def christmas():
turtle.bgcolor("black")
turtle.speed("fastest")
for i in range(2):
coordinate = random.randint(-300, 300), random.randint(-300, 300)
right = random.randint(1, 360)
turtle.penup()
turtle.goto(coordinate)
turtle.pendown()
turtle.right(right)
for y in range(10):
turtle.right(36)
for x in range(50):
turtle.color("red")
turtle.pensize(x / 5)
turtle.forward(1)
turtle.penup()
turtle.goto(coordinate)
turtle.pendown()
turtle.right(18)
for z in range(10):
turtle.right(36)
for w in range(50):
turtle.color("green")
turtle.pensize(w / 3)
turtle.forward(1)
turtle.penup()
turtle.goto(coordinate)
turtle.pendown()
spiral()
christmas()
turtle.done()

Categories

Resources