简体   繁体   English

pygame项目我卡住了,请回复

[英]Pygame project i'm stuck, please reply

So i'm using python 2.7.3 and 1.9.1 or 1.9.2, and i'm making a little app where you can draw and change colors and save. 所以我正在使用python 2.7.3和1.9.1或1.9.2,并且我正在制作一个小应用程序,您可以在其中绘制和更改颜色并保存。 there's only one big problem. 只有一个大问题。 i'll show you the code first(full code) 我先给你看代码(完整代码)

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

So basically, i want it to be so that if i, let's say, click the picture "red", brush turns into "red1" that's the only thing i need help with, i can change the brush colour, but i just need it to be so that if i click one of them, i can change the brush colour Thanks :) ps i use eclipse as the IDE 基本上,我希望这样,如果我单击图片“红色”,画笔变成“ red1”,这是我唯一需要帮助的地方,我可以更改画笔的颜色,但是我只需要它是这样的话,如果我单击其中之一,就可以更改画笔的颜色,谢谢:) ps,我使用eclipse作为IDE

Check this out: 看一下这个:

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)

Computers are good at repetitive tasks. 计算机擅长重复性工作。 If you find yourself writing the same line of code over and over again, you can use a loop instead. 如果发现自己一遍又一遍地写同一行代码,则可以改用循环。

As for getting the image that was clicked, you can do something like this: 至于获取被点击的图像,您可以执行以下操作:

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

And so that you don't have to re-create the new py_image if you've already done it before, you can do something like this: 这样,如果您以前已经完成过操作,就不必重新创建新的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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM