I use yolo to detect objects
when AI detects the blue bubble, the mouse moves to the blue bubble
How do I make the mouse automatically click on the blue bubble when I hover over the blue bubble
def is_click_enabled():
return True if click_status == colored("ENABLED", 'green') else False
def is_targeted():
return True if win32api.GetKeyState(0x01) in (-127, -128) else False
I tried to use mouse.click but it wasn't good
Related
Using the graphics.py library, Write a program to draw 3 rectangles on the screen. If the user clicks the left mouse button inside one of the rectangles change its color. Feel free to use whatever colors you like. If the user clicks inside the graphics window, but outside one of the three rectangles, close the window and exit the program.
When you click on a rectangle it changes colors then you should be able to click on the next and change its color and so on until you click outside. But as soon as I click the first rectangle and it changes colors it ends the program with object already drawn error.
Result: if self.canvas and not self.canvas.isClosed(): raise GraphicsError(OBJ_ALREADY_DRAWN)
Here is the code:
win3 = graphics.GraphWin("",500,500)
rect1 = graphics.Rectangle(graphics.Point(150,100),graphics.Point(350,200))
rect2 = graphics.Rectangle(graphics.Point(150,220),graphics.Point(350,320))
rect3 = graphics.Rectangle(graphics.Point(150,340),graphics.Point(350,440))
rect1.draw(win3)
rect2.draw(win3)
rect3.draw(win3)
def inside(point, rectangle): # checks if the click is inside of the rectangle
ll = rectangle.getP1() # lower left corner of the rectangle
ur = rectangle.getP2() # upper right corner of the rectangle
return ll.getX() < point.getX() < ur.getX() and ll.getY() < point.getY() < ur.getY()
outside = False
while outside == False:
click = win3.getMouse()
if inside(click,rect1):
rect1.setFill("Red")
rect1.draw(win3)
elif inside(click,rect2):
rect2.setFill("Green")
rect2.draw(win3)
elif inside(click,rect3):
rect3.setFill("Blue")
rect3.draw(win3)
else:
win3.close()
outside = True
I have generated a circle with graphic.py. I want the color of the circle to change with a mouse click. How can I create a mouse event for this?
def draw_circle():
#make green circle
win = GraphWin('Traffic Lights', 400,400)
win.setBackground(color_rgb(211,211,211))
pt = Point(200,200)
cir = Circle(pt,50)
cir.setFill('Green')
cir.draw(win)
win.getMouse
initial()
def click:
#action to take place
# when mouse cicked
add_mouse_click_handler(click)
main()
I have the following code:
drawing = False # true if mouse is pressed
mode = True # if True, draw rectangle. Press 'm' to toggle to curve
ix,iy = -1,-1
# mouse callback function
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv2.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv2.EVENT_MOUSEMOVE:
if drawing == True:
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),20,(0,0,255),-1)
elif event == cv2.EVENT_LBUTTONUP:
drawing = False
if mode == True:
cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv2.circle(img,(x,y),20,(0,0,255),-1)
I don't understand the use of xi and yi on the rectangle. There is a line which stated that xi,yi = x,y. Would it be the same? How would it draw rectangle? From what I understand, to draw a rectangle, there should be two sets of coordinates. I can't see which part in the line that shows that ix,iy will be different with x and y value.
Can anyone explain?
Its pretty straightforward here is what happens in steps:
the user clicks down - current mouse position x,y is stored as ix,iy and drawing is set to true
the user moves the mouse - if drawing is true and mode is true draws a rectangle using the saved ix,iy and the new current x,y (note that ix,iy does NOT equal x,y anymore since its only saved when the mouse button down triggers)
the user releases the mouse - drawing is set to false so the mouse can be moved without drawing anything and any current images are saved
#Imported Pygame
import pygame
#The Colors
BLACK = ( 0, 0, 0)
GREEN = ( 0, 255, 0)
WHITE = ( 255, 255, 255)
RED = ( 255, 0, 0)
ORANGE = ( 255, 115, 0)
YELLOW = ( 242, 255, 0)
BROWN = ( 115, 87, 39)
PURPLE = ( 298, 0, 247)
GRAY = ( 168, 168, 168)
PINK = ( 255, 0, 234)
pygame.init()
#The Screen
screen = pygame.display.set_mode([1000,500])
#Name of the window
pygame.display.set_caption("My first game")
clock = pygame.time.Clock()
#The sounds
# Positions of graphics
background_position = [0,0]
singleplayer_position = [350, 200]
tutorial_position = [350,300]
sorry_position = [0,0]
developer_position = [0,450]
rules_position = [0,0]
#The graphics
background_image = pygame.image.load("Castle.png").convert()
singleplayer_image = pygame.image.load("SinglePlayer.png").convert()
singleplayer_image.set_colorkey(WHITE)
tutorial_button = pygame.image.load("Tutorial_button.png").convert()
sorry_message = pygame.image.load("Sorry.png").convert()
sorry_message.set_colorkey(WHITE)
developer_message = pygame.image.load("Developer.png").convert()
developer_message.set_colorkey(WHITE)
Rules_image = pygame.image.load("Rules.png").convert()
#Main Loop __________________________
done = False
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# Copy of background or main menu
screen.blit(background_image, background_position)
#Copy of other images
mouse_pos = pygame.mouse.get_pos()
my_rect = pygame.Rect(350,200,393,75)
tutorial_rect = pygame.Rect(350,300,393,75)
screen.blit(singleplayer_image, singleplayer_position)
screen.blit(tutorial_button, tutorial_position)
screen.blit(developer_message, developer_position)
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
screen.blit(sorry_message, sorry_position)
correct = False
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
#Here I make the screen fill white
if python.mouse.get_pressed()[0]tutorial_rect.collidepoint(mouse.pos):
correct = True
if correct == True:
screen.blit(Rules_image, rules_position)
pygame.display.flip()
clock.tick(60)
#To quit game
pygame.quit()
This is basically my code... When I hit the single player button I have it making the area white but it doesn't stay there. Like when I hit it once and hold the singleplayer button it stays white but when i unclick the screens back to what it was. Is there anyway I can just erase everything I did before and start a new screen when I hit the Singleplayer button?'
Ok back to the answer you gave me..
I structured my code like you said.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
color_white = True
if color_white = True
screen.fill(WHITE)
This isn't working because It still doesn't make the screen stay white.
I tried this.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
color_white = True
if color_white = True
screen.fill(WHITE)
This also doesn't seem to work because it keeps saying color_white is undefined.
Your confusion results from the while loop and how it behaves, so I'll explain that to answer your question.
Quick note: if you are not using a pygame clock object with tick at end of code, comment and I'll explain that at end, its important you do this.(http://www.pygame.org/docs/ref/time.html)
Okay, the problem: your picture is not remaining white after you click it. It stays white if you hold the mouse down, but it goes away once you lift up. I assume you want it to remain white even once you lift the mouse click.
Currently, your code colors the picture white inside of an if statement.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
Review the docs on what .get_pressed() does. It returns True if the mouse button is pressed. So, when you click it, it says True, if you are holding it down, it says True. If you are not clicking or holding, its False. So it only colors it white when the mouse is clicked or held down, since thats when its told to do so. What makes it turn back to normal are your blits earlier in the loop. Each loop, pygame makes the image normal (via blit) and colors the picture white if your statement evaluates to True. This means whenever your if statement is False, the picture remains normal.
To make it remain painted white, use a boolean.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
color_white = True
And then instead of putting the code to color the white inside the if statement that now sets the boolean to true, make a new if statement before your loop ends.
if color_white:
# Code to make the screen white.
This way, it can remain white even while not holding it down. If you want to make it back to normal with another click. You can expand your first if statement.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
if color_white is True:
color_white = False
else:
color_white = True
Which can be coded in a shorter fashion...
color_white = False if color_white == True else True
Edit: I wrote the previous code considering events. This code would work if you were using the MOUSEBUTTONDOWN event to change the color. However, if you want to use get_pressed(), you'll have to use a different mouse button. If you only use left click, how should the program know whether to turn it off or on with so many loops going by?
I'll rewrite the code with get_pressed in mind.
if pygame.mouse.get_pressed()[0] and my_rect.collidepoint(mouse_pos):
color_white = True
if pygame.mouse.get_pressed()[1] and my_rect.collidepoint(mouse_pos): # Not sure if it should be 1 or 2 in get_pressed, but I'm assuming they represent the right click and middle mouse button. So you can use these to turn the screen back to normal.
color_white = False
Edit2: Your color_white is undefined, because it doesn't get defined until after the if statements in your code. So before you get a chance to click (and define it), a loop runs and gets to
if color_white:
But color_white doesn't exist to the computer yet. To solve, define color_white before your while loop.
color_white = False # Default to not color the screen white.
I have an image:
newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha()
I then display it on the screen:
screen.blit(newGameButton, (0,0))
How do I detect if the mouse is touching the image?
Use Surface.get_rect to get a Rect describing the bounds of your Surface, then use .collidepoint() to check if the mouse cursor is inside this Rect.
Example:
if newGameButton.get_rect().collidepoint(pygame.mouse.get_pos()):
print "mouse is over 'newGameButton'"
I'm sure there are more pythonic ways to do this, but here is a simple example:
button_x = 0
button_y = 0
newGameButton = pygame.image.load("images/newGameButton.png").convert_alpha()
x_len = newGameButton.get_width()
y_len = newGameButton.get_height()
mos_x, mos_y = pygame.mouse.get_pos()
if mos_x>button_x and (mos_x<button_x+x_len):
x_inside = True
else: x_inside = False
if mos_y>button_y and (mos_y<button_y+y_len):
y_inside = True
else: y_inside = False
if x_inside and y_inside:
#Mouse is hovering over button
screen.blit(newGameButton, (button_x,button_y))
Read more on the mouse in pygame, as well as surfaces in pygame.
Also here is an example closely related to this.