pygame(一)

旧巷老猫 提交于 2020-01-01 19:43:00

个人博客

pygame

安装见这里
引用import pygame,除此外,个人还喜欢用sys与time个人喜好。
起始from pygame.locals import *pygame.init()

基本设置

窗口设置

windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('标题名称')

第一行设置窗口大小500*400,第二行设置窗口标题。

颜色与字体

一般用RGB表示,先设置好常用的,后面引用函数即可。
WHITE = (255, 255, 255)
basicFont = pygame.font.SysFont(“仿宋”,48)
第二行为字体设置,字体样式与大小。

渲染对象

text =basicFont.render('文本', True,颜色, 颜色)
render()为对文本的设置,第二个是抗锯齿(更为平滑)的布尔值,第三个是文本颜色,第四个是文本背景颜色。

位置与基本图形

文本位置

pygame的React属性用于表示一个特定大小和位置的矩形区域,可以作为窗口内对象的位置设置。

函数名 = text.get_rect()
函数名x = windowSurfcae.get_rect().conterx
函数名y = windowSurfcae.get_rect().contery

这样就已经定义了React中央X、Y坐标的整数值,而后赋值即可。
类似的Rect属性还有myRect.left、myRect.top、myRect.width、myRect.size等等。

填充颜色

windowSurface.fill(颜色)在调用pygame.display.update()

绘制图形

  • 多边形 pygame.draw.polygon(windowSurface, 颜色, (各点坐标))
  • 直线 pygame.draw.line(windowSurface, 颜色, (起始点), (终点), 宽度)
  • 圆形 pygame.draw.circle(windowSurface, 颜色, (圆心), 半径大小, 线条宽度值)
  • 椭圆形 pygame.draw.ellipse(windowSurface, 颜色, (X坐标, Y坐标, 宽, 高), 线条宽度值)
  • 矩形 可用来提供文本位置,pygame.draw.rect(windowSurface, 颜色, (xxxRect.left - 20, xxxRect.top - 20等等))

给像素着色

pygame.PixelArray对象可以用来控制像素,

pixArray = pygame.PixelArray(windowSurface)
pixArray[xx][xx] = 颜色
del pixArray

记得删除PixArray对象,否则会报错。

绘制一个内容到另一个对象上

blit方法是将一个对象绘制与另一个对象上的方法,可用来将上面文本内容绘制在图形上。
windowSurface.blit(text, textRect)

绘制屏幕

pygame.display.update()只有在所有函数调用完才会更新屏幕。

循环以及退出

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()

程序本身会每秒执行100次,正常不会停止,在if里设置event.type == QUIT实际上用来关机窗口的事件,如果窗后被关闭,会出现QUIT事件,然后程序会停止。
附源码

import pygame, sys
from pygame.locals import *

pygame.init()

windowSurface = pygame.display.set_mode((500, 400), 0, 32)
pygame.display.set_caption('标题')

BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

basicFont = pygame.font.SysFont("仿宋", 48)

text = basicFont.render('hissssssssss', 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()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!