ZhangZhihui's Blog  

A typical event-driven program is not doing anything most of the time, and its functionality is only triggered by external signals, such as user input or timer alarms (think how an application like a calculator or a text editor works).

 

The turtle is designed to operate in an event-driven environment. The call to turtle.done() actually starts a so-called event loop, which enables proper event handling. For this reason, a well-designed turtle-based application should quickly start an event loop, and only react to incoming events. This type of program structure requires quite a different thinking style.

 

 

Let me pause for a moment to appreciate how much the turtle helps us to implement moving onscreen objects. 
Suppose our task is to move a black circle over a white background. Each step of this process breaks down into three actions: delete the circle by turning each of its pixels white, update its coordinates, and draw the circle at its new position.
An obvious flaw of this simple method is an inevitable flickering effect it produces. By deleting the circle before drawing it elsewhere, we create situations when nothing is drawn on the screen at all. As short as these periods may be, they still make the circle visibly flicker when moving. In addition, simple removal of onscreen objects might work only if the background is empty; if the circle is moving over a graphical pattern, it has to be preserved somehow.

This difficulty can be resolved with a popular technique of double bufferingAn invisible virtual screen (buffer) is used to draw the whole onscreen scene from scratch on each animation frame. When the scene is ready, it is transferred to the main screen at once. Thus, there is no need to save and restore the screen areas hidden behind the moving objects, since they are redrawn on every frame. This method also removes flickering but is still prone to tearing. 

Computer monitor updates screen content at a certain fixed rate (60 times per second is typical for conventional non-gaming monitors; gaming devices have refresh rate of 100 times per second or higher). While the virtual screen is transferred “at once”, this process still takes time, and if screen refresh happens when half of virtual screen content is already on the main screen, you’ll see a picture made of this new half and the previously drawn frame. This issue is resolved by synchronizing frame drawing with screen refresh rate (a technique known as VSync).

The turtle at least implements proper double buffering, so we will never have to deal with tearing or think what happens when a turtle crosses a patterned background or other moving objects. In addition, we don’t have to do anything to restore screen content when the main application window is minimized and restored or temporarily covered by another window.

 

Python turtle library comes with six shapes (“classic”, “arrow”, “turtle”, “triangle”, “circle”, and “square”), but this list can be extended with custom user-created shapes.

          

 

Likewise, it is possible to adjust turtle size. This functionality is slightly less straightforward to use, however. All built-in turtle shapes fit into a 20×20 pixel square. This fact can be checked from a Python shell:

>>> import turtle
>>> turtle.Screen()._shapes['square']._data
((10, -10), (10, 10), (-10, 10), (-10, -10))

The square shape is defined as a list of connected vertices. As it can be seen, each vertex coordinate in the list lies within the range [-10, 10]. To make a turtle larger or smaller, we have to specify “stretch factors” for its shape:
m = turtle.Turtle()
m.shapesize(2)  # make the turtle twice larger
Thus, if we pass size to shapesize(), the actual shape would fit into a 20×size pixel-wide square.

posted on   ZhangZhihuiAAA  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
历史上的今天:
2023-08-02 Linux - check CPU and memory information
 
点击右上角即可分享
微信分享提示