According to the plan of last week, start learning pygame today.
1. hello world Program of pygame version.
Code:
[Python]
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Import sys
# Import the pygame module. Line 4 is used to simplify your input. For example, you do not need to add the pygame Module name before the event.
Import pygame
From pygame. locals import *
Def hello_world ():
# Any pygame program needs to execute this sentence for module initialization
Pygame. init ()
# Set the window mode. (680,480) indicates the pixel of the window and (width and height)
# This function returns a Surface object, which is not saved because it is not used in this program.
Pygame. display. set_mode (680,480 ))
# Set the window title
Pygame. display. set_caption ('Hello World! ')
# Loop until window close events are received
While True:
# Event handling
For event in pygame. event. get ():
# Window close events received
If event. type = QUIT:
# Exit
Pygame. quit ()
Sys. exit ()
# Drawing the Surface object God on the screen
Pygame. display. update ()
If _ name _ = "_ main __":
Hello_world ()
#! /Usr/bin/python
#-*-Coding: UTF-8 -*-
Import sys
# Import the pygame module. Line 4 is used to simplify your input. For example, you do not need to add the pygame Module name before the event.
Import pygame
From pygame. locals import *
Def hello_world ():
# Any pygame program needs to execute this sentence for module initialization
Pygame. init ()
# Set the window mode. (680,480) indicates the pixel of the window and (width and height)
# This function returns a Surface object, which is not saved because it is not used in this program.
Pygame. display. set_mode (680,480 ))
# Set the window title
Pygame. display. set_caption ('Hello World! ')
# Loop until window close events are received
While True:
# Event handling
For event in pygame. event. get ():
# Window close events received
If event. type = QUIT:
# Exit www.2cto.com
Pygame. quit ()
Sys. exit ()
# Drawing the Surface object God on the screen
Pygame. display. update ()
If _ name _ = "_ main __":
Hello_world ()
Test:
Key points to be understood:
1. Infinite Loop
Almost every pygame program needs it. In it, you can perform the following events:
A. Handle events
B. Update game status
C. Drawing on the screen
For example:
2. pygame. event. get ()
Used to obtain various keyboard and mouse events.
From Socrates Column