日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

pygame畫點線方法詳解_python

作者:永遠的麥田 ? 更新時間: 2022-12-09 編程語言

一、復(fù)習(xí)

首先將上次畫的矩形做復(fù)雜一些的小程序:

import pygame,sys, random
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for i in range(100):
    width = random.randint(0, 250)
    height = random.randint(0, 100)
    top = random.randint(0, 400)
    left = random.randint(0, 500)
    pygame.draw.rect(screen, [0, 0, 0], [left, top, width, height], 1)
    pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

在此基礎(chǔ)上還可以增加矩形的寬度和顏色:

import pygame,sys, random
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for i in range(100):
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    rect_width = random.randint(1, 5)
    width = random.randint(0, 250)
    height = random.randint(0, 100)
    top = random.randint(0, 400)
    left = random.randint(0, 500)
    pygame.draw.rect(screen, [r, g, b], [left, top, width, height], rect_width)
    pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

實現(xiàn)的效果如下:

二、畫單個像素

單個像素在pygame中就是畫一個寬高為1的矩形。

代碼示例:

import pygame, sys
import math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for x in range(0, 640):
    y = int(math.sin(x/640*math.pi*4)*200 + 240)
    pygame.draw.rect(screen, [0, 0, 0], [x, y, 1, 1], 1)
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

效果圖:

需要注意的是,矩形的線寬須是1,而不是平常寫的為0,這是因為矩形太小了,沒有中間部分可以填充。

三、連接多個點

二中畫的曲線,如果仔細(xì)看就會發(fā)現(xiàn)中間不是連續(xù)的,點與點之前存在間隙。這是因為在比較陡峭的地方,x每變動1個值,y就要變動2個或更多的值,因此出現(xiàn)縫隙。

我們可以用畫線的方式把各個點連接起來,這樣就不會有間隙了:

首先來看畫線函數(shù):

發(fā)現(xiàn)此函數(shù)與draw.rect相比,只是參數(shù)plotPoints略有不同

1.連接程序生成的點

上關(guān)鍵代碼:

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

由于plotPoints是一個數(shù)組,因此我們需要先根據(jù)x值計算出所有的y值,然后將x,y成隊的加入到數(shù)組plotPoints中,最后再通過lines一次性畫出整個曲線來

效果圖如下:

2.連接外部給定的點

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

生成的效果圖:

四、逐點繪制

如果我們只是想改變某些像素的顏色,用draw.rect通過小矩形來做就有點浪費資源,可以用screen.set_at([x, y], [0, 0, 0])來實現(xiàn)相同的效果

示例代碼:

import pygame, sys, math
pygame.init()
screen = pygame.display.set_mode([640, 480])
screen.fill([255, 255, 255])
for x in range(640):
    y = math.sin(x/640*math.pi*4) * 200 + 240
    screen.set_at([int(x), int(y)], [0, 0, 0])
pygame.display.flip()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()

效果圖:

原文鏈接:https://blog.csdn.net/luhouxiang/article/details/127699604

欄目分類
最近更新