python中的pygame模块_python中pygame模块用法实例

本文介绍了如何使用Python的Pygame模块创建图形界面,包括设置窗口、填充颜色、绘制多边形、线条、圆形、椭圆和文本。还展示了如何处理事件循环,以及通过PixelArray操作像素。
摘要由CSDN通过智能技术生成

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程序设计有所帮助。

本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉

本文系统来源:php中文网

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值