15

如我所见,有两种方法可以处理鼠标事件来绘制图片。

第一个是检测鼠标何时移动并画一条线到鼠标所在的位置,如图所示。但是,这样做的问题是,对于较大的画笔大小,每条不直的“线”之间会出现许多间隙,因为它使用线条的笔触大小来创建粗线。

另一种方法是在鼠标移动时绘制圆圈,如图所示。这样做的问题是,如果鼠标移动的速度快于计算机检测到鼠标输入的速度,每个圆圈之间就会出现间隙。

这是我的两个问题的屏幕截图:

http://imgur.com/32DXN.jpg

实现像 MS Paint 这样的画笔的最佳方法是什么,画笔大小相当大,线条的笔划没有间隙或每个圆圈之间没有间隙?

4

5 回答 5

16

为什么不两者都做?

在每个端点处画一个圆圈,并在两者之间画一条线。

编辑rof,只是无法阻止自己。

实际上,您不想使用pygame.draw.line,因为它会作弊。它填充 1 像素宽的行或列(取决于攻角)像素。如果您确实以大致垂直的角度,0 度或 90 度,这不是问题,但在 45 度时,您会注意到一种串豆效应。

唯一的解决方案是在每个像素的距离处画一个圆圈。这里...

import pygame, random

screen = pygame.display.set_mode((800,600))

draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10

def roundline(srf, color, start, end, radius=1):
    dx = end[0]-start[0]
    dy = end[1]-start[1]
    distance = max(abs(dx), abs(dy))
    for i in range(distance):
        x = int( start[0]+float(i)/distance*dx)
        y = int( start[1]+float(i)/distance*dy)
        pygame.draw.circle(srf, color, (x, y), radius)

try:
    while True:
        e = pygame.event.wait()
        if e.type == pygame.QUIT:
            raise StopIteration
        if e.type == pygame.MOUSEBUTTONDOWN:
            color = (random.randrange(256), random.randrange(256), random.randrange(256))
            pygame.draw.circle(screen, color, e.pos, radius)
            draw_on = True
        if e.type == pygame.MOUSEBUTTONUP:
            draw_on = False
        if e.type == pygame.MOUSEMOTION:
            if draw_on:
                pygame.draw.circle(screen, color, e.pos, radius)
                roundline(screen, color, e.pos, last_pos,  radius)
            last_pos = e.pos
        pygame.display.flip()

except StopIteration:
    pass

pygame.quit()
于 2009-02-28T02:50:16.167 回答
3

在每个循环步骤中不进行 blitting 可以提高绘图速度(使用从前一个改编的代码可以消除我机器上的滞后问题)

import pygame, random

screen = pygame.display.set_mode((800,600))

draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10

def roundline(srf, color, start, end, radius=1):
    dx = end[0]-start[0]
    dy = end[1]-start[1]
    distance = max(abs(dx), abs(dy))
    for i in range(distance):
        x = int( start[0]+float(i)/distance*dx)
        y = int( start[1]+float(i)/distance*dy)
        pygame.display.update(pygame.draw.circle(srf, color, (x, y), radius))

try:
    while True:
        e = pygame.event.wait()
        if e.type == pygame.QUIT:
            raise StopIteration
        if e.type == pygame.MOUSEBUTTONDOWN:
            color = (random.randrange(256), random.randrange(256), random.randrange(256))
            pygame.draw.circle(screen, color, e.pos, radius)
            draw_on = True
        if e.type == pygame.MOUSEBUTTONUP:
            draw_on = False
        if e.type == pygame.MOUSEMOTION:
            if draw_on:
                pygame.display.update(pygame.draw.circle(screen, color, e.pos, radius))
                roundline(screen, color, e.pos, last_pos,  radius)
            last_pos = e.pos
        #pygame.display.flip()

except StopIteration:
    pass

pygame.quit()
于 2011-05-05T16:04:42.680 回答
2

对于第一个问题,您需要有一个背景,即使它只是一种颜色。我制作的复制乒乓球游戏也遇到了同样的问题。下面是我做的一个复制画图程序的例子,左键绘制,右键擦除,点击彩色图像选择颜色,向上键清屏:

import os
os.environ['SDL_VIDEO_CENTERED'] = '1'
from pygamehelper import *
from pygame import *
from pygame.locals import *
from vec2d import *
from math import e, pi, cos, sin, sqrt
from random import uniform

