ygame 是 SDL 多媒体库的 Python 包装模块。 它包含 Python 函数和类,允许您使用 SDL 支持播放 CDROM、音频和视频输出以及键盘、鼠标和操纵杆输入。
Pygame 是一个跨平台库,旨在让您可以轻松地用 Python 编写多媒体软件(例如游戏)。 Pygame 需要 Python 语言和 SDL 多媒体库。 它还可以利用其他几个流行的库。
#!/usr/bin/env python
# -*- coding: cp936 -*-
#导入pygame库
import pygame
#导入一些常用的函数和常量
from pygame.locals import *
#向sys模块借一个exit函数用来退出程序
from sys import exit
#指定图像文件名称
background_image_filename = 'sushiplate.jpg'
mouse_image_filename = 'test3.png'
#初始化pygame,为使用硬件做准备
pygame.init()
#创建了一个窗口
screen = pygame.display.set_mode((800, 600), 0, 32)
#设置窗口标题
pygame.display.set_caption("Hello, World!")
#加载并转换图像
background = pygame.image.load(background_image_filename).convert()
mouse_cursor = pygame.image.load(mouse_image_filename).convert_alpha()
#游戏主循环
while True:
for event in pygame.event.get():
if event.type == QUIT:
#接收到退出事件后退出程序
exit()
#将背景图画上去
screen.blit(background, (0,0))
#获得鼠标坐标
x, y = pygame.mouse.get_pos()
#隐藏鼠标
pygame.mouse.set_visible(False)
#计算光标的左上角位置
x-= mouse_cursor.get_width() / 2
y-= mouse_cursor.get_height() / 2
#把光标画上去
screen.blit(mouse_cursor, (x, y))
#刷新一下画面
pygame.display.update()
# -*- coding: cp936 -*-
import pygame
from pygame.locals import *
from sys import exit
pygame.init()
SCREEN_SIZE = (640, 480)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
font = pygame.font.SysFont("arial", 16);
font_height = font.get_linesize()
event_text = []
while True:
event = pygame.event.wait()
event_text.append(str(event))
#获得时间的名称
event_text = event_text[-SCREEN_SIZE[1]/font_height:]
#这个切片操作保证了event_text里面只保留一个屏幕的文字
if event.type == QUIT:
exit()
screen.fill((255, 255, 255))
y = SCREEN_SIZE[1]-font_height
#找一个合适的起笔位置,最下面开始但是要留一行的空
for text in reversed(event_text):
screen.blit( font.render(text, True, (0, 0, 0)), (0, y) )
#以后会讲
y-=font_height
#把笔提一行
pygame.display.update()