Question

Write a program that changes the color of a circle based on whether the mouse cursor...

Write a program that changes the color of a circle based on whether the mouse cursor is in the top half of the display or the bottom half. Here are the specific requirements. a. Set the display size to 600x400 pixels b. Using the line function, draw a horizontal line that is centered vertically from the left side to the right side of the display c. Using the ellipse function, draw a circle of size 100x100 anywhere on the display d. If the mouse cursor is in the top half of the display, the color of the circle should be blue [hint: use fill(0,0,255)] e. If the mouse cursor is in the bottom half of the display, the color of the circle should be red [hint: use fill(255,0,0)]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The code below uses pygame library of python to draw elements and achieve the mentioned objective.

The code is well commented to understand the functioning. Following are some points that would clarify understanding:

1. Pygame considers the Origin (0,0) at Top-Left Corner

2. x-coordinates increase from left to right and y-coordinates from top to bottom.

3. The function of ellipse plotting requires 4 parameters (x, y, w, h) where w, h signifies the major and minor axis of the ellipse. But, since we are using it to plot a circle, major-axis=minor-axis=circle_diameter=100. So, w=h=100 is given. x, y determine the positioning of the ellipse.

----------------------------------------------------------------------------------------------------

Python 3 Code: Change Color of Circle based on Mouse Position

----------------------------------------------------------------------------------------------------

# importing pygame library
import pygame

# To decide if the window is still running or not
running = 1

# Specifying dimensions of display screen
width = 600
height = 400

# creating a screen of above dimensions
screen = pygame.display.set_mode((width, height))

# Specifying color of line and background
linecolor = 255, 255, 0
bgcolor = 0, 0, 0

# While the screen is running
while running:
   # untill the event is of Quit type, we continue running the screen & do following
   event = pygame.event.poll()
   if event.type == pygame.QUIT:
       running = 0

   # Filling the background color as black
   screen.fill(bgcolor)
   # Drawing the line of yellow color at vertical midway
   pygame.draw.line(screen, linecolor, (0, 200), (600, 200))

   # By defualt creating a circle of red color
   pygame.draw.ellipse(screen, pygame.Color("red"), (250, 150, 100, 100))

   # checking if the current mouse cordinate is in top half then, coloring the circle blue
   if pygame.mouse.get_pos()[1] < height/2:
       pygame.draw.ellipse(screen, pygame.Color("blue"), (250, 150, 100, 100))
          
   # After drawing all the things, this function is called to output it to screen
   pygame.display.flip()

----------------------------------------------------------------------------------------------------

Sample Output Screen Screenshot was taken when the cursor was in top half:

----------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
Write a program that changes the color of a circle based on whether the mouse cursor...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT