Coding Issues Archive: Help Appreciated - QueenChristina/Neverending-Dream-RPG-in-Python GitHub Wiki

  These are some problems that I am encountering as I try to code the game. Some are quite basic, as I am relatively new to Pygame, but I would be grateful for any kind of input! I appreciate any kind of help! Most of these problems I've done extensive research, but still can't find the answer too.

Current Problems


  Problems/questions I encounter that I couldn't find answers to easily online or in books will be posted here to seek help in addition to issues tab.

Past Issues (Solved)

  Solved problems are put here so I can see how far I've come (and also for future reference)! Hopefully this will also help anyone else with a similar problem.

2/18/19 Pygame's colliderect() not working?
  I've scoured the Stack Overflow for similar threads, but can't find one pertaining to my problem exactly. Though I've assigned images with .getrect(), when I try to call colliderect(), the function does not work. Testing by placing a print() statement into the conditional statement, it seems that two rectangles (as pertaining to their respective images) are perpetually confirmed to be intersecting, though they are not. By printing the rect.left, right, top, bottom, moreover, it returns the values of 0, width, 0, and height instead of the coordinates of the rectangle, as if the coordinate plane itself starts and ends on the image, instead of on the coordinate plane of the screen.
  Why is this? How can I fix this so that colliderect() works and how do I obtain the coordinate points of the top, left, bottom, and right of the rectangles?
  My end goal here to to make an object (image) into an impassible obstacle, so the player does not travel over the image eg. of a rock.
The sample code (not all of it):

 
def draw_object(path, file, scale, x, y):
      global player_y, player_x
      obj = pygame.image.load (os.path.join (path, file))
      obj_rect = obj.get_rect()
      obj = pygame.transform.scale (obj, (obj_rect.width*scale , obj_rect.height*scale))
      DISPLAY.blit(obj, (x,y))
      player_rect = player.get_rect()
      if player_rect.colliderect(obj_rect):
          do stuff here 

2/22/19 Solved :
  For now, I don't quite get colliderect(), but since colliderect() is pretty much a predefined function, I decided to make my own that will detect if one rectangle (the player) is inside of another rectangle (the object). Later on I will study the sprite class and try to optimize the code so it uses less lines, but for now I'm quite proud of this. It took me quite a while to get this because I assumed the coordinates of an image would refer to the center of the image. Turns out, it refers to the top left corner of the image as image coordinates. Moreover, it took me a while to realize that the scaling of an image is what threw off the boundaries on the bottom and right of the object (seems that scaling works relative to a constant top left coordinate).
 
def PointinBox(player_x, player_y, player_rect, obj_x, obj_y, obj_rect, scale):
    if (player_x < obj_x + (obj_rect.width * scale )) \
    and (player_x + (player_rect.width) > obj_x ) \
    and (player_y < obj_y + (obj_rect.height * scale )) \
    and (player_y + (player_rect.height) > obj_y ) \
    :
        return True
    else:
        return False

4/12/19 Solved : The past me was a dummy: colliderect() is for pygame.sprite.Sprite classes; you couldn't have made this work without first creating a Sprite class for your player.
⚠️ **GitHub.com Fallback** ⚠️