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挑选文件夹里宽大于300图片的方法
Mar 05 Python
对于Python编程中一些重用与缩减的建议
Apr 14 Python
Python批量转换文件编码格式
May 17 Python
Python中zfill()方法的使用教程
May 20 Python
Python实现简单多线程任务队列
Feb 27 Python
Python实现读写sqlite3数据库并将统计数据写入Excel的方法示例
Aug 07 Python
人脸识别经典算法一 特征脸方法(Eigenface)
Mar 13 Python
详解python里的命名规范
Jul 16 Python
python实现查找所有程序的安装信息
Feb 18 Python
python os模块常用的29种方法使用详解
Jun 02 Python
pytorch 查看cuda 版本方式
Jun 23 Python
Pytorch之扩充tensor的操作
Mar 04 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
为什么那些咖啡爱好者大多看不上连锁咖啡店?
2021/03/06 咖啡文化
PHP安装攻略:常见问题解答(三)
2006/10/09 PHP
PHP mysql与mysqli事务使用说明 分享
2013/08/17 PHP
一个基于phpQuery的php通用采集类分享
2014/04/09 PHP
THINKPHP支持YAML配置文件的设置方法
2015/03/17 PHP
zen cart实现订单中增加paypal中预留电话的方法
2016/07/12 PHP
php通过pecl方式安装扩展的实例讲解
2018/02/02 PHP
PHP删除数组中指定值的元素常用方法实例分析【4种方法】
2018/08/21 PHP
PHP基于GD2函数库实现验证码功能示例
2019/01/27 PHP
PHP htmlspecialchars()函数用法与实例讲解
2019/03/08 PHP
js禁止小键盘输入数字功能代码
2011/08/01 Javascript
ExtJS4如何自动生成控制grid的列显示、隐藏的checkbox
2014/05/02 Javascript
jQuery版本升级踩坑大全
2016/01/12 Javascript
jQuery遍历DOM节点操作之filter()方法详解
2016/04/14 Javascript
AngularJs Forms详解及简单示例
2016/09/01 Javascript
使用 bootstrap modal遇到的问题小结
2016/11/09 Javascript
原生javascript上传图片带进度条【实例分享】
2017/04/06 Javascript
JS字符串去除连续或全部重复字符的实例
2018/03/08 Javascript
Vue递归实现树形菜单方法实例
2018/11/06 Javascript
vue+element tabs选项卡分页效果
2020/06/29 Javascript
JavaScript修改注册表实例代码
2020/01/05 Javascript
vue2.* element tabs tab-pane 动态加载组件操作
2020/07/19 Javascript
vue实现选中效果
2020/10/07 Javascript
python获得一个月有多少天的方法
2015/06/04 Python
python3 tkinter实现点击一个按钮跳出另一个窗口的方法
2019/06/13 Python
pytorch 模拟关系拟合——回归实例
2020/01/14 Python
Django模板之基本的 for 循环 和 List内容的显示方式
2020/03/31 Python
python super()函数的基本使用
2020/09/10 Python
利用Python实现自动扫雷小脚本
2020/12/17 Python
js实现移动端H5页面手指滑动刻度尺功能
2017/11/16 HTML / CSS
花园仓库建筑:Garden Buildings Direct
2018/02/16 全球购物
数控专业毕业生求职信范文
2013/09/21 职场文书
办公室综合文员岗位职责范本
2014/02/13 职场文书
购房协议书
2014/04/11 职场文书
离婚财产分割协议书
2015/08/11 职场文书
MySQL系列之九 mysql查询缓存及索引
2021/07/02 MySQL