import pygame,sys
from pygame.locals import *
pygame.init()
DISPLAYSURF=pygame.display.set_mode((400,300))
pygame.display.set_caption("Hello World!")
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
Note that when you import the pygame module you automatically import all the modules that are in the pygame module as well, such as pygame.images and pygame.mixer.mus
Line 2 is also an import statement. However, instead of the import modulename format, it uses the from modulename import * format. Normally if you want to call a function that is in a module, you must use the modulename.functionname() format before importing the module. However, with from modulename import *, you can skip the modulename.portion and simply use functionname()
The reason we use this form of import statement for pygame.locals is because pygame.locals contains several constant variables that are easy to identify as being in the
pygame.locals module without pygame.locals. in front of them.
Line 4 is the pygame.init() function call, which always needs to be called after importing the pygame module and before calling any other Pygame function. You don’t need to know what this function does, you just need to know that it needs to be called first
in order for many Pygame functions to work.
Line 5 is a call to the pygame.display.set_mode() function, which returns the pygame.Surface object for the window. (Surface objects are described later in this chapter.)
Notice that we pass a tuple value of two integers to the function: (400, 300). This tuple tells the set_mode() function how wide and how high to make the window in pixels. (400, 300) will make a window with a width of 400 pixels and height of 300 pixels. Remember
to pass a tuple of two integers to set_mode(), not just two integers themselves.
sys.exit() (terminates the program)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
的作用:
1. Handles events
2. Updates the game state.
3Draws the game state to the screen
1comes with自带
2module 模块
3several 一些
4framewotk 框架
5hand 支持
6input 输入
7assume 承担
8command 命令
9interface 界面
10pronounced 明显的
11rhyme 押韵
12somewhat 有点
13 even then 尽管那样
14realtime 实际时间
15 GUI = graphical user interface
16blank 空白的
17period 句号
18bar 条
19caption 标题
20set up 建立
21statement 声明
22automatically 自动地
23format 形式
24constant 常量
25variable 变量
26identify as 定义为
27being 要素
28 in font of 在...前面
29object 对象
30pixel 像素
31loop 循环
32evaluate 求..的值
33due to 因为
34execute 执行
35terminate 结束
36comment 注释