当前位置: 移动技术网 > IT编程>脚本编程>Python > python中pygame模块用法实例

python中pygame模块用法实例

2019年06月14日  | 移动技术网IT编程  | 我要评论

开心网001首页,t124次列车,贵州民族大学教务管理系统

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网