所以我使用python 2.7.3和1.9.1或1.9.2,我正在制作一个小应用程序,你可以绘制和更改颜色并保存。这只是一个大问题。我会先给你看代码(完整代码)
import sys, pygame
from pygame import *
pygame.init()
size = 500, 500
screen = pygame.display.set_mode(size)
screen.fill((255,255,255))
brush = pygame.image.load("C:\Users\Pygame draw\\black1.png")
pygame.display.set_caption("THE EPIC DRAWING THING...")
clock = pygame.time.Clock()
black = pygame.image.load("C:\Users\Pygame draw\\black.png")
white = pygame.image.load("C:\Users\Pygame draw\white.png")
blue = pygame.image.load("C:\Users\Pygame draw\\blue.png")
brown = pygame.image.load("C:\Users\Pygame draw\\brown.png")
green = pygame.image.load("C:\Users\Pygame draw\green.png")
light_blue = pygame.image.load("C:\Users\Pygame draw\light blue.png")
pink = pygame.image.load("C:\Users\Pygame draw\pink.png")
purple =pygame.image.load("C:\Users\Pygame draw\purple.png")
red = pygame.image.load("C:\Users\Pygame draw\\red.png")
save = pygame.image.load("C:\Users\Pygame draw\save drawing.png")
exit = pygame.image.load("C:\Users\Pygame draw\exit.png")
black1 = pygame.image.load("C:\Users2\Pygame draw\\black1.png")
white1 = pygame.image.load("C:\Users\Pygame draw\white1.png")
blue1 = pygame.image.load("C:\Users\Pygame draw\\blue1.png")
brown1 = pygame.image.load("C:\Users\Pygame draw\\brown1.png")
green1 = pygame.image.load("C:\Users\Pygame draw\green1.png")
light_blue1 = pygame.image.load("C:\Users\Pygame draw\light blue1.png")
pink1 = pygame.image.load("C:\Users\Pygame draw\pink1.png")
purple1 =pygame.image.load("C:\Users\Pygame draw\purple1.png")
red1 = pygame.image.load("C:\Users\Pygame draw\\red1.png")
z = 0
while 1:
clock.tick(60)
mx,my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:sys.exit()
elif event.type == KEYDOWN and event.key == K_ESCAPE:sys.exit()
elif event.type == MOUSEBUTTONDOWN and pygame.mouse.get_pressed(): z = 1
elif event.type == MOUSEBUTTONUP: z = 0
if z == 1:
screen.blit(brush,(mx-5,my-5))
screen.blit(black,(0,0))
screen.blit(brown,(21,0))
screen.blit(purple,(42,0))
screen.blit(blue,(63,0))
screen.blit(light_blue,(84,0))
screen.blit(green,(105,0))
screen.blit(red,(126,0))
screen.blit(pink,(148,0))
screen.blit(white,(169,0))
screen.blit(save,(207,0))
screen.blit(exit,(430,0))
pygame.display.update()
所以基本上,我希望它是如此,如果我,让我们说,点击图片“红色”,刷变成“red1”,这是我唯一需要帮助的东西,我可以改变画笔颜色,但我只需要它,如果我点击其中一个,我可以改变画笔颜色 谢谢 :) p.s我使用eclipse作为IDE
答案 0 :(得分:3)
检查出来:
import os.path
img_dir = "C:/Users/Pygame draw/"
image_files = [
'black.png',
'white.png',
'blue.png',
]
py_images = {}
for image_file in image_files:
color_name, ext = os.path.splitext(image_file)
py_image = pygame.image.load(
os.path.join(img_dir, image_file)
)
py_images[color_name] = py_image
points = {
'blue' : (63, 0),
'white' : (169, 0),
'black' : (0, 0)
}
for color_name, point in points.items():
py_image = py_images[color_name]
screen.blit(py_image, point)
计算机擅长重复性任务。如果您发现自己一遍又一遍地编写相同的代码行,则可以改为使用循环。
至于获取点击的图像,您可以执行以下操作:
py_rects = {}
alt_img_name = "1.png"
for color_name, point in points.items():
py_image = py_images[color_name]
rect = screen.blit(py_image, point)
py_rects[rect] = color_name
...
...
elif event.type == MOUSEBUTTONDOWN:
mx,my = pygame.mouse.get_pos()
for rect in py_rects:
if rect.colllidepoint(mx, my):
clicked_color_name = py_rects[rect]
point = points[clicked_color_name]
py_image = pygame.image.load(
os.path.join(img_dir, clicked_color_name + alt_img_name)
)
screen.blit(py_image, point)
break
因此,如果您之前已经完成了新的py_image,则无需重新创建新的py_image,您可以执行以下操作:
for rect in py_rects:
if rect.colllidepoint(mx, my):
clicked_color_name = py_rects[rect]
point = points[clicked_color_name]
py_image = py_images.setdefault(
clicked_color_name + "1",
pygame.image.load(
os.path.join(img_dir, clicked_color_name + alt_img_name)
)
screen.blit(py_image, point)
break