pygame how to check mouse coordinates
Solution 1
you can just do
x, y = pygame.mouse.get_pos()
then
print(x, y)
Lets say we wanted to make some Flying blocks from one position to another. We could do something like this
import pygame
from pygame import *
import sys, random, math, fractions
pygame.init()
Screen_Width = 800
Screen_Height = 600
Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
clock = pygame.time.Clock()
class Blocks(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
all_sprite_list.add(self)
block_list.add(self)
self.image = pygame.Surface((32,32))
self.rect = self.image.get_rect()
self.change_y = 0
self.change_x = 0
def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
block_vec_x = cursor_pos_x - player_pos_x
block_vec_y = cursor_pos_y - player_pos_y
vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
block_vec_y = (block_vec_y / vec_length) * 5
block_vec_x = (block_vec_x / vec_length) * 5
self.change_y += block_vec_y
self.change_x += block_vec_x
def update(self):
self.rect.y += self.change_y
self.rect.x += self.change_x
Now well make a simple player class just for a position to fire the blocks to the mouse position well place this right in the middle of the window!
class Player(pygame.sprite.Sprite):
def __init__(self):
player_list.add(self)
self.rect = Rect(400,300,16,16)
self.image = pygame.surface((16,16))
all_sprite_list = pygame.sprite.Group()
player_list = pygame.sprite.Group()
block_list = pygame.sprite.Group()
block = Blocks(16,16)
player = Player()
running = True
while running:
clock.tick(60)
for e in pygame.event.get()
if e == pygame.Quit:
running = False
if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
running = False
Here is where we get the mouse position we set Mouse_X and Mouse_Y = mouse.get_pos() which will output (x, y) or Mouse_X = mouse pos x and Mouse_Y = mouse pos y. You can also out put the position of the mouse by adding a print(Mouse_x & " " & Mouse_y)
Mouse_x, Mouse_y = pygame.mouse.get_pos()
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
block = Blocks(16,16)
block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
block.rect.x = player.rect.x
block.rect.y = player.rect.y
block.update()
Total_Display.fill((255,0,0))
for sprite in all_sprite_list:
pygame.draw.rect(Total_Display,(0,0,0), sprite)
for blocks in block_list:
pygame.draw.rect(Total_Display, (0,0,0), block)
pygame.display.flip()
I probably went a little overboard but hey hope this helps someone out there! And here's the entire thing just to make sure I didn't make a mistake
import pygame
from pygame import *
import sys, random, math, fractions
pygame.init()
Screen_Width = 800
Screen_Height = 600
Total_Display = pygame.display.set_mode((Screen_Width, Screen_Height))
clock = pygame.time.Clock()
class Blocks(pygame.sprite.Sprite):
def __init__(self, width, height):
super().__init__()
all_sprite_list.add(self)
block_list.add(self)
self.image = pygame.Surface((32,32))
self.rect = self.image.get_rect()
self.change_y = 0
self.change_x = 0
def blockMove (self, cursor_pos_x, cursor_pos_y, player_pos_x, player_pos_y):
block_vec_x = cursor_pos_x - player_pos_x
block_vec_y = cursor_pos_y - player_pos_y
vec_length = math.sqrt(block_vec_x ** 2 + block_vec_y ** 2)
block_vec_y = (block_vec_y / vec_length) * 5
block_vec_x = (block_vec_x / vec_length) * 5
self.change_y += block_vec_y
self.change_x += block_vec_x
def update(self):
self.rect.y += self.change_y
self.rect.x += self.change_x
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
player_list.add(self)
all_sprite_list.add(self)
self.rect = Rect(400,300,16,16)
self.image = pygame.Surface((16,16))
all_sprite_list = pygame.sprite.Group()
player_list = pygame.sprite.GroupSingle()
block_list = pygame.sprite.Group()
block = Blocks(16,16)
player = Player()
running = True
while running:
clock.tick(60)
for e in pygame.event.get():
if e == pygame.QUIT:
Running = False
if e.type == pygame.KEYDOWN and e.type == pygame.K_ESCAPE:
running = False
Mouse_x, Mouse_y = pygame.mouse.get_pos()
key = pygame.key.get_pressed()
if key[pygame.K_SPACE]:
block = Blocks(16,16)
block.update()
block.blockMove(Mouse_x, Mouse_y, player.rect.x, player.rect.y)
block.rect.x = player.rect.x
block.rect.y = player.rect.y
block.update()
Total_Display.fill((255,0,0))
for sprite in all_sprite_list:
pygame.draw.rect(Total_Display,(0,0,0), sprite)
for blocks in block_list:
pygame.draw.rect(Total_Display, (0,0,0), block)
pygame.display.flip()
Solution 2
You can use pygame.mouse.get_pos()
(docs)
pygame.mouse.get_pos()
get the mouse cursor position get_pos() -> (x, y)
Returns the X and Y position of the mouse cursor. The position is relative the the top-left corner of the display. The cursor position can be located outside of the display window, but is always constrained to the screen.
Solution 3
Well, first of all, you would need the mouse coordinates. You could do that like this:
mousex, mousey = pygame.mouse.get_pos()
Then you would need a function that would check if the mouse is inside of the circles. To do this you would need a hitbox for the circles. A circular hitbox is hard to make so you could use a square one by doing the following:
def check_pressed ():
mousex, mousey = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()
if click != (0, 0, 0):
if mousex >= buttonx and mousex <= buttonx + button_width and
mousey >= buttony and mousey <= buttony + button_height:
#run function to make button bigger and run a clicked event
Add this check_pressed function to your main loop. Hope this helps!
jason97931
Updated on June 21, 2020Comments
-
jason97931 almost 4 years
I am making a multiple choice quiz game with python and pygame. I have circles and what a want is when you click on the circle it will change the width and make the circle full. But I don't know of a python function that can do that
Heres my code so far:
# Controls the width of the circles width_1 = 2 width_2 = 2 width_3 = 2 width_4 = 2 # Circles pygame.draw.circle(screen, BLACK, [250, 230], 7, width_1) pygame.draw.circle(screen, BLACK, [250, 260], 7, width_2) pygame.draw.circle(screen, BLACK, [250, 290], 7, width_3) pygame.draw.circle(screen, BLACK, [250, 320], 7, width_4) for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
I want to have something so that if the mouse is hovering over the circles AND mousebuttondown AND event.button == 1 then change the width to 0 (Fill it in)
Thanks in advance!
P.S. This is not all my code