欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

python中pygame模块用法实例

程序员文章站 2023-11-05 10:29:04
本文实例讲述了python中pygame模块用法,分享给大家供大家参考。具体方法如下: import pygame, sys from pygame.loca...

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