class Starter(PygameHelper):
    def __init__(self):
        self.w, self.h = 800, 600
        PygameHelper.__init__(self, size=(self.w, self.h), fill=((255,255,255)))

        self.img= pygame.image.load("colors.png")
        self.screen.blit(self.img, (0,0))

        self.drawcolor= (0,0,0)
        self.x= 0

    def update(self):
        pass

    def keyUp(self, key):
        if key==K_UP:
            self.screen.fill((255,255,255))
            self.screen.blit(self.img, (0,0))




    def mouseUp(self, button, pos):
        pass

    def mouseMotion(self, buttons, pos, rel):
        if pos[1]>=172: 
            if buttons[0]==1:
                #pygame.draw.circle(self.screen, (0,0,0), pos, 5)
                pygame.draw.line(self.screen, self.drawcolor, pos, (pos[0]-rel[0], pos[1]-rel[1]),5)                
            if buttons[2]==1:
                pygame.draw.circle(self.screen, (255,255,255), pos, 30)
            if buttons[1]==1:
                #RAINBOW MODE
                color= self.screen.get_at((self.x, 0))
                pygame.draw.line(self.screen, color, pos, (pos[0]-rel[0], pos[1]-rel[1]), 5)

                self.x+= 1
                if self.x>172: self.x=0

        else:
            if pos[0]<172:
                if buttons[0]==1:
                    self.drawcolor= self.screen.get_at(pos)
                    pygame.draw.circle(self.screen, self.drawcolor, (250, 100), 30)

    def draw(self):
        pass
        #self.screen.fill((255,255,255))
        #pygame.draw.circle(self.screen, (0,0,0), (50,100), 20)

s = Starter()
s.mainLoop(40)
于 2011-04-16T23:31:12.337 回答
1

这是 Matthew示例的简化版本,遗憾的是它无法运行。

当鼠标移动时,pygame.MOUSEMOTION事件被添加到包含位置和相对移动的事件队列中。您可以使用这些来计算先前的位置,然后将两个点传递给pygame.draw.line

pygame.MOUSEMOTION事件还有一个buttons 属性,您可以使用它来检查当前按下的鼠标按钮。

import os
import random

import pygame as pg


class App:

    def __init__(self):
        os.environ['SDL_VIDEO_CENTERED'] = '1'
        pg.init()
        self.w, self.h = 800, 600
        self.screen = pg.display.set_mode((self.w, self.h))
        self.screen.fill(pg.Color('white'))
        self.clock = pg.time.Clock()
        self.drawcolor = (0, 0, 0)

    def mainloop(self):
        while True:
            for event in pg.event.get():
                if event.type == pg.QUIT:
                    return
                elif event.type == pg.MOUSEBUTTONDOWN:
                    if event.button == 2:  # Color picker (middle mouse button).
                        self.drawcolor = self.screen.get_at(pos)
                        # Pick a random color.
                        # self.drawcolor = [random.randrange(256) for _ in range(3)]
                elif event.type == pg.MOUSEMOTION:
                    pos, rel = event.pos, event.rel
                    if event.buttons[0]:  # If the left mouse button is down.
                        # Draw a line from the pos to the previous pos.
                        pg.draw.line(self.screen, self.drawcolor, pos, (pos[0]-rel[0], pos[1]-rel[1]), 5)
                    elif event.buttons[2]:  # If the right mouse button is down.
                        # Erase by drawing a circle.
                        pg.draw.circle(self.screen, (255, 255, 255), pos, 30)

            pg.display.flip()
            self.clock.tick(30)


if __name__ == '__main__':
    app = App()
    app.mainloop()
    pg.quit()
于 2018-04-27T18:34:56.627 回答
0

我制作了一个更有效的圆形功能版本。

import pygame as pg,random,math


screen = pg.display.set_mode((800,600))

draw_on = False
last_pos = (0, 0)
color = (255, 128, 0)
radius = 10

def roundline(srf, color, start, end, radius=1):
    pg.display.update([drawline(srf,color,start,end,radius),
                       pg.draw.circle(srf, color, end, radius),
                       pg.draw.circle(srf, color, start, radius)])

def drawline(srf, color,start,end,width):
    v=(start[0]- end[0],start[1]- end[1])
    betrag=width/math.sqrt(v[0]*v[0]+v[1]*v[1])
    v_=(v[1]*betrag,-betrag*v[0])
    return pg.draw.polygon(srf, color,(
            (int(start[0]+v_[0]),int(start[1]+v_[1])),
            (int(start[0]-v_[0]),int(start[1]-v_[1])),
            (int(end[0]-v_[0]),int(end[1]-v_[1])),
            (int(end[0]+v_[0]),int(end[1]+v_[1]))
            ))

try:
    while True:
        e = pg.event.wait()
        if e.type == pg.QUIT:
            raise StopIteration
        elif e.type == pg.MOUSEBUTTONDOWN:
            color = (random.randrange(256), random.randrange(256), random.randrange(256))
            #pg.draw.circle(screen, color, e.pos, radius)
            draw_on = True
            start=e.pos
        elif e.type == pg.MOUSEBUTTONUP:
            draw_on = False
        elif e.type == pg.MOUSEMOTION:
            if draw_on:
                roundline(screen, color, e.pos, last_pos,  radius)
            last_pos = e.pos
        elif e.type == pg.MOUSEWHEEL:
            radius=max(1,radius+e.y)
finally:
    pg.quit()

于 2021-02-27T23:47:41.137 回答