一、pygame简介
Pygame 是一组用来开发游戏软件的 Python 程序模块,基于 SDL 库的基础上开发。允许你在 Python 程序中创建功能丰富的游戏和多媒体程序,Pygame 是一个高可移植性的模块可以支持多个操作系统。用它来开发小游戏非常适合
二、pygame的使用
第一步是把pygame导入到Python程序中,
import pygame
然后需要引入pygame中所有常量
from pygame.locals import *
在经过初始化以后,我们就可以正常的使用pygame了
pygame.ini()
通常来说我们需要创建一个窗口,方便我们与程序就行交互,下面创建一个600 x 500的窗口
screen=pygame.display.set_mode((600,500))
1、打印字体
pygame支持使用pygame.font将文打印到窗口,要打印一个文本的话首先需要创建一个文本对象
myfont = pygame.font.Font(None,60)
这个文本是个重量级的进程,比较耗费时间,常用的做法是先在内存中创建文本对象,然后将文本当做一个图像来渲染
white= 255,255,255 blue = 0,0,200 textImage = myfont.render("hello Pygame",True,white)
textImage对象可以使用screen.blit()来绘制,上面的render函数第一个参数是文本,第二个参数是抗锯齿字体,第三个参数是一个颜色值(RGB值)
要绘制文本,通常的过程是清屏,绘制,刷新
screen.fill(blue) screen.blit(textImage,(100,100)) pygame.display.update()
如果此时运行程序的话,会出现一个窗口一闪而过,为了让他长时间显示,需要把他放入一个循环中
import pygame from pygame.locals import * import sys white = 255,255,255 blue = 0,0,200 pygame.init() screen = pygame.display.set_mode((600,400)) myfont = pygame.font.Font(None,60) textImage = myfont.render("Hello World",True,white) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill(blue) screen.blit(textImage,(100,100)) pygame.display.update()
执行结果:
pygame除了能打印字体,仍然可以绘制各种图像
2、绘制一个圆形
使用pygame.draw.cicle()方法,该方法需要传递圆的大小,颜色和参数
import pygame,sys pygame.init() screen = pygame.display.set_mode((600,400)) #设置圆形的位置和移动的速度变量 pos_x = 300 pos_y = 250 vel_x = 2 vel_y = 1 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((0, 0, 200)) #移动圆形 pos_x += vel_x pos_y += vel_y #让圆形保持在窗口内 if pos_x > 500 or pos_x < 0: vel_x = -vel_x if pos_y > 400 or pos_y <0: vel_y = -vel_y color = 255, 255, 0 position = 300, 250 radius = 80 # 半径 width = 0 pos = pos_x,pos_y #print(pos) pygame.draw.circle(screen,color,pos,radius,width) pygame.display.update()
执行结果
3、绘制一个矩形
绘制一个可以移动的矩形,而非简单的显示在屏幕中间
首先需要设置pos_x,pos_y两个变量来记录矩形的位置信息,然后在创建一堆速度变量(vel_x,vel_y),在while循环中不断变化矩形的位置,当矩形移动到屏幕边缘的时候,将速度变量取返,这样就可以产生碰撞的效果
from pygame.locals import * import pygame import sys pygame.init() screen = pygame.display.set_mode((600,400)) title = pygame.display.set_caption("Drawing Rectangles") #记录矩形的位置信息,记录移动速度的位置信息 pos_x = 300 pos_y = 250 vel_x = 2 vel_y = 1 while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((0,0,200)) #移动矩形 pos_x += vel_x pos_y += vel_y #使矩形保持在窗口内 if pos_x > 500 or pos_x <0: vel_x = -vel_x if pos_y >400 or pos_y < 0: vel_y = -vel_y #绘制矩形 color = 255,255,0 width = 0 pos = pos_x,pos_y,100,100 pygame.draw.rect(screen,color,pos,width) pygame.display.update()
执行结果:
4、绘制线条
使用pygame.draw.line()方法,该方法需要传递起点和终点,还有线条的颜色和宽度
import pygame import sys pygame.init() screen = pygame.display.set_mode((600,400)) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((0,0,255)) color = 255,255,0 width = 5 pygame.draw.line(screen,color,(100,100),(500,300),width) pygame.display.update()
执行结果:
5、绘制弧形
弧形是圆形的一部分,可以使用pygame.draw.arc方法绘制它,由于这个形状相对比较复杂,需要用到的知识比前面的知识要多
首先,需要一个矩形来表示弧形的边界(需提供矩形左上角的位置,宽度和高度)弧形就绘制在这个矩形当中
然后需要提供弧形的起始角度和结束角度,平常我们在生活中都是用度为单位衡量一个角度,但在几何三角形中,通常使用的单位是弧度
将角度转换成弧度的函数是math.redians()它包含在math库中,因此使用之前一定要引入math库
import math import pygame from pygame.locals import * import sys pygame.init() screen = pygame.display.set_mode((600,500)) title = pygame.display.set_caption("Drawing Arcs") while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() screen.fill((0,0,200)) #绘制弧形 color = 255,0,255 position = 200,150,200,200 start_angle = math.radians(0) stop_angle = math.radians(180) print(start_angle,stop_angle) width =8 pygame.draw.arc(screen,color,position,start_angle,stop_angle,width) pygame.display.update()
执行结果: