python中pygame模块用法实例


Posted in Python onOctober 09, 2014

本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下:

import pygame, sys 
from pygame.locals import * 
 
#set up pygame 
pygame.init() 
 
windowSurface = pygame.display.set_mode((500, 400), 0, 32) 
pygame.display.set_caption("hello, world") 
 
BLACK = (0, 0, 0) 
WHITE = (255, 255, 255) 
RED = (255, 0, 0) 
GREEN = (0, 255, 0) 
BLUE = (0, 0, 255) 
 
basicFont = pygame.font.SysFont(None, 48) 
text = basicFont.render("Hello ,world", True, WHITE, BLUE) 
textRect = text.get_rect() 
 
textRect.centerx = windowSurface.get_rect().centerx 
textRect.centery = windowSurface.get_rect().centery 
 
windowSurface.fill(WHITE) 
 
pygame.draw.polygon(windowSurface, GREEN, ((146, 0),  
(291, 106), (236, 277), (56, 277), (0, 106)))  
 
pygame.draw.line(windowSurface, BLUE, (60, 60), (120,  
60), 4)  
pygame.draw.line(windowSurface, BLUE, (120, 60), (60,  
120))  
pygame.draw.line(windowSurface, BLUE, (60, 120), (120,  
120), 4)  
pygame.draw.circle(windowSurface, BLUE, (300, 50), 20, 0) 
 
pygame.draw.ellipse(windowSurface, RED, (300, 250, 40,  
80), 1)  
 
pygame.draw.rect(windowSurface, RED, (textRect.left - 20,  
textRect.top - 20, textRect.width + 40, textRect.height + 40)) 
 
pixArray = pygame.PixelArray(windowSurface)  
pixArray[480][380] = BLACK  
del pixArray  
 
windowSurface.blit(text, textRect)  
 
pygame.display.update() 
 
while True:  
  for event in pygame.event.get():  
    if event.type == QUIT:  
      pygame.quit()  
      sys.exit()

运行后打出的图片如下:

python中pygame模块用法实例

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python标准库之sqlite3使用实例
Nov 25 Python
Python中使用支持向量机SVM实践
Dec 27 Python
Python通过Pygame绘制移动的矩形实例代码
Jan 03 Python
Django权限机制实现代码详解
Feb 05 Python
python实现植物大战僵尸游戏实例代码
Jun 10 Python
利用Python进行图像的加法,图像混合(附代码)
Jul 14 Python
python生成器/yield协程/gevent写简单的图片下载器功能示例
Oct 28 Python
django正续或者倒序查库实例
May 19 Python
python3 循环读取excel文件并写入json操作
Jul 14 Python
利用python绘制中国地图(含省界、河流等)
Sep 21 Python
Python中使用subprocess库创建附加进程
May 11 Python
Python内置类型集合set和frozenset的使用详解
Apr 26 Python
python根据文件大小打log日志
Oct 09 #Python
python命令行参数解析OptionParser类用法实例
Oct 09 #Python
python测试驱动开发实例
Oct 08 #Python
python批量提交沙箱问题实例
Oct 08 #Python
python求pi的方法
Oct 08 #Python
python实现简单的TCP代理服务器
Oct 08 #Python
python操作CouchDB的方法
Oct 08 #Python
You might like
默默小谈PHP&MYSQL分页原理及实现
2007/01/02 PHP
PHP COOKIE及时生效的方法介绍
2014/02/14 PHP
destoon后台网站设置变成空白的解决方法
2014/06/21 PHP
百度工程师讲PHP函数的实现原理及性能分析(三)
2015/05/13 PHP
TP3.2批量上传文件或图片 同名冲突问题的解决方法
2017/08/01 PHP
Laravel框架中集成MongoDB和使用详解
2019/10/17 PHP
extjs form textfield的隐藏方法
2008/12/29 Javascript
javascript 原型继承介绍
2011/08/30 Javascript
利用Javascript判断操作系统的类型实现不同操作系统下的兼容性
2013/01/29 Javascript
js禁止页面使用右键(简单示例代码)
2013/11/13 Javascript
jquery实现鼠标滑过小图时显示大图的方法
2015/01/14 Javascript
js小数计算小数点后显示多位小数的实现方法
2016/05/30 Javascript
Vue.js自定义指令的用法与实例解析
2017/01/18 Javascript
利用 spin.js 生成等待效果(js 等待效果)
2017/06/25 Javascript
前端主流框架vue学习笔记第一篇
2017/07/26 Javascript
基于Node.js实现压缩和解压缩的方法
2018/02/13 Javascript
Windows上node.js的多版本管理工具用法实例分析
2019/11/06 Javascript
Vue ElementUI实现:限制输入框只能输入正整数的问题
2020/07/31 Javascript
vscode 使用Prettier插件格式化配置使用代码详解
2020/08/10 Javascript
[02:36]DOTA2英雄基础教程 斯拉克
2013/11/29 DOTA
[51:11]2014 DOTA2国际邀请赛中国区预选赛5.21 LGD-CDEC VS DT
2014/05/22 DOTA
python列表与元组详解实例
2013/11/01 Python
python之消除前缀重命名的方法
2018/10/21 Python
解决python ogr shp字段写入中文乱码的问题
2018/12/31 Python
Python pandas用法最全整理
2019/08/04 Python
基于HTML5新特性Mutation Observer实现编辑器的撤销和回退操作
2016/01/11 HTML / CSS
如何让pre和textarea等HTML元素去掉滚动条自动换行自适应文本内容高度
2019/08/01 HTML / CSS
劳动工资科岗位职责范本
2014/03/02 职场文书
大学生2014全国两会学习心得体会
2014/03/10 职场文书
文明生主要事迹
2014/05/25 职场文书
退学证明范本3篇
2014/10/29 职场文书
公司开会通知
2015/04/20 职场文书
学校计划生育责任书
2015/05/09 职场文书
2016年3月份红领巾广播稿
2015/12/21 职场文书
在python中实现导入一个需要传参的模块
2021/05/12 Python
基于JavaScript实现年月日三级联动
2021/06/22 Javascript