[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[pygame] ** NEWBIE ** Rect.collidepoint() problems
Hello all,
My name is Chris McCormick, and I am a newbie to the world of pyGame. I did some programming in Python last year, have been away from it for a while, and am now returning, and learning
SDL/pyGame at the same time.
I've created a simple tic tac toe program to learn some of the basics of mouse event handling, among other stuff. I am getting some weird behavior with the Rect.collidepoint() method. In brief, I am
getting hits where I shouldn't. I stripped out the irrelevent stuff to create the example below, which still exhibits the weird behavior. Can someone tell me where I'm going wrong here? I assume that the colliderect()
method is a shorthand for seeing that the point's x and y coordinates fall within the range of the corresponding coordinates for the rectangle. Is that right?
Thanks in advance for any help,
Chris McCormick
PS - If you run the example below, it will clearly list out all hits. Change the coords variable to try out different inputs.
<------ Start of code -------->
#! /usr/bin/env python
## tic_tac_toe.py - A learning exercise
import pygame
def main():
#Initialize Everything
rect_list = []
rect1 = pygame.Rect(20,20,70,70) # Rectangle for tile1
rect2 = pygame.Rect(90,20,140,70) # Rectangle for tile2
rect3 = pygame.Rect(160,20,210,70) # Rectangle for tile3
rect4 = pygame.Rect(20,90,70,140) # Rectangle for tile4
rect5 = pygame.Rect(90,90,140,140) # Rectangle for tile5
rect6 = pygame.Rect(160,90,210,140) # Rectangle for tile6
rect7 = pygame.Rect(20,160,70,210) # Rectangle for tile7
rect8 = pygame.Rect(90,160,140,210) # Rectangle for tile8
rect9 = pygame.Rect(160,160,210,210) # Rectangle for tile9
# Create list of squares
rect_list.append(rect1)
rect_list.append(rect2)
rect_list.append(rect3)
rect_list.append(rect4)
rect_list.append(rect5)
rect_list.append(rect6)
rect_list.append(rect7)
rect_list.append(rect8)
rect_list.append(rect9)
# Coordinates to check in rectangle
coords = (181,179)
# Loop to check for point in rectangles
for i in range(9):
print "Rect", i, rect_list[i],
if rect_list[i].collidepoint(coords):
print "Match - ", coords
else:
print "No match"
if __name__ == '__main__': main()
<------ End of code ----->
____________________________________
pygame mailing list
pygame-users@seul.org
http://pygame.seul.org