def destroy(self): super().destroy() turtle.turtles().remove(self.water_battery_turtle) turtle.turtles().remove(self.food_battery_turtle) turtle.turtles().remove(self.orientation_turtle) for t in itertools.chain(self.sensor_turtles, *self.sensor_sig_turtles): turtle.turtles().remove(t)
def draw_grid(): """ turtle这个库在函数内创建的turtle实例不会被销毁,所以代码容易写的变量名到处飞 TODO 做动画/游戏还是推荐Rust的ggez库 """ t_row = turtle.clone() turtle.tracer(False) # 将光标移动到画板的左上角 t_row.setpos(-5 * GRID_WIDTH, 5 * GRID_WIDTH) t_row.pendown() t_col = t_row.clone() t_col.right(90) for _ in range(ROWS): t_row.fd(GRID_WIDTH) t_clone = t_row.clone() t_clone.right(90) t_clone.fd(10 * GRID_WIDTH) t_col.fd(GRID_WIDTH) t_clone = t_col.clone() t_clone.left(90) t_clone.fd(10 * GRID_WIDTH) turtle.tracer(True) # 释放无用turtle对象内存 del turtle.turtles()[1:] # del c只会删除c本身
def create_assets(self): self.create_pens() self.create_border() self.create_player() self.create_particles() self.draw_score() for num in range(3, 0, -1): turtle.ontimer(self.countdown(num), 1000) for t in turtle.turtles(): t.setundobuffer(None)
def menu_click( x, y ): """ Figures out which 'button' was pressed and draws the appropriate image. """ # The turtle screen is a "Singleton Object", which means there is only one. # When this line of code is called in main(), it creates the new screen. # When this line of code is called here, it gets the already created screen. screen = turtle.Screen() # Don't listen for more clicks while drawing the current shape. screen.onclick( None ) # Shift and invert the y-coordinate so it is a positive number with 0 at the top of the screen. # This way, when divided by BUTTON_SIZE, it can be used as an index into the OPTIONS list. y = HEIGHT - ( y + HEIGHT // 2 ) option = OPTIONS[ int( y / BUTTON_SIZE ) ] # Since this function can only receive two parameters from the system, # we have to get our faithful turtle, tom, from the list of all turtles. tom = turtle.turtles()[ 0 ] # First in the list since there is only one. tom.clear() tom.penup() tom.home() tom.pensize( 1 ) # Figure out which shape is being requested and draw it. if option == "Sierpinski Triangle": draw_sierpinski_triangle( tom ) elif option == "Sierpinski Carpet": draw_sierpinski_carpet( tom ) elif option == "Sierpinski-ish Circles": draw_sierpinski_circles( tom ) elif option == "H Tree": draw_h_tree( tom ) elif option == "Leafy Tree": draw_leafy_tree( tom ) elif option == "Koch Snowflake": draw_koch_snowflake( tom ) else: turtle.bye() # Closes the turtle screen. sys.exit() # Actually stops the program. if DRAW_FAST: # If tracing is turned off, call update to show the image drawn. turtle.update() # Pause for a few seconds to admire the pretty picture, then show the menu again. # (Skip the pause of the user impatiently presses the Escape key.) if not key_press.escape: time.sleep( 3 ) show_menu( tom ) # Start listening for clicks again. screen.onclick( menu_click )
def switchScreen(fun): turtle.resetscreen() button.buttons.clear() for t in turtle.turtles(): t.ht() fun()
turtle.speed('fast') turtle.penup() turtle.hideturtle() # def main(): # setup() # run_turtle_timer_event() if __name__ == '__main__': setup() draw_grid() # pos = init_time() # 传坐标 防"当前时间"被clear # 清理上面函数产生的turtle对象 del turtle.turtles()[1:] # time_writer = turtle.clone() # time_writer.setpos(pos) fishes = [] for _ in range(6): fishes.append(Fish()) # 定时更新部分 https://zhuanlan.zhihu.com/p/32094690 # 用死循环不断调用定时器会一直占用线程 # 而且经实验,若死循环内除定时器外还有别的代码 # 会导致定时器的间隔interval变大,比如设好1秒更新的时间加入鱼游动的代码后2秒才变一次 # # 另外一种方法,就是在回调函数中,创建定时器并启动,形成递归调用 # update_time()
wn.bgcolor("orange") print(turtle.window_height(), turtle.window_width()) #turtle.setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) #turtle.setup(width=_CFG["width"], height=_CFG["height"], startx=_CFG["leftright"], starty=_CFG["topbottom"]) tess = turtle.Turtle() tess.shape("turtle") tess.color("blue") wn.textinput("NIM", "Name of first player:") tid_i_sekunder = wn.numinput("Poker", "Your stakes:", 1000, minval=10, maxval=10000) tid_i_sekunder = int(tid_i_sekunder) tess.penup() # This is new tess.pendown() print(turtle.turtles()) #turtle.screensize(canvwidth=None, canvheight=None, bg=None) size = 1 turtle.colormode(255) for i in range(tid_i_sekunder): print(i) turtle.delay(100) tess.stamp() # Leave an impression on the canvas tess.pencolor(random.randint(0,255), 160, 80) size = size + 3 # Increase the size on every iteration tess.forward(size) # Move tess along tess.right(24) # ... and turn her tess.shapesize(i*0.1+1,i*0.1+1) tess.color(random.randint(0,255),random.randint(0,255),random.randint(0,255)) #winsound.PlaySound("SystemExit", winsound.SND_ALIAS) #winsound.PlaySound("*", winsound.SND_ALIAS)
def destroy(self): turtle.turtles().remove(self.artist) self.artist = None # Prevent turtle from being used anymore
def destroy(self): super().destroy() for t in self.sensor_sig_turtles: turtle.turtles().remove(t)
def get_turtle_pos(self,sqr_str): pos = self.chess_coord[sqr_str] for p in turtle.turtles(): if p.position() == (pos[0],pos[1]): return p
import turtle turtle.turtles() turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.right(90) turtle.forward(100) turtle.forward(100) turtle.right(90) turtle.forward(100)
def get_turtle(self): self.t = turtle.turtles()[0] return (self.t)