Rohit Jose
Rohit Jose

Reputation: 209

Pygame's colliderect isn't noticing collisions

Before this I used the code to say 'every button press it moves' but now I have changed it to 'when you press the button it moves in one direction until further instructions', but now the colliderect doesn't work. It worked before, and I'm still new to pygame. I have found a few similar issues but I think I've done what the people asking the questions have done. Any help at all is accepted.

import pygame, sys, random
from pygame.locals import *
from time import sleep
pygame.init()

def render():
    windowSurface.fill(black)
    player = pygame.Rect(225 + xmod,450 - ymod,30,30)
    pygame.draw.rect(windowSurface,white,player)
    pygame.draw.rect(windowSurface,white,wall_1)
    pygame.draw.rect(windowSurface,white,wall_2)
    pygame.display.update()

black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
windowSurface.fill(black)
xmod = 0
ymod = 0
direction = 'none'
player = pygame.Rect(225 + xmod,450 - ymod,30,30)
wall_1 = pygame.Rect(0,225,250,50)
wall_2 = pygame.Rect(300,250,200,50)
render()

while True:
    render()
    for event in pygame.event.get():
        if event.type == KEYDOWN:
            if event.key == K_LEFT:
                direction = 'left'
            if event.key == K_RIGHT:
                direction = 'right'
            if event.key == K_UP:
                direction = 'up'
            if event.key == K_DOWN:
                direction = 'down'
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    if player.colliderect(wall_1) or player.colliderect(wall_2):
        xmod = 0
        ymod = 0
        player = pygame.Rect(225 + xmod,450 - ymod,30,30)
        render()
        print('again')
    if direction == 'left':
        xmod -= 1
        sleep(0.004)
    if direction == 'right':
        xmod += 1
        sleep(0.004)
    if direction == 'up':
        ymod += 1
        sleep(0.004)
    if direction == 'down':
        ymod -= 1
        sleep(0.004)

Upvotes: 2

Views: 90

Answers (1)

skrx
skrx

Reputation: 20438

You never update the global player variable, so it stays at its original coordinates. In the render function you create a new rect and assign it to a local player variable, but it is not the same as the global player which you use for the collision detection.

I suggest adding variables for the velocity speed_x, speed_y and adding them to the player.x and .y attributes every frame to move the rect directly.

The local player rect in the render function can be removed.

import sys
import pygame
from pygame.locals import *


pygame.init()

def render():
    windowSurface.fill(black)
    pygame.draw.rect(windowSurface, white, wall_1)
    pygame.draw.rect(windowSurface, white, wall_2)
    pygame.draw.rect(windowSurface, white, player)
    pygame.display.update()


black = (0,0,0)
white = (255,255,255)
windowSurface = pygame.display.set_mode((500, 500),0,32)
clock = pygame.time.Clock()  # A clock to limit the frame rate.

player = pygame.Rect(225, 450, 30, 30)
wall_1 = pygame.Rect(0,225,250,50)
wall_2 = pygame.Rect(300,250,200,50)

speed_x = 0
speed_y = 0

while True:
    # Handle events.
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        elif event.type == KEYDOWN:
            if event.key == K_LEFT:
                speed_x = -5
                speed_y = 0
            elif event.key == K_RIGHT:
                speed_x = 5
                speed_y = 0
            elif event.key == K_UP:
                speed_y = -5
                speed_x = 0
            elif event.key == K_DOWN:
                speed_y = 5
                speed_x = 0

    # Add the speed to the x and y attributes to move the rect.
    player.x += speed_x
    player.y += speed_y

    # Game logic.
    if player.colliderect(wall_1) or player.colliderect(wall_2):
        player = pygame.Rect(225, 450, 30, 30)
        print('again')

    # Render everything.
    render()

    clock.tick(60)  # Limit the frame rate to 60 FPS.

Upvotes: 1

Related Questions