我最近用Python构建了一个简单的图像编辑器。对于这个代码,我没有什么特别的不满之处。我只是要求一般的批评,以帮助我改善。
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)发布于 2020-08-12 21:55:32
总之,代码看起来不错,图像编辑器很有趣,我试过了。
以下是一些小小的建议:
名称x实际上不是一个好名称,它不告诉您变量的用途或值:
x = input("Do you want to edit an image?")我会使用像user_input或更好的edit_image_user_input这样的名字。这使得代码更容易理解。但是,我必须承认,在这种特殊情况下,名称并不那么重要,因为变量已经在下面的行中使用了,并且只使用了一次。不过,我还是觉得选择有意义的名字是个好习惯。
作为用户,当我看到提示符时
"Do you want to edit an image?"我不知道该如何作出决定。我应该输入“是”还是"y“也可以?输入大小写敏感吗?而且,这个问题可能更具体:“你想编辑一个图像吗?”->当然我想编辑一个图像,我只是打开了一个图像编辑器。您应该告诉用户,他可以从URL中打开图像。
要使代码更改更容易,请将下面代码中的神奇数字5替换为变量
if radius < 5:
radius = 5我建议
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值的元组,尽管您之前已经定义了几行黑色:
black = (0, 0, 0)
# [...]
background_colour = (0,0,0)最好这样做:
black = (0, 0, 0)
# [...]
background_colour = black 看看这一大块代码:
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.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这些都是大量的重复,在每个代码块内和它们之间。像这样的东西不太灵活,而且读起来也不多。例如,如果要更改颜色为黑色的键,则必须进行两次:背景颜色和钢笔颜色。您可以尝试另一种方法。首先,以字典的形式创建从键到颜色的映射:
key_to_color_map = {'n': black,
'w': white,
'r': red} # do this for all colors然后,将if语句的大块替换为
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。
https://codereview.stackexchange.com/questions/247321
复制相似问题