pygame2 draw dotted line

1. Review:

First, make the rectangle drawn last time into a more complicated small program:

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()

On this basis, you can also increase the width and color of the rectangle:

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()

The effect achieved is as follows:

 2. Draw a single pixel

A single pixel in pygame is to draw a rectangle with a width and height of 1.

Code example:

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()

Renderings:

It should be noted that the line width of the rectangle must be 1 instead of 0 as usual, because the rectangle is too small and there is no middle part to fill. 

3. Connect multiple points

If you look carefully at the curve drawn in No. 2, you will find that the middle is not continuous, and there are gaps between points. This is because in steeper places, for every value of x that changes, y changes by 2 or more values, so gaps appear.

We can connect the points by drawing lines so that there are no gaps:

First look at the line drawing function:

 It is found that this function is compared with draw.rect, but the parameter plotPoints is slightly different

1. Connect the points generated by the program

The above key code:

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)

Since plotPoints is an array, we need to calculate all the y values ​​​​based on the x value first, then add x and y to the array plotPoints in groups, and finally draw the entire curve at once through the lines.

The renderings are as follows:

2. Connect externally given points:

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]
]

Generated renderings:

 4. Drawing point by point

If we just want to change the color of some pixels, it is a bit wasteful to use draw.rect to do it through a small rectangle. We can use screen.set_at([x, y], [0, 0, 0]) to achieve the same effect

Sample code:

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()

Renderings:

Guess you like

Origin blog.csdn.net/luhouxiang/article/details/127699604