【问题标题】:How to draw objects that can be dragged and droped on the screen using pygame?如何使用pygame在屏幕上绘制可以拖放的对象?
【发布时间】:2017-06-03 11:52:29
【问题描述】:

我正在尝试绘制 5 个矩形,所有这些矩形都可以在屏幕上拖放。我正在使用 pygame。我设法绘制了 1 个可以拖放的矩形,但我不能用 5 个来完成。这是我的代码:

import pygame
from pygame.locals import *
from random import randint


SCREEN_WIDTH = 1024
SCREEN_HEIGHT = 768

BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)
pygame.init()

screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()

pygame.display.set_caption("Moving circles")

rectangle = pygame.rect.Rect(20,20, 17, 17)
rectangle_draging = False


clock = pygame.time.Clock()

running = True

while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            if event.button == 1:            
                if rectangle.collidepoint(event.pos):
                    rectangle_draging = True
                    mouse_x, mouse_y = event.pos
                    offset_x = rectangle.x - mouse_x
                    offset_y = rectangle.y - mouse_y

        elif event.type == pygame.MOUSEBUTTONUP:
            if event.button == 1:            
                rectangle_draging = False

        elif event.type == pygame.MOUSEMOTION:
            if rectangle_draging:
                mouse_x, mouse_y = event.pos
                rectangle.x = mouse_x + offset_x
                rectangle.y = mouse_y + offset_y


    screen.fill(WHITE)
    pygame.draw.rect(screen, RED, rectangle)

    pygame.display.flip()

clock.tick(FPS)


pygame.quit()

我想这是最重要的部分:

  pygame.draw.rect(screen, RED, rectangle)

每次我尝试绘制其中的 5 个时,我都无法拖动它们中的任何一个。有人对此有解决方案吗?

【问题讨论】:

  • 你能展示其中一项失败的努力吗?我怀疑您可能正在尝试重用 rectangle 变量...
  • @GlennRogers 是的,我确实尝试过,因为我不知道该怎么做。

标签: python python-3.x pygame


【解决方案1】:

您可以创建一个矩形列表和一个指向当前选定矩形的selected_rect 变量。在事件循环中检查其中一个矩形是否与 event.pos 碰撞,然后将selected_rect 设置为鼠标光标下的矩形并移动它。

我将pygame.math.Vector2 用于offset 以在示例中节省几行代码。

import sys
import pygame as pg
from pygame.math import Vector2


pg.init()

WHITE = (255, 255, 255)
RED   = (255,   0,   0)

screen = pg.display.set_mode((1024, 768))

selected_rect = None  # Currently selected rectangle.
rectangles = []
for y in range(5):
    rectangles.append(pg.Rect(20, 30*y, 17, 17))
# As a list comprehension.
# rectangles = [pg.Rect(20, 30*y, 17, 17) for y in range(5)]

clock = pg.time.Clock()
running = True

while running:
    for event in pg.event.get():
        if event.type == pg.QUIT:
            running = False
        elif event.type == pg.MOUSEBUTTONDOWN:
            if event.button == 1:
                for rectangle in rectangles:
                    if rectangle.collidepoint(event.pos):
                        offset = Vector2(rectangle.topleft) - event.pos
                        selected_rect = rectangle
        elif event.type == pg.MOUSEBUTTONUP:
            if event.button == 1:
                selected_rect = None
        elif event.type == pg.MOUSEMOTION:
            if selected_rect:
                selected_rect.topleft = event.pos + offset

    screen.fill(WHITE)
    for rectangle in rectangles:
        pg.draw.rect(screen, RED, rectangle)

    pg.display.flip()
    clock.tick(30)

pg.quit()
sys.exit()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多