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代码(逐级优化)
May 25 Python
windows下python模拟鼠标点击和键盘输示例
Feb 28 Python
python使用cPickle模块序列化实例
Sep 25 Python
Python可跨平台实现获取按键的方法
Mar 05 Python
Python中用于计算对数的log()方法
May 15 Python
Python简单实现enum功能的方法
Apr 25 Python
Python3.6.0+opencv3.3.0人脸检测示例
May 25 Python
python的pytest框架之命令行参数详解(上)
Jun 27 Python
Python configparser模块配置文件过程解析
Mar 03 Python
python3通过qq邮箱发送邮件以及附件
May 20 Python
Python 使用双重循环打印图形菱形操作
Aug 09 Python
详解Python之Scrapy爬虫教程NBA球员数据存放到Mysql数据库
Jan 24 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 session_start()关于Cannot send session cache limiter - headers already sent错误解决方法
2009/11/27 PHP
CI框架中zip类应用示例
2014/06/17 PHP
PHP中的日期加减方法示例
2014/08/21 PHP
ThinkPHP中where()使用方法详解
2016/04/19 PHP
详解php用static方法的原因
2018/09/12 PHP
一个符号插入器 中用到的js代码
2007/09/04 Javascript
用javascript实现画板的代码
2007/09/05 Javascript
jquery easyui的tabs使用时的问题
2010/03/23 Javascript
关于JavaScript中原型继承中的一点思考
2012/07/25 Javascript
php图像生成函数之间的区别分析
2012/12/06 Javascript
js实现拉伸拖动iframe的具体代码
2013/08/03 Javascript
js如何判断用户是在PC端和还是移动端访问
2014/04/24 Javascript
JavaScript实现阿拉伯数字和中文数字互相转换
2016/06/12 Javascript
JS当前页面登录注册框,固定DIV,底层阴影的实例代码
2016/09/29 Javascript
获取今天,昨天,本周,上周,本月,上月时间(实例分享)
2017/01/04 Javascript
vue axios用法教程详解
2017/07/23 Javascript
浅谈react-native热更新react-native-pushy集成遇到的问题
2017/09/30 Javascript
js判断输入框不能为空格或null值的实现方法
2018/03/02 Javascript
JS中获取 DOM 元素的绝对位置实例详解
2018/04/23 Javascript
vue 实现图片懒加载功能
2020/12/31 Vue.js
[02:05]2014DOTA2国际邀请赛 BBC外卡赛赛后总结
2014/07/09 DOTA
python字符串连接方式汇总
2014/08/21 Python
Python的动态重新封装的教程
2015/04/11 Python
python使用urlparse分析网址中域名的方法
2015/04/15 Python
python与php实现分割文件代码
2017/03/06 Python
Empty test suite.(PyCharm程序运行错误的解决方法)
2018/11/30 Python
Python 模拟动态产生字母验证码图片功能
2019/12/24 Python
python 基于opencv实现图像增强
2020/12/23 Python
伦敦鲜花递送:Flower Station
2021/02/03 全球购物
客服部工作职责范本
2014/02/14 职场文书
实习推荐信
2014/05/10 职场文书
完美的中文自荐信
2014/05/24 职场文书
暑期培训班策划方案
2014/08/26 职场文书
党的群众路线教育实践活动先进个人材料
2014/12/24 职场文书
理想国读书笔记
2015/06/25 职场文书
2016年“世界环境日”校园广播稿
2015/12/18 职场文书