python中pygame模块用法实例

yipeiwu_com5年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

Tensorflow加载预训练模型和保存模型的实例

使用tensorflow过程中,训练结束后我们需要用到模型文件。有时候,我们可能也需要用到别人训练好的模型,并在这个基础上再次训练。这时候我们需要掌握如何操作这些模型数据。看完本文,相信...

ubuntu 18.04 安装opencv3.4.5的教程(图解)

ubuntu 18.04 安装opencv3.4.5的教程(图解)

【写在前面】 这真的是太那个什么了 不管怎么说 做过的东西做个笔记总是好的 花一点点时间做笔记 不然如果哪一天要重新做了 或者哪一天要汇报工作 都不知道该从哪里入手 又要重新来...

对python中return和print的一些理解

前言 最近刚开始学习python,之前只有一些c的基础,也忘得差不多了,现在想边学习边总结和分享收获~看书时看了return的用法,可是后来和print混了,老是感觉可以将函数retur...

JS设计模式之责任链模式实例详解

JS设计模式之责任链模式实例详解

本文实例讲述了JS设计模式之责任链模式。分享给大家供大家参考,具体如下: 责任链设计模式: 在责任链模式里,很多对象由每一个对象对其下家的引用而连接起来形成一条链。请求在这个链上传递,直...

新手常见6种的python报错及解决方法

此篇文章整理新手编写代码常见的一些错误,有些错误是粗心的错误,但对于新手而已,会折腾很长时间才搞定,所以在此总结下我遇到的一些问题。希望帮助到刚入门的朋友们。 1.NameError变量...