Python: pygame game programming tour 1 (Hello World)

Source: Internet
Author: User

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
 

Related Article

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.