How can I detect if an image has been touched in pygame on a touch screen? I have searched but I can not find how to detect if a particular image is touched.
Asked
Active
Viewed 3,077 times
0
-
1Have you read the [documentation](https://www.pygame.org/docs/ref/touch.html)? – Jan Wilamowski Sep 02 '21 at 04:10
-
yes, I read it but I don't understand it as I am a newbie to python. – Vickash Bharttiya Sep 02 '21 at 04:34
-
I suggest that you first focus on the pygame basics (displaying images, getting mouse input, detecting collision) and then move on to touch device interaction. – Jan Wilamowski Sep 02 '21 at 04:47
-
I am making a flappy bird game but to make it work on android I need to detect touch – Vickash Bharttiya Sep 02 '21 at 04:51
-
If you have never even used Python, let alone handled touch input, this project might be too challenging. Start out with something smaller and simpler, while keeping in mind your end goal. All the things I've listed previously will be necessary to learn, among others. – Jan Wilamowski Sep 02 '21 at 04:58
-
I have made the flappy bird game for laptop – Vickash Bharttiya Sep 02 '21 at 04:59
-
If you already know how to use pygame and you read the documentation then what exactly is your question? You have describe a specific problem with your code to find help here. – Jan Wilamowski Sep 02 '21 at 05:01
-
I am asking is there a simple method to see if an image was touched – Vickash Bharttiya Sep 02 '21 at 05:07
-
[Pygame mouse clicking detection](https://stackoverflow.com/questions/10990137/pygame-mouse-clicking-detection/64533684#64533684) and [How can I add an image or icon to a button rectangle in Pygame?](https://stackoverflow.com/questions/64990710/how-can-i-add-an-image-or-icon-to-a-button-rectangle-in-pygame/64990819#64990819) - works also with touch – Rabbid76 Sep 02 '21 at 05:08
-
Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Sep 05 '21 at 10:35
2 Answers
2
To detect if an image has been touched, you can use the MOUSEBUTTONDOWN
event. See Pygame mouse clicking detection.
Load the image with pygame.image.load()
. Crate the bounding rectangle (pygame.Rect
). Create a pygame.Mask
from the image with pygame.mask.from_surface
:
image = pygame.image.load('image.png').convert_alpha()
image _rect = image.get_rect(topleft = (x, y))
image_mask = pygame.mask.from_surface(image)
Use the MOUSEBUTTONDOWN
event to detect if the mouse is clicked in the rectangular area of the image. Check if the corresponding bit in the image mask is set:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
image_x, image_y = event.pos[0] - image_rect.x, event.pos[1] - image_rect.y
if image_rect.collidepoint(event.pos) and image_mask.get_at((image_x, image_y)):
print("touched")
Minimal example:
import pygame
pygame.init()
window = pygame.display.set_mode((400, 400))
font = pygame.font.SysFont(None, 100)
clock = pygame.time.Clock()
text = font.render("Text", True, (255, 255, 0))
bomb = pygame.image.load('Bomb-256.png').convert_alpha()
bomb_rect = bomb.get_rect(center = window.get_rect().center)
bomb_mask = pygame.mask.from_surface(bomb)
click_count = 0
click_text = font.render('touch: ' + str(click_count), True, "black")
run = True
while run:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
image_x, image_y = event.pos[0] - bomb_rect.x, event.pos[1] - bomb_rect.y
if bomb_rect.collidepoint(event.pos) and bomb_mask.get_at((image_x, image_y)):
click_count += 1
click_text = font.render('touch: ' + str(click_count), True, "black")
window.fill((255, 255, 255))
window.blit(bomb, bomb_rect)
window.blit(click_text, click_text.get_rect(topleft = (20, 20)))
pygame.display.flip()
pygame.quit()
exit()

Rabbid76
- 202,892
- 27
- 131
- 174
-1
If I run this from the raspberry pi desktop the touch is not detected.
But if I execute this from ssh remote it works fine

Danny Constantine
- 21
- 4
-
1As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 19 '22 at 08:36