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合并多个装饰器小技巧
Apr 28 Python
详解Python的Django框架中的templates设置
May 11 Python
python中datetime模块中strftime/strptime函数的使用
Jul 03 Python
python pandas库的安装和创建
Jan 10 Python
python石头剪刀布小游戏(三局两胜制)
Jan 20 Python
Python-Tkinter Text输入内容在界面显示的实例
Jul 12 Python
Python发送邮件的实例代码讲解
Oct 16 Python
Python模块/包/库安装的六种方法及区别
Feb 24 Python
简单了解python调用其他脚本方法实例
Mar 26 Python
Django路由层URLconf作用及原理解析
Sep 24 Python
几款Python编译器比较与推荐(小结)
Oct 15 Python
Python爬虫破解登陆哔哩哔哩的方法
Nov 17 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时的知识积累总结
2013/06/07 PHP
PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径
2014/06/30 PHP
php实现学生管理系统
2020/03/21 PHP
Laravel 5.5 的自定义验证对象/类示例代码详解
2017/08/29 PHP
PHP验证类的封装与使用方法详解
2019/01/10 PHP
基于Jquery的仿照flash放大图片效果代码
2011/03/16 Javascript
JS+CSS 制作的超级简单的下拉菜单附图
2013/11/22 Javascript
ajax请求乱码的解决方法(中文乱码)
2014/04/10 Javascript
js获取内联样式的方法
2015/01/27 Javascript
JQuery操作textarea,input,select,checkbox方法
2015/09/02 Javascript
jQuery实现的点赞随机数字显示动画效果(附在线演示与demo源码下载)
2015/12/31 Javascript
javascript获取wx.config内部字段解决微信分享
2016/03/09 Javascript
JS实现页面数据无限加载
2016/09/13 Javascript
简单实现node.js图片上传
2016/12/18 Javascript
微信小程序之picker日期和时间选择器
2017/02/09 Javascript
基于vue实现分页/翻页组件paginator示例
2017/03/09 Javascript
原生js 封装get ,post, delete 请求的实例
2017/08/11 Javascript
javascript中的replace函数(带注释demo)
2018/01/07 Javascript
微信小程序实现星星评价效果
2018/11/02 Javascript
Vue 实现手动刷新组件的方法
2019/02/19 Javascript
jQuery实现的导航条点击后高亮显示功能示例
2019/03/04 jQuery
vue canvas绘制矩形并解决由clearRec带来的闪屏问题
2019/09/02 Javascript
JQuery+drag.js上传图片并且实现图片拖曳
2020/11/18 jQuery
Python Web框架Pylons中使用MongoDB的例子
2013/12/03 Python
用C++封装MySQL的API的教程
2015/05/06 Python
Python列表推导式与生成器用法分析
2018/08/02 Python
Python干货:分享Python绘制六种可视化图表
2018/08/27 Python
使用Python的toolz库开始函数式编程的方法
2018/11/15 Python
python getopt模块使用实例解析
2019/12/18 Python
如何通过python计算圆周率PI
2020/11/11 Python
信访稳定工作汇报
2014/10/27 职场文书
职代会闭幕词
2015/01/28 职场文书
水电施工员岗位职责
2015/04/11 职场文书
JS监听Esc 键触发事键
2021/04/14 Javascript
i5-10400f处理相当于i7多少水平
2022/04/19 数码科技
JavaScript设计模式之原型模式详情
2022/06/21 Javascript