如何使用 Python 和 pygame 创建 MS Paint 克隆 [英] How to create MS Paint clone with Python and pygame

查看:29
本文介绍了如何使用 Python 和 pygame 创建 MS Paint 克隆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,有两种方法可以处理鼠标事件来绘制图片.

As I see it, there are two ways to handle mouse events to draw a picture.

首先是检测鼠标何时移动并在鼠标所在的位置画一条线,如图这里.然而,这样做的问题是,当画笔尺寸较大时,每条不直的线条"之间会出现许多间隙,因为它使用线条的笔触大小来创建粗线条.

The first is to detect when the mouse moves and draw a line to where the mouse is, shown here. However, the problem with this is that with a large brush size, many gaps appear between each "line" that is not straight since it is using the line's stroke size to create thick lines.

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

The other way is to draw circles when the mouse moves as is shown here. The problem with this is that gaps appear between each circle if the mouse moves faster than the computer detects mouse input.

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

Here's a screenshot with my issues with both:

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

What is the best way to implement a brush like MS Paint's, with a decently-big brush size with no gaps in the stroke of the line or no gaps between each circle?

推荐答案

为什么不两者都做?

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

Draw a circle at each endpoint and a line between the two.

EDIT rofl,就是无法阻止自己.

EDIT rofl, just couldn't stop myself.

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

Actually, you don't want to use pygame.draw.line because it cheats. It fills a 1 pixel wide row or column (depending on angle of attack) of pixels. If you do go at a roughly perpendicular angle, 0 deg or 90 deg, this isn't an issue, but at 45's, you'll notice a sort of string bean effect.

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

The only solution is to draw a circle at every pixel's distance. Here...

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

这篇关于如何使用 Python 和 pygame 创建 MS Paint 克隆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