接着来介绍Pygame一些基础的东西。python
Drawing Images with pygame.image.load() and blit()函数
不少游戏都不仅是用简单的画图函数就能够完成的,还须要各类各样的图片(也被称做精灵)pygame能够加载到surface对象上的图片格式有PNG, JPG, GIF和BMP。关于格式的不一样地方能够上网搜索。字体
接着对上篇最后的一段代码里面的东西作些介绍。ui
pygame.image.load()函数返回一个surface对象用于显示图片。这个surface对象和显示窗口对象是分离的,因此咱们必须把图片显示对象复制到显示窗口对象经过blit()方法。spa
方法使用以下:code
DISPLAYSURF.blit(catImg, (catx, caty))对象
第一个参数是要加载的图片对象,第二个参数是有两个整数的元组分别表示图片将要显示的左上角X和Y的坐标值。游戏
字体图片
游戏都须要显示文字,pygame为字体和建立文本提供了一些简单的函数。下面就是一个使用pygame字体的程序。字符串
import pygame, sys from pygame.locals import * pygame.init() DISPLAYSURF = pygame.display.set_mode((400,300)) pygame.display.set_caption("Hello World") WHITE = (255, 255, 255) GREEN = (0, 255, 0) BLUE = (0, 0 ,128) fontObj = pygame.font.Font('freesansbold.ttf', 32) textSurfaceObj = fontObj.render('Hello World!', True, GREEN, BLUE) textRectObj = textSurfaceObj.get_rect() textRectObj.center = (200, 150) while True: DISPLAYSURF.fill(WHITE) DISPLAYSURF.blit(textSurfaceObj, textRectObj) for event in pygame.event.get(): if event.type == QUIT: pygame.quit() sys.exit() pygame.display.update()
将字体显示在屏幕上有六个步骤:
soundObj = pygame.mixer.Sound('beeps.wav') soundObj.play() import time time.sleep(1) soundObj.stop()
pygame.mixer.music.load('backgroundmusic.mp3') pygame.mixer.music.play(-1,0.0) #....some more of your code goes here.. pygame.mixer.music.stop()