0
我想绘制5个矩形,我可以在屏幕上拖放所有的矩形。我正在使用pygame。我设法使1个矩形,我可以拖放,但我不能5.做这是我的代码: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是的,我尝试过,因为我不知道如何以任何其他方式做到这一点。 – Nenad