3

I have tried to make a Zelda clone, and now I'm wondering how to calculate collision, can anyone tell me how to do that? I have tried colliderct and it simply won't work here is my code:

import pygame
pygame.init()
display = pygame.display.set_mode((800,600))
white=(255,255,255)
black=(0,0,0)
x=50
y=50
width = 40
height = 60
vel = 5
playerimg= pygame.image.load('link1.jpg').convert()
def player(x,y):
    display.blit(playerimg,(x,y))
while True:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            break
        keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
        x -= vel
    if keys[pygame.K_d]:
        x += vel
    if keys[pygame.K_w]:
        y -= vel
    if keys[pygame.K_s]:
        y += vel
    display.fill(white)
    player(x,y)
    pygame.draw.rect(display, (255,0,0), hitbox,2)
    pygame.display.update() 
pygame.quit()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
Haoyang Song
  • 162
  • 12
  • You have to be more specific. What exactly do you want to do? The collision of which objects do you want to detect? Anyway, read about [`pygame.Rect`](https://www.pygame.org/docs/ref/rect.html) / [`colliderect()`](https://www.pygame.org/docs/ref/rect.html#pygame.Rect.colliderect). Or even better [`pygame.sprite.Sprite`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Sprite), [`pygame.sprite.Group`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.Group) and [`pygame.sprite.spritecollide()`](https://www.pygame.org/docs/ref/sprite.html#pygame.sprite.spritecollide). – Rabbid76 Jul 19 '20 at 07:19

2 Answers2

3

you can use 'hitboxes' to do this, one you must know the dimensions of your image now that you got them, you can do

hitbox=(x,y, 102,131) 
hitbox1=pygame.draw.rect(display, (255,0,0), hitbox,2)
if the_thing_it_hits.colliderect(hitbox) == True:
        print('ouch')

put this in the while True: loop and it should be good

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
1

You can do a collision test by using pygame.Rect and colliderect(). For instance you can define an obstacle and get the rectangle from playerimg by get_rect(). Test if the 2 rectangles are colliding:

while True:
    # [...]

    hitbox = pygame.Rect(100, 100, 100, 100) 
    player_rect = playerimg.get_rect(topleft = (x, y))

    if player_rect.colliderect(hitbox):
        print("hit")

    display.fill(white)
    player(x,y)
    pygame.draw.rect(display, (255,0,0), hitbox,2)
    pygame.display.update() 

Anyway, I recommend to use pygame.sprite.Sprite, pygame.sprite.Group and pygame.sprite.spritecollide().


Furthermore, your implementation of the QUIT event will not quit the game

while True:
   for event in pygame.event.get():
       if event.type == pygame.QUIT:
           break

because the break statement will just break the event loop, but not the application loop.
Use a variable instead:

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
Glenn Mackintosh
  • 2,765
  • 1
  • 10
  • 18
Rabbid76
  • 202,892
  • 27
  • 131
  • 174