Apoorv
Apoorv

Reputation: 383

is the display inverted in pygame?

I am building a game in which a ball is supposed to bounce off the edges of the screen but it seems that the display surface's rect is inverted because when the ball hits an edge, code prints out the other side (for example, when the ball hits the bottom edge, it prints 'top')

here is the code:

import pygame

def coll_side(obj1, obj2, is_obj1_rect, is_obj2_rect):
    if is_obj1_rect:
        rect1 = obj1
    else:
        rect1 = obj1.rect
    if is_obj2_rect:
        rect2 = obj2
    else:
        rect2 = obj2.rect

    tl = rect1.collidepoint(rect2.topleft)
    bl = rect1.collidepoint(rect2.bottomleft)
    tr = rect1.collidepoint(rect2.topright)
    br = rect1.collidepoint(rect2.bottomright)

    if tl and bl:
        side = 'left'
    elif tl and tr:
        side = 'top'
    elif br and tr:
        side = 'right'
    elif br and bl:
        side = 'bottom'
    elif tl:
        side = 'topleft'
    elif tr:
        side = 'topright'
    elif bl:
        side = 'bottomleft'
    elif br:
        side = 'bottomright'
    else:
        side = None    

    print side
    return side

class Ball(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.area = pygame.display.get_surface()
        self.rect = pygame.Rect((295, 455), (10,10))

    def update(self):
        if not self.area.contains(self.rect):
            side = coll_side(self.area.get_rect(), self.rect, True, True)

So, is there any problem with my code or pygame's display rect is inverted ?

Thanks

EDIT: Added link for screenshot

http://postimg.org/image/dcc3h50k9/

^in this scenario, the console outputs 'left'

Upvotes: 0

Views: 1020

Answers (2)

user890167
user890167

Reputation:

Rect.collidepoint(x, y) checks to see if the x, y coordinates you pass in are inside the Rect you're calling it on. So screen.rect.collidepoint(ball.x, ball.y) for example returns a bool after evaluating "Does this Rect contain this exact point?"

From the PyGame documentation:

collidepoint()

test if a point is inside a rectangle

collidepoint(x,y) -> bool

collidepoint((x,y)) -> bool

Returns true if the given point is inside the rectangle. A point along the right or bottom edge is not considered to be inside the rectangle.

In other words, I don't think this does what you want it to do in the first place. You're not establishing a bounds for the ball rect to stay in; you're just evaluating whether or not the rect has any of those four points inside of it.

Therefore, if you are using screen.rect as rect1 and ball.rect as rect2 and you are using a series of if/elif/elif as you are currently doing, then the first statement on your list - does the screen rect contain both the topleft and bottomleft corners of the ball rect - will typically evaluate to True, and it will not bother with the rest of the elifs.

If the ultimate goal of this function is to evaluate whether or not one Rect is entirely within another, you can use obj.rect.contains(obj2.rect), which will return True or False depending on whether or not the second Rect is entirely inside the first.

Upvotes: 1

unwind
unwind

Reputation: 399793

The PyGame tutorial says:

In pygame we always pass positions as an (X,Y) coordinate. This represents the number of pixels to the right, and the number of pixels down to place the image.

That's the standard coordinate system for 2D computer graphics.

Upvotes: 2

Related Questions