224k views
2 votes
App.background = 'sienna'

Rect(0, 205, 210, 135, fill='silver')
Line(0, 274, 200, 276, lineWidth=115, dashes=(10, 10))
Line(100, 210, 99, 340, fill='silver', lineWidth=200, dashes=(11, 12))
Rect(-100, -50, 310, 260, fill=gradient('aqua', 'royalBlue', 'midnightBlue'))
Line(0, 205, 210, 205, lineWidth=3)

cursor = Polygon(100, 90, 100, 109, 104, 105, 107, 111, 111, 109, 108, 103,
113, 103, border='white', borderWidth=1)

def onMouseMove(mouseX, mouseY):
# Only move the cursor if your mouse is on the left or top halves of the canvas.
### Place Your Code Here ###
pass

2 Answers

1 vote

Answer with Explanation:

The code provided is written in Python and includes instructions for drawing shapes and lines on a canvas. It also defines a function called `onMouseMove` that is triggered when the mouse is moved.

To complete the code and make the cursor move based on the mouse's position on the canvas, you need to add the appropriate logic inside the `onMouseMove` function. Specifically, you need to check if the mouse is in the top-left, bottom-left, top-right, or bottom-right quadrant of the canvas.

Here are the steps to complete the code:

1. Move the mouse on the top-left:

- Uncomment the code inside the `onMouseMove` function.

- Replace the comment `### Place Your Code Here ###` with the following code:

```python

if mouseX <= canvas.width/2 and mouseY <= canvas.height/2:

cursor.moveTo(mouseX, mouseY)

```

- Save the changes.

2. Move the mouse on the bottom-left:

- Add the following code after the previous step:

```python

elif mouseX <= canvas.width/2 and mouseY > canvas.height/2:

cursor.moveTo(mouseX, mouseY)

```

- Save the changes.

3. Move the mouse on the top-right:

- Add the following code after the previous step:

```python

elif mouseX > canvas.width/2 and mouseY <= canvas.height/2:

cursor.moveTo(mouseX, mouseY)

```

- Save the changes.

4. Move the mouse on the bottom-right:

- Add the following code after the previous step:

```python

elif mouseX > canvas.width/2 and mouseY > canvas.height/2:

cursor.moveTo(mouseX, mouseY)

```

- Save the changes.

User Yoann Kergall
by
8.3k points
1 vote

The student's question involves writing a Python function to update the position of a cursor on a canvas but only when the mouse is within certain areas. The onMouseMove function should be completed with logic to detect if the mouse is on the left or top halves of the canvas before changing the cursor position.

The student is asking about a Python script that involves drawing shapes and lines on a canvas, and updating a cursor's position based on mouse movement. The code provided defines shapes using a specific Python library for creating graphical user interfaces, likely one that is used in an educational context such as 'tkinter' or a web-based canvas. The onMouseMove function is meant to be completed to allow for cursor movement within specific regions of the canvas as per the commented instructions.

To move the cursor only when the mouse is on the left or top halves of the canvas, one could write:

def onMouseMove(mouseX, mouseY):

if mouseX < 210 / 2 or mouseY < 340 / 2:

cursor.centerX = mouseX

cursor.centerY = mouseY

Here, two conditions are used to check if the mouse pointer is in the left half (by dividing the width by 2) or the top half (by dividing the height by 2) of the canvas before updating the cursor's position. This would ensure the cursor moves only within the specified regions.

User Priyanka Mishra
by
8.5k points