一、安装Python和pygame
1、在Windows环境下,安装Python
略
2、安装pygame,网址: http://pygame.org
使用Python自带pip工具即可快速安装pygame:
python3 -m pip install -U pygame --user
查看是否安装成功,进入Python命令行,看是否能导入pygame:
>>> import pygame
pygame 1.9.4
Hello from the pygame community. https://www.pygame.org/contribute.html
>>>
能成功导入,说明安装成功了
二、pygame基础
pygame框架包含几个模块,。诸如:绘制图形、处理鼠标输入、播放声音等等,要了解更多关于pygame,见 http://pygame.org/docs
下面我们实现一个简单的用pygame输出标题为helloworld的图形框:
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()
实际效果如下:
这个代码主要部分在于那个while循环,我们称之为game loop,一个game loop通常做三件事:
The QUIT Event and pygame.quit() Function