单击并创建一个正方形,我在pygame中单击

时间:2018-04-30 22:28:59

标签: python pygame

在下面的代码中,我想点击"菜单中的方块"下面它是在"工作区"中复制的。 (我们将点击的地方(顶部)。但目前它只在鼠标的同时移动方块。

我无法解决这个问题,你能帮帮我吗?

亲切

import pygame
from pygame.locals import *

pygame.init()

#window
window = pygame.display.set_mode([1000,800])
pygame.display.set_caption("ArchiConnect World")

#constantes
clickable_area = pygame.Rect((100,720), (70, 70))
clickable_area2 = pygame.Rect((100,500), (70, 70))
clickable_area3 = pygame.Rect((220,730), (50, 50))
clickable_area4 = pygame.Rect((300, 450), (50, 50))
hold = 0;

#colors
GREEN = (84,173,65)
RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
rect_draw = 0
x = 100
y = 500

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
    event.pos = pygame.mouse.get_pos();
    #menu rectangle
    pygame.draw.rect(window, (BLUE), Rect((0,700), (1000,10)))
    #server rectangle
    pygame.draw.rect(window, (GREEN), Rect((100,720), (70,70)))
    #server rectangle in board


    if event.type == MOUSEBUTTONUP:
        if event.button == 1:
            if clickable_area.collidepoint(event.pos):
                hold =1

    if event.type == MOUSEMOTION:  # Si mouvement de souris
        # On change les coordonnées du
        if hold == 1:
            carre_x = event.pos[0]
            carre_y = event.pos[1]
            pygame.draw.rect(window, (GREEN), Rect((carre_x, carre_y), (70,70)))

pygame.display.update()

1 个答案:

答案 0 :(得分:0)

您可以定义当用户单击鼠标按钮时附加新rects的pygame rects列表。然后在for循环的帮助下绘制列表中的rects。

import pygame
from pygame.locals import *

pygame.init()

window = pygame.display.set_mode([1000,800])
clock = pygame.time.Clock()

clickable_area = pygame.Rect((0, 0), (1000, 630))
# Define a list to which we add the pygame.Rects.
rects = []

GREEN = (84,173,65)
RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)

done = False
while not done:
    for event in pygame.event.get():
        if event.type == QUIT:
            done = True
        if event.type == MOUSEBUTTONDOWN:
            if clickable_area.collidepoint(event.pos):
                # Append a new rect to the rects list.
                rects.append(Rect(event.pos, (70, 70)))

    #menu rectangle
    pygame.draw.rect(window, BLUE, Rect((0, 700), (1000, 10)))
    #server rectangle
    pygame.draw.rect(window, GREEN, Rect((100, 720), (70, 70)))
    pygame.draw.rect(window, GREEN, clickable_area, 3)

    # Draw the rects.
    for rect in rects:
        pygame.draw.rect(window, GREEN, rect)

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

pygame.quit()