Pygame基本图形绘制

358 阅读3分钟

绘制文字

下面例子定义了一个白色,字体大小为40,在x=50,y=50处开始绘制的文字

myfont = pygame.font.Font(None,40)
white = 255,255,255
textImage = myfont.render("指尖编程", True, white)
screen.blit(textImage, (200,200))

把示例加入[Pygame入门](Pygame入门 #掘金文章# juejin.cn/post/696835…

# -*- coding=utf-8 -*-
import pygame
pygame.init()
screencaption = pygame.display.set_caption('指尖编程')
screen = pygame.display.set_mode((400,400)) #设置400*400窗口
screen.fill((0,0,255)) # 将界面设置为蓝色

myfont = pygame.font.Font(None,40)
white = 255,255,255
textImage = myfont.render("www.zhijiancode.com", True, white)
screen.blit(textImage, (50,50))


pygame.display.update() # 必须调用update才能看到绘图显示

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

程序效果如下图所示

在屏幕显示鼠标位置

获取鼠标位置只需要用到pygame.mouse.get_pos()

x, y = pygame.mouse.get_pos() # 获取鼠标位置
textImage = myfont.render("x=%s,y=%s"%(x,y), True, white) #将鼠标位置设置为显示内容

由于鼠标的移动位置刷新后会重复绘制文字,所以我们要将界面的底色设置部分和文字的绘制部分的代码放到主循环中,并且将pygame.display.update() 也放入主循环,修改后的完整代码如下

# -*- coding=utf-8 -*-
import pygame
pygame.init()
screencaption = pygame.display.set_caption('指尖编程')
screen = pygame.display.set_mode((400,400)) #设置400*400窗口

myfont = pygame.font.Font(None,30)
white = 255,255,255

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

    screen.fill((0,0,255)) # 将界面设置为蓝色
    x, y = pygame.mouse.get_pos() # 获取鼠标位置
    textImage = myfont.render("x=%s,y=%s"%(x,y), True, white)
    screen.blit(textImage, (200,200))
    pygame.display.update() # 必须调用update才能看到绘图显示

现在鼠标移动时中间的数字就会立即显示鼠标的坐标了。

绘制圆形

下面定义了一个黄色、半径是30的、中心位置在100,50的圆

circle_color = [255,255,0]
circle_x = 100
circle_y = 50
circle_size = 30
line_width = 2
pygame.draw.circle(screen,circle_color,[circle_x,circle_y],circle_size,line_width)

我们把这段代码加入主循环的 pygame.display.update() 前看到界面上多了一个圆形。

绘制矩形

下面定义了一个黄色,宽80,高30,左上角顶点在300,50位置的矩形

    rect_color = 255,255,0
    rect_x = 300
    rect_y = 50
    rect_width = 80
    rect_height = 30
    pygame.draw.rect(screen,rect_color,[rect_x,rect_y,rect_width,rect_height],line_width)

我们和前面一样把这段代码加入主循环的 pygame.display.update() 前看到界面上又多了一个矩形。

绘制线段

下面定义了一个黄色,从x=50,y=120的点连接到x=300,y=160的点的线段

    line_color = 255,255,0
    pygame.draw.line(screen,line_color,[50,120],[300,160], line_width)

还是跟前面一样加入进去,效果如下:

绘制图片

我们要先将这个图片存放到程序的运行目录,并且命名为pygame.gif

加载图片代码如下

image = pygame.image.load('pygame.gif') # 加载图片
image = pygame.transform.scale(image,(338,100)) #图片有点大,将它缩小到宽338100的大小
screen.blit(image, (40,250)) # 在界面上x=40,y=250的位置显示此图片

我们把前两行代码加入主循环外,第三行代码加入主循环的pygame.display.update() 前,看到的效果是这样的

最后附上完整代码

# -*- coding=utf-8 -*-
import pygame

pygame.init()
screencaption = pygame.display.set_caption('指尖编程')
screen = pygame.display.set_mode((400, 400))  # 设置400*400窗口

myfont = pygame.font.Font(None, 30)
white = 255, 255, 255

image = pygame.image.load('img/pygame.gif.jpeg')  # 加载图片
image = pygame.transform.scale(image, (338, 100))  # 图片有点大,将它缩小到宽338高100的大小

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

    screen.fill((0, 0, 255))  # 将界面设置为蓝色
    x, y = pygame.mouse.get_pos()  # 获取鼠标位置
    textImage = myfont.render("x=%s,y=%s" % (x, y), True, white)
    screen.blit(textImage, (200, 200))

    circle_color = 255, 255, 0
    circle_x = 100
    circle_y = 50
    circle_size = 30
    line_width = 2
    pygame.draw.circle(screen, circle_color, [circle_x, circle_y], circle_size, line_width)
    rect_color = 255, 255, 0
    rect_x = 300
    rect_y = 50
    rect_width = 80
    rect_height = 30
    pygame.draw.rect(screen, rect_color, [rect_x, rect_y, rect_width, rect_height], line_width)

    line_color = 255, 255, 0
    pygame.draw.line(screen, line_color, [50, 120], [300, 160], line_width)

    screen.blit(image, (40, 250))  # 在界面上x=40,y=250的位置显示此图片

    pygame.display.update()  # 必须调用update才能看到绘图显示