python3 pygame中画线实例_在pygame中用鼠标画直线?

我正在创建一个类似的游戏流。如果你熟悉它,它需要用户通过网格上相同颜色的线来匹配相同的圆。我有一个问题,哪里有鼠标运动事件,我希望用户只能画直线(左,右,上,下)。目前用户可以在任何地方画画,我有一个功能,每当鼠标移动事件发生时,我都会画圆圈,虽然我用了一些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")

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值