0

Click detection python

Hello, I am in the path to creating a tic tac toe game, in another day or two, each day full time, because i am a beginner, My question is, in this code: import pygame from pygame.locals import * pygame.init() screen = pygame.display.set_mode((600, 600),0,32) background = pygame.image.load("bg.png").convert_alpha() image1 = pygame.image.load("test.png").convert_alpha() image2 = pygame.image.load("test.png").convert_alpha() image3 = pygame.image.load("test.png").convert_alpha() image4 = pygame.image.load("test.png").convert_alpha() image5 = pygame.image.load("test.png").convert_alpha() image6 = pygame.image.load("test.png").convert_alpha() image7 = pygame.image.load("test.png").convert_alpha() image8 = pygame.image.load("test.png").convert_alpha() image9 = pygame.image.load("test.png").convert_alpha() screen.blit(background, (0,0)) #screen.blit(image1, (0, 0)) #screen.blit(image2, (200, 0)) #screen.blit(image3, (400, 0)) #screen.blit(image4, (0, 200)) #screen.blit(image5, (0, 400)) #screen.blit(image6, (200, 200)) #screen.blit(image7, (200, 400)) #screen.blit(image8, (400, 200)) #screen.blit(image9, (400, 400)) pygame.display.update() a = input("Type anything to exit") quit() Its near to chess, but thats not what i want. What i want is, is that wherever i click, i want it you print/etc the x or y of where i clicked, Hope you understood my question.

2nd Nov 2017, 12:42 AM
DarkHorrorOfThirdEye
DarkHorrorOfThirdEye - avatar
2 Answers
+ 2
You may want to check out the pygame documentation. I'm not very familiar with pygame, but it looks like you may want to loop over the events and find the MOUSEBUTTONUP or MOUSEBUTTONDOWN event and get the position from it. Something like: x, y = None, None for event in pygame.event.get(): if event.type == pygame.MOUSEBUTTONDOWN: x, y = event.pos # or maybe pygame.mouse.get_pos() You'll have to do some experimenting and testing to see what you need, this is just my best guess without building a project. Maybe try printing out event.pos or pygame.mouse.get_pos() http://www.pygame.org/docs/ref/event.html http://www.pygame.org/docs/ref/mouse.html
2nd Nov 2017, 2:03 AM
ChaoticDawg
ChaoticDawg - avatar
0
@ChaoticDawg Thanks for your reply, Yes i might i have to put the events in a loop. I first did put them in a loop, because when its in a loop it just opens the pictures infinity times right? I don't know i told you im a beginner.. Anyways im gonna try what you say, Thanks.
2nd Nov 2017, 10:05 AM
DarkHorrorOfThirdEye
DarkHorrorOfThirdEye - avatar