-1

所以我正在使用 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”,这是我唯一需要帮助的事情,我可以改变画笔颜色,但我只需要它这样如果我单击其中一个,我可以更改画笔颜色谢谢 :) ps 我使用 eclipse 作为 IDE

4

1 回答 1

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,您可以执行以下操作:

    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
于 2013-09-10T17:41:21.893 回答