我正在创建一个类似的游戏流。如果你熟悉它,它需要用户通过网格上相同颜色的线来匹配相同的圆。我有一个问题,哪里有鼠标运动事件,我希望用户只能画直线(左,右,上,下)。目前用户可以在任何地方画画,我有一个功能,每当鼠标移动事件发生时,我都会画圆圈,虽然我用了一些if语句来限制用户可以画的地方,但它仍然不起作用。也许有更好的方法,而不是使用鼠标运动事件?我不确定,但任何帮助都是非常感谢!在
下面是一些代码:''' *** 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")