首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PyGame图像编辑器

PyGame图像编辑器
EN

Code Review用户
提问于 2020-07-31 20:14:30
回答 1查看 353关注 0票数 2

我最近用Python构建了一个简单的图像编辑器。对于这个代码,我没有什么特别的不满之处。我只是要求一般的批评,以帮助我改善。

代码语言:javascript
复制
import pygame
import random
import time

x = input("Do you want to edit an image?")

if  x == "yes":
    image_url = input("please enter image url")
    image = pygame.image.load(image_url) 
    i_edit = True

else:
    i_edit = False



pygame.init()


red = (255,0,0)
orange = (255,165,0)
yellow = (255,255,0)
green = (0,255,0)
blue = (0,0,215)
indigo = (75,0,130)
violet = (128,0,128)
black = (0, 0, 0)
white = (255,255,255)

gameDisplay = pygame.display.set_mode((900, 900))
clock = pygame.time.Clock()

color = white

crashed = False
buttpressed = 0
rightkeypressed = False
radius = 10

background_colour = (0,0,0)
gameDisplay.fill(background_colour)

if i_edit:
    gameDisplay.blit(image, (0, 0))

while not crashed:

    if radius < 5:
        radius = 5
 
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            crashed = True

        pygame.display.set_caption("Your Pen Size is {0}".format(radius))

        if event.type == pygame.KEYDOWN and rightkeypressed is True:
            if event.unicode == 'n':
                background_colour = black
            if event.unicode == 'w':
                background_colour = white
            if event.unicode == 'r':
                background_colour = red
            if event.unicode == 'o':
                background_colour = orange
            if event.unicode == 'y':
                background_colour = yellow
            if event.unicode == 'g':
                background_colour = green
            if event.unicode == 'b':
                background_colour = blue
            if event.unicode == 'i':
                background_colour = indigo
            if event.unicode == 'p':
                background_colour = violet
            if event.unicode == 'e':
                color = background_colour
            gameDisplay.fill(background_colour)
            if event.unicode == 'q':
                gameDisplay.blit(image, (0, 0))


        if event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 3:
                rightkeypressed = True

        if event.type == pygame.MOUSEBUTTONUP:
            if event.button == 3:
                rightkeypressed = False
        
        if event.type == pygame.KEYDOWN:
            if event.unicode == 'n':
                color = black
            if event.unicode == 'w':
                color = white
            if event.unicode == 'r':
                color = red
            if event.unicode == 'o':
                color = orange
            if event.unicode == 'y':
                color = yellow
            if event.unicode == 'g':
                color = green
            if event.unicode == 'b':
                color = blue
            if event.unicode == 'i':
                color = indigo
            if event.unicode == 'p':
                color = violet
            if event.unicode == 'e':
                color = background_colour
            if event.unicode == '=':
                radius += 5
            if event.unicode == '-':
                radius -= 5

        
        if event.type == pygame.MOUSEBUTTONDOWN:
            buttpressed += 1


        if event.type == pygame.MOUSEMOTION and (buttpressed%2) != 0:
            pygame.draw.circle(gameDisplay, color, pygame.mouse.get_pos(), radius)

    pygame.display.update()
    clock.tick(60)
EN

回答 1

Code Review用户

发布于 2020-08-12 21:55:32

总之,代码看起来不错,图像编辑器很有趣,我试过了。

以下是一些小小的建议:

变量,并告诉名称

名称x实际上不是一个好名称,它不告诉您变量的用途或值:

代码语言:javascript
复制
x = input("Do you want to edit an image?")

我会使用像user_input或更好的edit_image_user_input这样的名字。这使得代码更容易理解。但是,我必须承认,在这种特殊情况下,名称并不那么重要,因为变量已经在下面的行中使用了,并且只使用了一次。不过,我还是觉得选择有意义的名字是个好习惯。

请求输入时,告诉用户哪些选项可用,以及如何输入

作为用户,当我看到提示符时

代码语言:javascript
复制
"Do you want to edit an image?"

我不知道该如何作出决定。我应该输入“是”还是"y“也可以?输入大小写敏感吗?而且,这个问题可能更具体:“你想编辑一个图像吗?”->当然我想编辑一个图像,我只是打开了一个图像编辑器。您应该告诉用户,他可以从URL中打开图像。

避免硬编码

要使代码更改更容易,请将下面代码中的神奇数字5替换为变量

代码语言:javascript
复制
if radius < 5:
    radius = 5

我建议

代码语言:javascript
复制
minimum_pen_radius = 5 # put this at the beginning of your script or in a separate config file
# [...]
if radius < minimum_pen_radius:
    radius = minimum_pen_radius

您可以为background_colour分配一个包含黑色RGB值的元组,尽管您之前已经定义了几行黑色:

代码语言:javascript
复制
black = (0, 0, 0)
# [...]
background_colour = (0,0,0)

最好这样做:

代码语言:javascript
复制
black = (0, 0, 0)
# [...]
background_colour = black 

避免重复

看看这一大块代码:

代码语言:javascript
复制
if event.unicode == 'n':
    background_colour = black
if event.unicode == 'w':
    background_colour = white
if event.unicode == 'r':
    background_colour = red
if event.unicode == 'o':
    background_colour = orange
if event.unicode == 'y':
    background_colour = yellow
if event.unicode == 'g':
    background_colour = green
if event.unicode == 'b':
    background_colour = blue
if event.unicode == 'i':
    background_colour = indigo
if event.unicode == 'p':
    background_colour = violet
if event.unicode == 'e':
    color = background_colour
gameDisplay.fill(background_colour)
if event.unicode == 'q':
    gameDisplay.blit(image, (0, 0))

而这个:

代码语言:javascript
复制
if event.unicode == 'n':
    color = black
if event.unicode == 'w':
    color = white
if event.unicode == 'r':
    color = red
if event.unicode == 'o':
    color = orange
if event.unicode == 'y':
    color = yellow
if event.unicode == 'g':
    color = green
if event.unicode == 'b':
    color = blue
if event.unicode == 'i':
    color = indigo
if event.unicode == 'p':
    color = violet
if event.unicode == 'e':
    color = background_colour
if event.unicode == '=':
    radius += 5
if event.unicode == '-':
    radius -= 5

这些都是大量的重复,在每个代码块内和它们之间。像这样的东西不太灵活,而且读起来也不多。例如,如果要更改颜色为黑色的键,则必须进行两次:背景颜色和钢笔颜色。您可以尝试另一种方法。首先,以字典的形式创建从键到颜色的映射:

代码语言:javascript
复制
key_to_color_map = {'n': black,
                    'w': white,
                    'r': red} # do this for all colors

然后,将if语句的大块替换为

代码语言:javascript
复制
try:
    background_colour = key_to_color_map[event.unicode]
    gameDisplay.fill(background_colour)   
except KeyError:
    if event.unicode == 'q':
        gameDisplay.blit(image, (0, 0))     

对于预先定义的所有键,只需在字典中查找相应的颜色即可。如果该键不在字典中(抛出一个KeyError),您将跳转到not块。您可以对设置背景色的块以及设置钢笔颜色的块执行此操作,并且可以对两者使用相同的字典key_to_color_map

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/247321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档