Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
why is my pen not drawing?
#1
This is my code in turtle graphics for chess. In the input class, I want the square that I click on the board to light up blue. It seems that I am unable to draw and fill the square. I have a few debug lines that show not only where I clicked, but also the coordinates of the bottom left of the square(I will start drawing and filling the square from there). The computer is getting to my code, but it just isn't drawing. Some detailed help would be great!


class ChessBoard:
 EAST = 0
 WEST = 180
 NORTH = 90
 SOUTH = 270
 
 def __init__(self, pen, square_sidelength, next_square, dark_color, light_color, border_color):
   
   '''
   
   Arguments for ChessBoard:
     
     square_sidelength = sidelength of one square on the board
     next_square = amount you have to move to get from one square to another
     dark_color = the darker of the two colors
     light_color = the lighter of the two colors
     color =  the color that is being used to draw the square it is drawing(just used for keeping track of things)
     y_top = the y coordinate of the top left of the board
     x_left = the x coordinate of the top left of the board
     x = changing x value
     y = changing y value
     border_size = the distance between the edge of the board and the edge of the border
   '''
   self.pen = pen
   self.square_sidelength = SQUARE_LENGTH
   self.next_square = square_sidelength + 1
   self.dark_color = dark_color
   self.light_color = light_color
   self.color = dark_color
   self.y_top = next_square * 4 
   self.x_left = -1 * (next_square*4)
   self.x = self.x_left
   self.y = self.y_top
   self.border_size = 1.15 * square_sidelength
   self.border_color = border_color
   self.outline_of_board_size = .07 * square_sidelength
   self.board_length = 8 * next_square
   self.board_squares = [[None for col in range(8)] for row in range(8)]
   
   
 def draw_square(self, pen, dark_color, light_color, square_sidelength, next_square, x, y, border_size):
   self.pen.up()
   self.pen.goto(self.x, self.y)
   self.pen.down()
   self.pen.color(self.color)
   self.pen.fill(True)
   for i in range(4):
     self.pen.forward(self.square_sidelength)
     self.pen.right(90)
   self.pen.fill(False)
   self.x += self.next_square
   
   
 def draw_row(self, pen, dark_color, light_color, square_sidelength, next_square, x, y, border_size):
   for i in range(8):
     self.draw_square(pen, dark_color, light_color, square_sidelength, next_square, x, y, border_size)
     if self.color == self.dark_color:
       self.color = self.light_color
     else:
       self.color = self.dark_color
       
       
 def draw_Board(self, pen, dark_color, light_color, square_sidelength, next_square, x, y, border_size, border_color, outline_of_board_size, board_length):
   #creates border
   self.pen.up()
   self.pen.goto(self.x_left - self.border_size, self.y_top + self.border_size)
   self.pen.down()
   self.pen.color(self.border_color)
   self.pen.fill(True)
   for i in range(4):
     self.pen.forward((2*self.border_size) + self.board_length)
     self.pen.right(90)
   self.pen.fill(False)
   self.pen.color('black')
   #creates outline
   self.pen.up()
   self.pen.goto(self.x_left - self.outline_of_board_size, self.y_top + self.outline_of_board_size)
   self.pen.down()
   self.pen.fill(True)
   for i in range(4):
     self.pen.forward((2*self.outline_of_board_size) + self.board_length)
     self.pen.right(90)
   self.pen.fill(False)
   #creates the board without borders and letters
   for i in range(8):
     p = 1
     self.draw_row(pen, dark_color, light_color, square_sidelength, next_square, x, y, border_size)
     self.pen.up()
     self.pen.goto(self.x_left - p, self.y)
     self.pen.down()
     self.x = self.x_left
     if self.color == self.dark_color:
       self.color = self.light_color
     else:
       self.color = self.dark_color
     self.y -= self.next_square
     p -= 5
     
   #creates letters and numbers
   self.pen.color('black')
   numbers = ['1', '2', '3', '4', '5', '6', '7', '8']
   letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
   self.x = self.x_left
   self.y = self.y_top
   
   #numbers
   for i in range(8):
     self.pen.up()
     self.pen.goto(self.x - (.8 * self.square_sidelength), self.y - (.75 * self.square_sidelength))
     self.pen.write(numbers[i], font=("Courier",round(.6 * self.square_sidelength), "normal"))
     self.y -= self.square_sidelength * 1.03
     
   #letters
   self.x = self.x_left
   self.y = self.y_top
   for i in range(8):
     self.pen.up()
     self.pen.goto(self.x + (.3 * self.square_sidelength), self.y - (self.board_length + .8 * self.square_sidelength))
     self.pen.write(letters[i], font=("Courier",round(.6 * self.square_sidelength), "normal"))
     self.x += self.square_sidelength * 1.03
     
 def goto_piece_xy(self, row, col):
   self.pen.up()
   x  = self.x_left + (col * self.next_square) + .05 * self.square_sidelength
   y = self.y_top - (row * self.next_square) - .8 * self.square_sidelength
   self.pen.goto(x,y)
 
   
 def put_piece(self, piece, row, col):
   self.board_squares[row][col] = piece
   self.pen.color((0,0,0))
   self.goto_piece_xy(row, col)
   self.pen.write(piece, font=("Courier", round(self.square_sidelength*.7), "normal"))

   
   
   
     
     
