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单元测试unittest的使用示例
Nov 18 Python
分享一下Python数据分析常用的8款工具
Apr 29 Python
Python爬虫常用小技巧之设置代理IP
Sep 13 Python
详解Python的循环结构知识点
May 20 Python
numpy库与pandas库axis=0,axis= 1轴的用法详解
May 27 Python
关于pytorch中网络loss传播和参数更新的理解
Aug 20 Python
在tensorflow中设置使用某一块GPU、多GPU、CPU的操作
Feb 07 Python
python 解决print数组/矩阵无法完整输出的问题
Feb 19 Python
Python loguru日志库之高效输出控制台日志和日志记录
Mar 07 Python
理解Django 中Call Stack机制的小Demo
Sep 01 Python
python中类与对象之间的关系详解
Dec 16 Python
python3定位并识别图片验证码实现自动登录功能
Jan 29 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
浅析is_writable的php实现
2013/06/18 PHP
PHP+jquery实时显示网站在线人数的方法
2015/01/04 PHP
基于PHP实现堆排序原理及实例详解
2020/06/19 PHP
Aster vs Newbee BO5 第二场2.19
2021/03/10 DOTA
IE下js调试工具Companion.JS
2010/10/15 Javascript
基于jquery的loading 加载提示效果实现代码
2011/09/01 Javascript
js split 的用法和定义 js split分割字符串成数组的实例代码
2012/05/13 Javascript
js制作的鼠标悬浮时产生的下拉框效果
2012/10/27 Javascript
JQuery伸缩导航练习示例
2013/11/13 Javascript
JS打开新窗口防止被浏览器阻止的方法
2015/01/03 Javascript
JavaScript点击按钮后弹出透明浮动层的方法
2015/05/11 Javascript
jquery实现一个全局计时器(商城可用)
2017/06/30 jQuery
opencv 识别微信登录验证滑动块位置
2018/08/07 Javascript
vue 监听 Treeselect 选择项的改变操作
2020/08/31 Javascript
详解微信小程序动画Animation执行过程
2020/09/23 Javascript
JavaScript实现简单动态表格
2020/12/02 Javascript
[15:39]教你分分钟做大人:龙骑士
2014/10/30 DOTA
python实现的登录和操作开心网脚本分享
2014/07/09 Python
深入学习Python中的上下文管理器与else块
2017/08/27 Python
用pycharm开发django项目示例代码
2018/10/24 Python
pyttsx3实现中文文字转语音的方法
2018/12/24 Python
Python redis操作实例分析【连接、管道、发布和订阅等】
2019/05/16 Python
python设置环境变量的原因和方法
2019/06/24 Python
Python爬取智联招聘数据分析师岗位相关信息的方法
2019/08/13 Python
简单了解django处理跨域请求最佳解决方案
2020/03/25 Python
CSS3近阶段篇之酷炫的3D旋转透视
2016/04/28 HTML / CSS
Html5新增标签与样式及让元素水平垂直居中
2019/07/11 HTML / CSS
Mankind美国/加拿大:英国领先的男士美容护发用品公司
2018/12/05 全球购物
培训心得体会
2013/12/29 职场文书
大学生毕业的自我评价分享
2014/01/02 职场文书
学校地质灾害防治方案
2014/06/10 职场文书
代领毕业证委托书
2014/08/02 职场文书
思想作风整顿个人剖析材料
2014/10/06 职场文书
2014年全国法制宣传日宣传活动方案
2014/11/02 职场文书
幼儿园教师个人总结
2015/02/05 职场文书
数据库的高级查询六:表连接查询:外连接(左外连接,右外连接,UNION关键字,连接中ON与WHERE的不同)
2021/04/05 MySQL