Learn Programming with your Kids. python self test

  • 2020-09-16 07:35:12
  • OfStack

Test 1.

1. The two events that the program can respond to are keyboard events and mouse events.

2. The code that handles events is called an event handler

3. Pygame USES THE KEYDOWN event to detect whether the key is pressed.

4. The Pos attribute will indicate the location of the mouse when the event occurs

5. To get the next available event number for the user event, use pygame.NUMEVENTS.

6. To create a timer, use pygame.time.set_timer ().

7. To display text in the Pygame window, you can use the font object.

8. There are three steps to using a font object:

Create 1 font object

Render text, create 1 surface

Move the surface block to the display surface

Go ahead and try 1

1. If instead of hitting the top edge of the racket, the ball hits the left and right sides of the racket, why does it behave strangely? This is because there is 1 collision, so the code tries to make the y direction of the ball (make it go up instead of down). But because the ball is coming from both sides (left or right), it will "collide" with the racket even after the reverse. On the next loop (after 1 frame), it reverses again, so it goes down again, and so on. To solve this problem, there is an easy way: Always set the ball to "up" when it hits the racket (y speed is 1 negative). This is not a perfect solution because it means that even if you touch the left and right sides of the racquet, it will bounce upwards -- which is not quite true! This solves the problem of the ball bouncing back and forth between the two sides of the racket. If you want a more realistic solution, you may need to write a little more code, perhaps add a little more content, and check which side the ball hits before "bouncing" the racket.

2. We have given an example of code that adds randomness to the program, as shown in TIO_CH18_2.py.

Test 2.

1. The file types for storing sound include waveform files (.wav), MP3 (.es67EN3), Ogg Vorbis (.ogg) and Windows media audio files (.wma).

2. Pygame.mixer module is used to play music

3. To set the volume of the Pygame sound object, use the set_volume () method of each sound object.

4, use pygame. mixer. music. set_volume () sets the background music volume.

5, let the music out, can use pygame. mixer. music. fadeout () method. The fade-out time (milliseconds, or one thousandth of a second) is provided as an argument. For example, pygame.mixer.music.fadeout (2000) will fade the sound out in 2 seconds.

Go ahead and try 1

We have provided the code to add the sound to the guessing program on the website, see TIO_ES110en19_1.py.

Test 3.

1. GUl graphic elements have three names, namely control, part and component.

2. To enter a menu, the letters pressed simultaneously with Alt are called hotkeys.

3. PythonCard resource file should end with ES127en.py.

4. GUI using PythonCard can contain the following component types: buttons, check boxes, gauges, lists, radio button sets, sliders, text fields, images, static text, and many others. Look at the Component menu in the Resource editor to see all the component types.

5. To get a component to do something, you need an event handler.

6. Use in PythonCard menu editor & Define hotkeys (and characters).

7. The content of the trim box in PythonCard is always 1 integer.

Go ahead and try 1

1. We have provided on the website the number guessing program completed with PythonCard, see es154EN_CH_1.py and TIO_CH20_1.rsrc.py.

2. To solve the springer problem, select the springer component in the Resource editor. Change the min and max attributes in the property editor. The min attribute should take a small value, such as -1000, and max can be very large, such as 1000000.


Related articles: