我正在爲Flow創建類似的遊戲。如果您熟悉它,則需要用戶通過網格上相同顏色的線將相同的圓圈相匹配。我遇到了一個問題,無論哪裏出現鼠標移動事件,我都希望用戶只能用直線繪製(左,右,上,下)。目前用戶可以在任何地方繪製,我有一個函數可以在鼠標移動事件發生時繪製圓圈,儘管我已經使用了一些if語句來限制用戶可以繪製的位置,但仍然無法使用。也許有更好的方法,而不是使用鼠標移動事件?我不確定,但任何幫助非常感謝!在pygame中用鼠標繪製直線?
下面是一些代碼:
''' *** IN PROGRESS *** '''
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True # Closes the game and exits the loop
elif event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
pygame.mouse.set_visible(True)
''' If the user clicks down on the left button the mouse coordinates at that point are assigned to variables
x and y which are used to check the condition of the click_detection function'''
elif event.type == pygame.MOUSEBUTTONUP:
pygame.mouse.set_visible(True)
# Makes the cursor visible to choose a new circle easily
elif event.type == pygame.MOUSEMOTION:
''' State is assigned to an array for each of three mouse buttons
at state[0] it checks the left mouse button'''
state = pygame.mouse.get_pressed()
for circle in circle_grid_list:
# Checks following conditions for every circle in that Sprite group
if ClickDetection.click_detection(circle) == True:
''' Checks whether a circle is being clicked
- If so then variable colour is assigned to the colour of the clicked circle
- This is used so that the move method from circle_line class can be called using the colour of the clicked circle'''
colour = circle.colour
# *** WORK IN PROGRESS ***
if MovementChecker.check() != False:
print("can draw")
if state[0] == 1:
# Checking the left mouse button state, if 1 then button is being clicked or held down
moving_line_mouse.Move()
elif MovementChecker.check() == False:
# Used to stop the move method for the moving_line object from being called if movement checker is false
print("Can't draw")
我從未聽過任何Python遊戲的名字 –
您能否提供該遊戲代碼的鏈接以便我們看到它? –
檢查您的鼠標是否在水平**或**垂直網格位置之外移動,如果是,則拒絕。按網格元素大小劃分三角形鼠標x和y;其中只有一個可能是1. – usr2564301