1

Im making a game which if the saw collide the player then it dies, but when i made the saw bounce of the wall then the colliderect doesn't work.

Heres my code:

import pygame
from pygame import draw
from pygame import Rect
pygame.init()

width = 1200
height = 700
screen = pygame.display.set_mode([width, height])

boss = pygame.image.load("C:/Users/Thien Truong/Downloads/Ninja.png")
bossx = 425 
bossy = 10
def drawBoss():
    screen.blit(boss,(bossx,bossy))

player = pygame.image.load("C:/Users/Thien Truong/Desktop/Player.png").convert()
playerrect = player.get_rect()
playerx = 575
playery = 625
playerspeed = 5

def drawPlayer():
    screen.blit(player,(playerx,playery))

circleX = 100
circleY = 100
radius = 10

saw = pygame.image.load("C:/Users/Thien Truong/Downloads/Saw3.png")
sawrect = saw.get_rect()
speed = [7, 7]
 
running = True
clock = pygame.time.Clock()
angle = 1
Bar = 0
rect_x = 50
rect_y = 50
 
rect_change_x = 2
rect_change_y = 2
while running:
    Bar += 0.04
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    keys = pygame.key.get_pressed()
    if keys[pygame.K_UP]:
        if playery >= 0:
            playery -=  playerspeed
        else:
            playery -= 0
    if keys[pygame.K_DOWN]:
        if playery <= 650:  
            playery += playerspeed
        else:
            playery -= 0
    if keys[pygame.K_LEFT]:
        if playerx >= 0:
            playerx -=  playerspeed
    if keys[pygame.K_RIGHT]:
        if playerx <= 1145:
            playerx += playerspeed
        else:
            playery -= 0

    screen.fill([0,0,0])
    drawBoss()
    rect_x += rect_change_x
    rect_y += rect_change_y
 

    if rect_y > 650 or rect_y < 0:
        rect_change_y = rect_change_y * -1
    if rect_x > 1150 or rect_x < 0:
        rect_change_x = rect_change_x * -1
    balls = pygame.transform.rotate(saw,angle)
    angle += 1
    screen.blit(saw, (rect_x,rect_y))
    if sawrect.colliderect(playerrect):
        print("d")
    drawPlayer()
    pygame.draw.rect(screen, (0, 100, 255), (1175, 150, 20, 475), 3)
    pygame.draw.rect(screen,(0,255,0),(1175, 150, 20, Bar))
    if Bar > 475:
        pygame.quit()
    pygame.display.flip()
pygame.quit()

The part where the saw bounces:

    rect_x += rect_change_x
    rect_y += rect_change_y

    if rect_y > 650 or rect_y < 0:
        rect_change_y = rect_change_y * -1
    if rect_x > 1150 or rect_x < 0:
        rect_change_x = rect_change_x * -1
    balls = pygame.transform.rotate(saw,angle)
    angle += 1

Collison:

    if playerrect.colliderect(sawrect):
        print("d")

I tried to run but it seems like it keep printing even if the saw isnt even close to the player.

Please help!

Rabbid76
  • 202,892
  • 27
  • 131
  • 174

1 Answers1

0

pygame.Surface.get_rect.get_rect() returns a rectangle with the size of the Surface object, that always starts at (0, 0) since a Surface object has no position. A Surface is blit at a position on the screen. The position of the rectangle can be specified by a keyword argument. For example, the top left of the rectangle can be specified with the keyword argument topleft. These keyword argument are applied to the attributes of the pygame.Rect before it is returned (see pygame.Rect for a full list of the keyword arguments).

Set the location of the rectangle, before the collision test:

playerrect.topleft = (playerx, playery)
sawrect.topleft = (rect_x, rect_y)

if playerrect.colliderect(sawrect):
    print("d")
Rabbid76
  • 202,892
  • 27
  • 131
  • 174