Python游戏编程入门第二章 笔记 第十页结束

本文介绍了一个使用Python Pygame库创建简单窗口程序的例子。通过导入必要的Pygame模块,并初始化Pygame,设置窗口尺寸,实现了基本的窗口显示及退出事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 注释

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值