鱼C论坛

 找回密码
 立即注册
查看: 1860|回复: 0

关于 turtle,screen.clickon() 之后没有反应

[复制链接]
发表于 2022-4-20 19:22:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import turtle
  2. import random

  3. SQUARE_LENGTH = 60
  4. OUTLINE = 5 # the width of the broad
  5. INTERVAL_LENGTH = 10 # the length between two squares
  6. ALL_COLOR = ['red', 'yellow', 'blue', 'purple', 'green']
  7. g_all_square = [] # store the color of every square
  8. g_all_center = []
  9. g_ifclick = True
  10. g_current_square = [] #append all to this list, final choice is g_current_square[-1]
  11. g_all_frame_pen = [] # in this llist, 0,1,2,3,4 are frame of color set, after this all chosen square in board


  12. def init_screen():
  13.     global screen
  14.     screen = turtle.Screen()
  15.     screen.setup(800, 800, 0, 0)
  16.     #screen.tracer(0)
  17.     #screen.tracer(0)
  18.     # set screen refersh to manual mode
  19.     draw_board()
  20.     draw_color_set()
  21.    
  22.    

  23. def draw_filled_square(p_start_x, p_start_y, p_lenth, p_fill_color):
  24.     #turtle.delay(None)
  25.     pen = turtle.Turtle()
  26.     #pen.speed(0)
  27.     #pen.hideturtle()
  28.     pen.penup()
  29.     pen.setx(p_start_x)  # set the start point's x coordinate, start point in the left bottom
  30.     pen.sety(p_start_y)  # set the start point's x coordinate
  31.     pen.pendown()
  32.     pen.shape('square')
  33.     streth_wid = (p_lenth/2) / 10
  34.     pen.shapesize(streth_wid,streth_wid, 0)
  35.     #pen.pen(p_fill_color, p_fill_color)
  36.     pen.color(p_fill_color)
  37.     #pen.pencolor(p_fill_color)
  38.     #pen.fillcolor(p_fill_color)
  39.     #pen.begin_fill()
  40.     #for i in range(4):
  41.     #    pen.forward(p_lenth)
  42.     #    pen.left(90)
  43.     #pen.end_fill()

  44. def draw_frame(p_start_x, p_start_y, p_lenth):
  45.     pen = turtle.Turtle()
  46.     #pen.speed(5)
  47.     pen.penup()
  48.     pen.setx(p_start_x)  # set the start point's x coordinate, start point in the left bottom
  49.     pen.sety(p_start_y)  # set the start point's x coordinate
  50.     pen.pendown()
  51.     pen.pensize(5)
  52.     for i in range(4):
  53.         pen.forward(p_lenth + 5)
  54.         pen.left(90)
  55.     g_all_frame_pen.append(pen)
  56.     #pen.down()  

  57. def draw_board():
  58.     #turtle.tracer(False)
  59.     # first start point is (0,0)
  60.     x0 = -140
  61.     y0 = -140
  62.     # use colums and lines to define the start point of each quare
  63.     for column in range(5):  #5 columns in totle
  64.         # adjust the y coordinate of start point
  65.         y = y0 + column*(SQUARE_LENGTH + INTERVAL_LENGTH)
  66.         line_square_color = [] # to store the color of square in this line
  67.         line_center = []
  68.         for row in range(5):  #5 rows in totle
  69.             # adjust the x coordinate of start point
  70.             # each time add square_length + interval length
  71.             x = x0 + row*(SQUARE_LENGTH + INTERVAL_LENGTH)
  72.             color = choose_color() # choose_color() return the name of color(string)
  73.             line_square_color.append(color)
  74.             line_center.append([x,y])
  75.             draw_filled_square(x, y, SQUARE_LENGTH, color)
  76.         g_all_square.append(line_square_color)   
  77.    
  78. # randomly choose the color of square in board           
  79. def choose_color():
  80.     color_index = random.randint(0,4) # list has index 0,1,2,3,4
  81.     color = ALL_COLOR[color_index]
  82.     return color
  83.       
  84. def draw_color_set():
  85.     # set the initial point(left bottom of the square)
  86.     x0 = -170
  87.     y0 = -215
  88.     # only 1 column with 5 rows
  89.     for line in range(5):
  90.         # adjust the x coordinate of start point
  91.         # each time add square_length + interval length
  92.         x = x0 + line*(SQUARE_LENGTH + + INTERVAL_LENGTH)
  93.         color = ALL_COLOR[line] # the color index is the same as the line
  94.         draw_filled_square(x, y0, SQUARE_LENGTH, color)
  95.         draw_frame(x-30-2.5, y0-30-2.5, SQUARE_LENGTH)

  96. # show the starting screen
  97. def start_game():
  98.     turtle.tracer(False)
  99.     init_screen()
  100.     turtle.update()
  101.     #turtle.exitonclick()

  102. def get_position(x,y):
  103.     click_position = [x,y]
  104.     return click_position
  105.    
  106. #return a list containing the click position
  107. #def select_tile():
  108.     #turtle.listen()
  109.     #mouse = turtle.Turtle()
  110.     #click_position = screen.onclick(get_position)
  111.     #return click_position

  112. #check whether the point in the color square is the border
  113. # if it is border, not change
  114. def check_point_color_square(p_clicked_point):
  115.     global g_ifclick
  116.     # reset the original point to (-170, 170) to make the caculation easier
  117.     x = p_clicked_point[0]+ 170
  118.     y = p_clicked_point[1] + 170
  119.    
  120.     # view (square_length + interval_length) as a period(T)
  121.     # x = T*row + row_remain
  122.     # row stands for the number of entire square in the left of x
  123.     # then solve the equation, we get next two lines
  124.     row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
  125.     row_remain = x-row*(SQUARE_LENGTH + INTERVAL_LENGTH)
  126.     # the same as x   
  127.     column = y // (SQUARE_LENGTH + INTERVAL_LENGTH)
  128.     column_remain = y-column*(SQUARE_LENGTH + INTERVAL_LENGTH)
  129.    
  130.     #check whether the user click in the square or the border
  131.     if row_remain > SQUARE_LENGTH or column_remain > SQUARE_LENGTH: #the user click the white space
  132.         g_ifclick = True # need to click again, will not change

  133.     else: #click inside the square
  134.         g_current_square.append([row,column])
  135.         center = g_all_center[column][row]
  136.         # undo last black frame
  137.         if len(g_all_frame_pen) > 5:
  138.             last_pen = g_all_frame_pen[-1]
  139.             last_pen.undo()
  140.         draw_frame(center[0], center[1], SQUARE_LENGTH)
  141.         #pass

  142. # get the color of square in board
  143. def get_color(p_coordinate):
  144.     row = p_coordinate[0]
  145.     column = p_coordinate[1]
  146.     color = g_all_square[column, row]
  147.     return color

  148. # if click the color set, change g_ifclick = False, get out of while
  149. # other situation, g_ifclick = True, remain in while
  150. def check_point(p_clicked_point):
  151.     global g_ifclick
  152.     x = p_clicked_point[0]
  153.     y = p_clicked_point[1]
  154.     if -245 <= y <= -185 and -200 <= x <= 140: # the area of color_set
  155.         if -140 <= x <= -130 or \
  156.             -70 <= x <= -60 or \
  157.             0 <= x <= 10 or \
  158.             70 <= x <= 80:
  159.             g_ifclick = True #clicked the broad
  160.             
  161.         else: #click the square in the color_set
  162.             if len(g_current_square) != 0:   
  163.                 g_ifclick = False # no more click, begin flipping
  164.             else: # havent chosen the square in the board to flipping
  165.                 g_ifclick = True
  166.         
  167.     elif -170 <= x <= 170 and -170 <= y <= 170: # clicked the color square
  168.         check_point_color_square()
  169.         
  170.     else: # in white space
  171.         g_ifclick = True
  172.    


  173.    
  174. #def click_empty_space():
  175.     #click = True
  176.     #return click

  177. # check if the color is the same, if same, then change
  178. def if_change_color(p_row, p_column, p_final_color):
  179.     if_change = False
  180.     inital_color = g_all_square[p_column][p_row]
  181.     if inital_color == p_final_color:
  182.         if_change = True
  183.     else:
  184.         if_change = False
  185.    
  186.     return if_change
  187.    
  188.    
  189. def x_to_row(p_x):
  190.     # reset the original point (0,0) to (-170, 170) to make the caculation easier
  191.     x = p_x + 170
  192.     # view (square_length + interval_length) as a period(T)
  193.     # x = T*row + row_remain
  194.     # row stands for the number of entire square in the left of x
  195.     # then solve the equation, we get next two lines
  196.     row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
  197.     return row  
  198.    
  199. def y_to_column(p_y):
  200.     # reset the original point (0,0) to (-170, 170) to make the caculation easier
  201.     y = p_y + 170
  202.     # the same as x
  203.     column = y // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
  204.     return column

  205. #def row_to_x(p_row):
  206.     # can be viewed as arithmetic progression
  207.     # x0 = -140; d = square_length + interval_length = 60 + 10 = 70
  208.     # x = x0 + d*(n-1); n stands for row
  209.     # x = 70n - 210
  210.     x = 70*p_row - 210
  211.     return x

  212. def get_final_color(p_coordinate):
  213.     x = p_coordinate[1] #all y makes no different in square
  214.     x = x + 200 # change the original point(0,0) t0 (-200, -245) to make calculation easier
  215.     row = x // (SQUARE_LENGTH + INTERVAL_LENGTH) # // get the round number
  216.     final_color = ALL_COLOR[row]
  217.     return final_color

  218. def flipping(p_row, p_column, p_final_color):
  219.     #check whether the color is same
  220.     #if same, then flipping
  221.     if_change = if_change_color(p_row, p_column, p_final_color)
  222.    
  223.     if if_change == True:
  224.         center_coordinate = g_all_center[p_column][p_row]
  225.         center_x = center_coordinate[0]
  226.         center_y = center_coordinate[1]
  227.         draw_filled_square(center_x, center_y, SQUARE_LENGTH, p_final_color)
  228.         
  229.         flipping(p_row-1, p_column, p_final_color) # flip the left square
  230.         flipping(p_row+1, p_column, p_final_color) # flip the right square
  231.         flipping(p_row, p_column-1, p_final_color) # flip the below square
  232.         flipping(p_row, p_column+1, p_final_color) # flip the upper square
  233.         
  234.     else: #color is different, do not change
  235.         pass
  236.         

  237. #start_game()
  238. #print(g_all_square)
  239. #clicked_point = screen.onclick(get_position)
  240. screen = turtle.Screen()
  241. screen.setup(800, 800, 0, 0)
  242. turtle.tracer(False)
  243. draw_board()
  244. draw_color_set()
  245. turtle.update()
  246. clicked_point = screen.onclick(get_position)

  247. if clicked_point != None:
  248.     check_point(clicked_point)
  249. if g_ifclick == False:
  250.     chosen_square = g_current_square[-1] #the last chosen square in board
  251.     clicked_row = x_to_row(chosen_square[0])
  252.     clicked_column = y_to_column(chosen_square[1])
  253.     final_color = get_final_color(clicked_point) # the clicked point must in the color set to make g_ifclick = False
  254.     flipping(clicked_row, clicked_column, final_color)

  255. turtle.mainloop()
复制代码


关于 turtle,screen.clickon() 之后没有反应
谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-29 04:27

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表