[英]How to click and drag an object in pygame?
我創建了這個窗口,允許用戶在窗口中創建點並鏈接它們,我現在要做的是允許他在完成繪制后移動他想要的任何點
在我的例子中我讓他畫五個點,然后我想讓他移動他選擇的點,我真的不知道該怎么做
這是我的代碼:
import pygame
#Initialise pygame
pygame.init()
#Create the screen
screen = pygame.display.set_mode((1200, 700))
#Change the title and the icon
pygame.display.set_caption('The Thoughtful Minds')
icon = pygame.image.load('IA.png')
pygame.display.set_icon(icon)
#Dots
dot = pygame.image.load('point.png')
class Dot:
def __init__(self, pos):
self.cx, self.cy = pos
def draw(self):
screen.blit(dot,(self.cx-8 , self.cy-8))
def text_objects(text,font):
textSurface = font.render(text, True, (100,100,100))
return textSurface, textSurface.get_rect()
dots = []
#Running the window
i = 0
running = True
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.MOUSEBUTTONDOWN:
for oPosition in dots:
if ((oPosition.cx - 8) < mx < (oPosition.cx + 8)) and ((oPosition.cy - 8) < my < (oPosition.cy + 8)):
'''
What should I put her to allow him to move the dot he clicked on
'''
if i<3:
# append a new dot at the current mouse position
dots.append(Dot((mx,my)))
i += 1
# clear the display
screen.fill((30,30,30))
# draw all the dots
if len(dots)>1:
for i in range(len(dots)-1):
pygame.draw.line(screen, (50, 50, 50), [dots[i].cx,dots[i].cy],[dots[i+1].cx,dots[i+1].cy],2 )
for d in dots:
d.draw()
if mx < 50 and my < 50:
pygame.draw.rect(screen,(24,24,24),(0,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(0,0,50,50))
text = pygame.font.Font("freesansbold.ttf",25)
textSurf, textRect = text_objects('–', text)
textRect.center = (25,25)
screen.blit( textSurf, textRect )
if 52 < mx < 102 and my < 50:
pygame.draw.rect(screen,(24,24,24),(52,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(52,0,50,50))
textSurf, textRect = text_objects('+', text)
textRect.center = (76,25)
screen.blit( textSurf, textRect )
# update the dispalay
pygame.display.flip()
謝謝。
pygame 是低級的,它沒有任何預先實現的拖放方法。 你必須自己構建它。
您必須處理各種事件類型。 這是一個想法:
首先,在主循環外創建一個變量dragged_dot = None
。 並在事件循環中檢查以下事件:
pygame.MOUSEBUTTONDOWN
事件會告訴您何時按下按鈕。 當您檢測到此事件時,請檢查鼠標是否單擊了現有的點。 這就是你的for
循環所做的。 如果沒有,請像您已經在做的那樣添加一個新點。 否則,設置要拖動的點: dragged_dot = oPosition
。pygame.MOUSEMOTION
事件告訴您鼠標何時移動。 當您檢測到此事件時,請檢查是否有拖動點: if dragged_dot is not None
。 如果是這樣,編輯它的坐標,添加鼠標運動,以便它可以在新位置重新繪制(記住刪除上一個位置的點圖像)。 使用event.rel
了解之前鼠標位置和當前鼠標位置之間的差異。pygame.MOUSEBUTTONUP
事件會告訴您按鈕何時被釋放。 只需設置dragged_dot = None
,這樣點就會被丟棄並且不再跟隨鼠標移動。好的,如果您有興趣,這就是答案
import pygame
#Initialise pygame
pygame.init()
#Create the screen
screen = pygame.display.set_mode((1200, 700))
screen.set_alpha(None)
#Change the title and the icon
pygame.display.set_caption('The Thoughtful Minds')
icon = pygame.image.load('IA.png')
pygame.display.set_icon(icon)
#Dots
dot = pygame.image.load('point.png')
class Dot:
def __init__(self, pos):
self.cx, self.cy = pos
def draw(self):
screen.blit(dot,(self.cx-8 , self.cy-8))
def text_objects(text,font):
textSurface = font.render(text, True, (100,100,100))
return textSurface, textSurface.get_rect()
dots = []
#Running the window
i = 0
running = True
draging = False
while running:
mx, my = pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
pass
elif event.type == pygame.MOUSEBUTTONDOWN:
for oPosition in dots:
if ((oPosition.cx - 8) < mx < (oPosition.cx + 8)) and ((oPosition.cy - 8) < my < (oPosition.cy + 8)):
draging = True
break
if i<3:
# append a new dot at the current mouse position
dots.append(Dot((mx,my)))
i += 1
elif event.type == pygame.MOUSEBUTTONUP:
draging = False
elif event.type == pygame.MOUSEMOTION:
if draging :
oPosition.cx = mx
oPosition.cy = my
# clear the display
screen.fill((30,30,30))
# draw all the dots
if len(dots)>1:
for i in range(len(dots)-1):
pygame.draw.line(screen, (50, 50, 50), [dots[i].cx,dots[i].cy],[dots[i+1].cx,dots[i+1].cy],2 )
for d in dots:
d.draw()
if mx < 50 and my < 50:
pygame.draw.rect(screen,(24,24,24),(0,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(0,0,50,50))
text = pygame.font.Font("freesansbold.ttf",25)
textSurf, textRect = text_objects('–', text)
textRect.center = (25,25)
screen.blit( textSurf, textRect )
if 52 < mx < 102 and my < 50:
pygame.draw.rect(screen,(24,24,24),(52,0,50,50))
else:
pygame.draw.rect(screen,(20,20,20),(52,0,50,50))
textSurf, textRect = text_objects('+', text)
textRect.center = (76,25)
screen.blit( textSurf, textRect )
# update the dispalay
pygame.display.flip()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.