##########################################################      
class Pieces:
 W_ROOK = u'♖'
 W_KNIGHT = u'♘'
 W_BISHOP = u'♗'
 W_QUEEN = u'♕'
 W_KING = u'♔'
 W_PAWN = u'♙'
 
 B_ROOK = u'♜'
 B_QUEEN = u'♛'
 B_KING = u'♚'
 B_BISHOP = u'♝'
 B_KNIGHT = u'♞'
 B_PAWN = u'♟'
   
 def __init__(self, chessBoard):
   self.chessBoard = chessBoard
   
 def beginning_place(self):
   w_pieces = [self.W_ROOK,
                self.W_KNIGHT,
                self.W_BISHOP,
                self.W_QUEEN,
                self.W_KING, 
                self.W_BISHOP, 
                self.W_KNIGHT,
                self.W_ROOK]
             
   b_pieces = [self.B_ROOK,
                self.B_KNIGHT,
                self.B_BISHOP,
                self.B_QUEEN,
                self.B_KING, 
                self.B_BISHOP, 
                self.B_KNIGHT,
                self.B_ROOK]
               
               
   for i in range(8):
     
     self.chessBoard.put_piece(w_pieces[i], 7, i)
     self.chessBoard.put_piece(self.W_PAWN, 6, i)
     self.chessBoard.put_piece(b_pieces[i], 0, i)
     self.chessBoard.put_piece(self.B_PAWN, 1, i)
     i += 1
     
     
class Input:
 
 
 def __init__(self, chessBoard, window):
   self.window = window
   window.onclick(self.onclick)
   self.board = chessBoard
   
 def onclick(self, x, y):
   if (self.board.x_left < x < self.board.x_left + self.board.next_square * 8 and self.board.y_top > y > self.board.y_top - self.board.next_square * 8):
     print('x:', x, 'y:', y) # debug
   x1 = x - (x % self.board.next_square)
   print(x1)
   y1 = y - (y % self.board.next_square)
   print(y1)

   self.board.color('blue')
   self.board.pen.setheading(90)
   self.board.x = x1
   self.board.y = y1
   self.board.pen.fill(True)
   for i in range(4):
     self.board.pen.forward(board.square_sidelength)
     self.board.pen.right(90)
   self.board.pen.fill(False)
   
 
     
   
   
##########################################################
import turtle
pen = turtle.Turtle()
turtle.tracer(0,0)
#pen.ht()
SQUARE_LENGTH = 59 # DECIDES ON SIZE OF EVERYTHING
window = turtle.Screen()

chessBoard = ChessBoard(pen, SQUARE_LENGTH, SQUARE_LENGTH + 1, (175,102,50), (252, 251, 227), (79, 36, 18))
chessBoard.draw_Board(chessBoard.x, chessBoard.y, chessBoard.dark_color, chessBoard.light_color, chessBoard.border_size, chessBoard.pen, chessBoard.square_sidelength, chessBoard.next_square, chessBoard.border_color, chessBoard.outline_of_board_size, chessBoard.board_length)
pieces = Pieces(chessBoard)
pieces.beginning_place()
INput = Input(chessBoard, window)
turtle.update()
while 0 == 0:
 raw_input
turtle.update()
Reply
#2
self.pen.color(self.color)
# do you mean
self.pen.color(dark_color)
# there is no color defined
Reply
#3
I did that, and it still didnt work. You can go to the website where I'm doing the code and see if u can fix it: https://repl.it/ImDt/53

I'm wondering if it has to do with the way I'm calling pen from my chessBoard class?

I'm wondering if it has to do with the way I'm calling pen from my chessBoard class?
Reply
#4
(Jun-27-2017, 04:42 PM)swaginator Wrote: I did that, and it still didnt work.

Is not helpful at all.

Quote:You can go to the website where I'm doing the code and see if u can fix it

Probably not going to happen. You need to describe the problem you are having in greater detail as to what you get versus what you expect to get, plus any error codes.

You also need to make your indents uniform, you use 1 space at times and 2 spaces at others. Make all indents uniform using 4 spaces.

You should not use "Input" as the name of your class.

DO NOT POST DUPLICATE THREADS, IT DOESN'T HELP YOU AND ONLY ANNOYS THE MODERATORS.
If it ain't broke, I just haven't gotten to it yet.
OS: Windows 10, openSuse 42.3, freeBSD 11, Raspian "Stretch"
Python 3.6.5, IDE: PyCharm 2018 Community Edition
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020