当前位置:   article > 正文

二、pygame基本图形绘制_pygame.draw.rect

pygame.draw.rect

1.绘制文字

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

myfont = pygame.font.Font(None,60)
white = 255,255,255
textImage = myfont.render("here", True, white)
screen.blit(textImage, (200,200))

  • 1
  • 2
  • 3
  • 4
  • 5

加入上一章的程序后完整程序如下

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

myfont = pygame.font.Font(None,60)
white = 255,255,255
textImage = myfont.render("here", True, white)
screen.blit(textImage, (200,200))

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

while True:
    for event in pygame.event.get():
         if event.type == pygame.QUIT:
             pygame.quit()
             exit()
             
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

程序的效果是下面这样的
在这里插入图片描述

2.在屏幕显示鼠标位置

获取鼠标位置只需要用到pygame.mouse.get_pos(),代码如下

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

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

# -*- coding=utf-8 -*-
import pygame
pygame.init()
screencaption = pygame.display.set_caption('first pygame')
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才能看到绘图显示
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21

现在鼠标移动时中间的文字就会跟着立即显示鼠标的坐标了,我们看到鼠标放到最左上角的时候x、y的值都是接近0的,放到最右下角是接近400的
在这里插入图片描述

3.绘制圆形

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

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)

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们把这段代码加入主循环的 pygame.display.update() 前看到界面上多了一个的圈
在这里插入图片描述

4.绘制矩形

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

    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)
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

我们把这段代码加入主循环的 pygame.display.update() 前看到界面上多了一个矩形
在这里插入图片描述

5.绘制线段

下面定义了一个黄色,从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)
  • 1
  • 2

绘制效果如下
在这里插入图片描述

6.绘制图片

请先将这个图片存放到程序的运行目录,并且命名为pygame.gif
在这里插入图片描述
下面是加载图片的代码

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

  • 1
  • 2
  • 3
  • 4

我们把前两行代码加入主循环外,第三行代码加入主循环的pygame.display.update() 前,看到的效果是这样的
在这里插入图片描述
结束本章前贴一下目前的完整代码

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

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

image = pygame.image.load('pygame.gif') # 加载图片
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才能看到绘图显示
    
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/154524
推荐阅读
相关标签
  

闽ICP备14008679号