IIPP Mini Project (iii) "Stopwatch:the Game"

Source: Internet
Author: User
Tags polyline

0 Description of this week's project

This time the blog is Coursera's Iipp course the third week of the mini-project implementation, the basic requirement is to do a stopwatch, can start can stop reset, the higher the requirement is on the basis of this stopwatch to complete two small games, but in view of the two games are all involved in the " Press one of the keys of the keyboard the frame will react immediately ", and this step how to implement the course has not been said, so this blog for the time being not to realize the two small games, simple to achieve by button control of the stopwatch.

1 Introduction to Canvas

to facilitate my own review of the follow-up process, I will organize the canvas as follows. This article in addition to the reference Coursera video, there are Simpleguics2pygame-canvas, but the personal feeling that this document is not good enough to write, not better than the codesculptor in the clear, But Google is not a wall, it is not easy to login codesculptor, so you can only combine video and this document to write programs.

I feel really at a glance when Mr. Joe opens the Codesculptor canvas user interface in the video.

In simple terms, the member functions provided by the canvas class can do the following:

(1) writing (canvas.draw_text);

(2) Draw straight line (canvas.draw_line);

(3) Draw the polyline (Canvas.draw_polyline);

(4) Draw closed polygon (Canvas.draw_polygon);

(5) Draw a circle (canvas.draw_circle);

(6) Load external image (Canvas.draw_image).

and each member function to fill in what kind of parameters, you have to consult Simpleguics2pygame-canvas, although this document how to use these functions are not clear, but to check a parameter is still possible.

Here I give a code for the experiment, which contains all the member functions of the above canvas class (except Canvas.draw_image), in order to facilitate the display of the effect of the member functions, I use different colors to draw these shapes.

1 ImportSimpleguics2pygame.simpleguics2pygame as Simplegui2 3 ## # event Handler # # #4 defDraw (Canvas):5Canvas.draw_text ("hello!", [100,100],24,"Red")#text6Canvas.draw_line ([3,3],[20,40],3," White")#Line7Canvas.draw_polyline ([(50,80), (60,10), (40,44)],3,"Green")#polyline8Canvas.draw_polygon ([(80,80), (110,120), (120,160)],3,"Blue")#closed graphic9Canvas.draw_circle ([60,60],40,3,"Yellow")#Draw a circleTen      One ## # Create a Frame # # # Aframe = Simplegui.create_frame ("stopwatch:the Game", 200,200)#200*200 to canvas size -  - ## # Register event Handlers # # # the Frame.set_draw_handler (Draw) -  - ## # start Frame # # # -Frame.start ()

The basic process is roughly described as follows: At the moment the frame is started, the canvas is started, and the callback function that hooks the canvas is called Draw (). Set what to draw in the draw () function, and what is displayed on the canvas. The final canvas appears as follows:

As for Canvas.draw_image, the visual is behind the mini-project will talk about, here I still do not know how to use it to load the image, the first left no matter.

2 Timers Introduction

The timer is the second key link in this small project, and the time change is precisely produced by it.

In particular, let's look at the Codesculptor manual that Mr. Scott opened:

You can find that when you create a timer, you need to give two parameters, one is the time interval-that is, the callback function Timer_handler is called every time, and the other is the callback function waiting to be called.

Also, if you want to use a timer, you need to open it in addition to creating it by starting the timer, which is Timer.start (), before starting the frame. Here is a bit like a single-chip timer counter, need to turn on the timer to operate, otherwise the timer is not working, as if the switch was turned off.

Below I give the code that constantly updates the current number of seconds on Canva:

1 ImportSimpleguics2pygame.simpleguics2pygame as Simplegui2 3 ## # global variable # # #4Message ="0"5Interval = 10006 7 ## # event Handler # # #8 defDraw (Canvas):#callback function for Canva9Canvas.draw_text (message,[100,100],24,"Red")Ten      One defTick ():#the callback function of the timer A     Globalmessage -num =int (message) -num + = 1 theMessage =str (num) -      - ## # Create a Frame # # # -frame = Simplegui.create_frame ("stopwatch:the Game", 200,200)#200*200 to canvas size +  - ## # Register event Handlers # # # + Frame.set_draw_handler (Draw) ATimer =Simplegui.create_timer (Interval,tick) at  - ## # start Frame # # # - Timer.start () -Frame.start ()

This means that every interval (1 seconds) will call the timer's callback function tick. The callback function tick is responsible for changing the value of the message, and the canvas can be automatically updated with its super-fast self-refresh mechanism.

So much for a stopwatch, the next thing to do is to refine it further, plus the start and stop and reset buttons.

IIPP Mini Project (iii) "Stopwatch:the Game"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.