• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

pygame2 画点线

武飞扬头像
永远的麦田
帮助1

一、复习:

首先将上次画的矩形做复杂一些的小程序:

  1.  
    import pygame,sys, random
  2.  
     
  3.  
    pygame.init()
  4.  
    screen = pygame.display.set_mode([640, 480])
  5.  
    screen.fill([255, 255, 255])
  6.  
     
  7.  
    for i in range(100):
  8.  
    width = random.randint(0, 250)
  9.  
    height = random.randint(0, 100)
  10.  
    top = random.randint(0, 400)
  11.  
    left = random.randint(0, 500)
  12.  
    pygame.draw.rect(screen, [0, 0, 0], [left, top, width, height], 1)
  13.  
    pygame.display.flip()
  14.  
     
  15.  
    while True:
  16.  
    for event in pygame.event.get():
  17.  
    if event.type == pygame.QUIT:
  18.  
    sys.exit()
学新通

在此基础上还可以增加矩形的宽度和颜色:

  1.  
    import pygame,sys, random
  2.  
     
  3.  
    pygame.init()
  4.  
    screen = pygame.display.set_mode([640, 480])
  5.  
    screen.fill([255, 255, 255])
  6.  
     
  7.  
    for i in range(100):
  8.  
    r = random.randint(0, 255)
  9.  
    g = random.randint(0, 255)
  10.  
    b = random.randint(0, 255)
  11.  
    rect_width = random.randint(1, 5)
  12.  
    width = random.randint(0, 250)
  13.  
    height = random.randint(0, 100)
  14.  
    top = random.randint(0, 400)
  15.  
    left = random.randint(0, 500)
  16.  
    pygame.draw.rect(screen, [r, g, b], [left, top, width, height], rect_width)
  17.  
    pygame.display.flip()
  18.  
     
  19.  
    while True:
  20.  
    for event in pygame.event.get():
  21.  
    if event.type == pygame.QUIT:
  22.  
    sys.exit()
学新通

实现的效果如下:

学新通

 二、画单个像素

单个像素在pygame中就是画一个宽高为1的矩形。

代码示例:

  1.  
    import pygame, sys
  2.  
    import math
  3.  
     
  4.  
    pygame.init()
  5.  
    screen = pygame.display.set_mode([640, 480])
  6.  
    screen.fill([255, 255, 255])
  7.  
     
  8.  
    for x in range(0, 640):
  9.  
    y = int(math.sin(x/640*math.pi*4)*200 240)
  10.  
    pygame.draw.rect(screen, [0, 0, 0], [x, y, 1, 1], 1)
  11.  
     
  12.  
    pygame.display.flip()
  13.  
     
  14.  
    while True:
  15.  
    for event in pygame.event.get():
  16.  
    if event.type == pygame.QUIT:
  17.  
    sys.exit()
学新通

效果图:

学新通

需要注意的是,矩形的线宽须是1,而不是平常写的为0,这是因为矩形太小了,没有中间部分可以填充。 

三、连接多个点

二中画的曲线,如果仔细看就会发现中间不是连续的,点与点之前存在间隙。这是因为在比较陡峭的地方,x每变动1个值,y就要变动2个或更多的值,因此出现缝隙。

我们可以用画线的方式把各个点连接起来,这样就不会有间隙了:

首先来看画线函数:

学新通

 发现此函数与draw.rect相比,只是参数plotPoints略有不同

1. 连接程序生成的点

上关键代码:

  1.  
    plotPoints = []
  2.  
    for x in range(0, 640):
  3.  
    y = int(math.sin(x/640*math.pi*4)*200 240)
  4.  
    plotPoints.append([x, y])
  5.  
    pygame.draw.lines(screen, [0, 0, 0], False, plotPoints, 1)

由于plotPoints是一个数组,因此我们需要先根据x值计算出所有的y值,然后将x,y成队的加入到数组plotPoints中,最后再通过lines一次性画出整个曲线来

效果图如下:

学新通

2. 连接外部给定的点:

  1.  
    import pygame, sys
  2.  
    from data import dots
  3.  
    pygame.init()
  4.  
    screen = pygame.display.set_mode([640, 480])
  5.  
    screen.fill([255, 255, 255])
  6.  
    pygame.draw.lines(screen, [0, 0, 0], True, dots, 2)
  7.  
    pygame.display.flip()
  8.  
    while True:
  9.  
    for event in pygame.event.get():
  10.  
    if event.type == pygame.QUIT:
  11.  
    sys.exit()
  1.  
    dots = [
  2.  
    [221, 432], [225, 331], [133, 342], [141, 310],
  3.  
    [51, 230], [74, 217], [58, 153], [114, 164],
  4.  
    [123, 135], [176, 190], [159, 77], [193, 93],
  5.  
    [230, 28], [267, 93], [301, 77], [284, 190],
  6.  
    [327, 135], [336, 164], [402, 153], [386, 217],
  7.  
    [409, 230], [319, 310], [327, 342], [233, 331],
  8.  
    [237, 432]
  9.  
    ]

生成的效果图:

学新通

 四、逐点绘制

如果我们只是想改变某些像素的颜色,用draw.rect通过小矩形来做就有点浪费资源,可以用screen.set_at([x, y], [0, 0, 0])来实现相同的效果

示例代码:

  1.  
    import pygame, sys, math
  2.  
    pygame.init()
  3.  
    screen = pygame.display.set_mode([640, 480])
  4.  
    screen.fill([255, 255, 255])
  5.  
    for x in range(640):
  6.  
    y = math.sin(x/640*math.pi*4) * 200 240
  7.  
    screen.set_at([int(x), int(y)], [0, 0, 0])
  8.  
    pygame.display.flip()
  9.  
    while True:
  10.  
    for event in pygame.event.get():
  11.  
    if event.type == pygame.QUIT:
  12.  
    sys.exit()

效果图:

学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhggcfge
系列文章
更多 icon
同类精品
更多 icon
继续加载