2

For a computer science project, I'm making a game in pygame.

I'm working on collision right now with the Mario sprite and that block. However pygame collision works based on two rectangles colliding from what I know. You can easily put a rectangle around the block, but the Mario sprite is a different matter entirely. It becomes even more complicated when you realize you switch images for going left or right or jumping or whatnot.

I've tried looking this up on Google, where none of the answers worked. My CSP teacher also said he's not familiar with pygame and sadly can't help.

import pygame
pygame.init()

#set the background
screen = pygame.display.set_mode((1600, 800))
clock = pygame.time.Clock()
FPS = 45
vel = 3.5
BLACK = (0,0,0)
back = pygame.image.load('background.png')
background = pygame.transform.scale(back, (800,600))
playerimg = pygame.image.load('mariosprite.png').convert_alpha()
playerimgflipped = pygame.image.load('normalmarioflipped.png')
playerimgjump = pygame.image.load('finishedjumpingmario.png')
blockonelol = pygame.image.load('oneblock.png')
blockoneX = 100
blockoneY = 415
blockone = pygame.transform.scale(blockonelol,(40,40))
mario = pygame.transform.scale(playerimg, (35,50))
playerX = 10
playerY = 490
isJump = False
jumpCount = 11
run = True
while run:
  
  clock.tick(FPS)
  screen.fill(BLACK)
  screen.blit(background, (0,0))
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
  keys = pygame.key.get_pressed()
  if keys[pygame.K_LEFT] and vel < playerX:
    playerX = playerX - vel
    mario = pygame.transform.scale(playerimgflipped, (35,50))

  if keys[pygame.K_RIGHT]:
    playerX+=vel
    mario = pygame.transform.scale(playerimg, (35,50))

  if not(isJump):
    if keys[pygame.K_UP]:
      isJump = True
      if(playerY == 490):
        mario = pygame.transform.scale(playerimg, (35, 50))
  else:
    mario = pygame.transform.scale(playerimgjump, (45,55))
    if jumpCount >= -11:
      goingdown = 1
      if jumpCount < 0:
        goingdown = -1
      playerY -= (jumpCount ** 2) * 0.25 * goingdown
      jumpCount = jumpCount - 1
      if(playerY == 490):
        mario = pygame.transform.scale(playerimg, (35, 50))
    else:
      isJump = False
      jumpCount = 11
      if(playerY == 490):
        mario = pygame.transform.scale(playerimg, (35, 50))
  screen.blit(blockone, (blockoneX, blockoneY))
  screen.blit(mario, (playerX, playerY))
  pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Ryan Lolz
  • 21
  • 1

1 Answers1

3

However pygame collision works based on two rectangles colliding

Yes, but you don't need to add rectangles around the sprites.

Use pygame.Rect objects and colliderect() to detect the collision between the bounding rectangles of 2 objects or 2 images:

rect1 = pygame.Rect(x1, y1, w1, h1)
rect2 = pygame.Rect(x2, y2, w2, h2)
if rect1.colliderect(rect2):
    # [...]

If you have to images (pygame.Surface objects), the bounding rectangle of can be get by get_rect(), where the location of the Surface has to be set by an keyword argument, since the returned rectangle always starts at (0, 0):

mario_rect = mario.get_rect(topleft = (playerX, playerY))
blockone_rect = blockone.get_rect(topleft = (blockoneX, blockoneY))
if mario_rect.colliderect(blockone_rect):
    print("hit")

See also:

Rabbid76
  • 202,892
  • 27
  • 131
  • 174