Python Forum

Full Version: Collision in not happening
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a collision not working .The walls are draw, but not the collision.

import pygame
from Settings import *
from game import *
class Player(object):
    def __init__(self,x,y,k,path):
        self.x = x
        self.y = y
        self.image = pygame.image.load(path)
        self.K =k
        self.myfont = pygame.font.SysFont("cosmicsansms", 30)
        self.label = self.myfont.render("Gracz!", 32, (255, 0, 255))
        self.rect = pygame.Rect(self.x,self.y,self.K,self.K)

    def draw(self,screen):
        screen.blit(self.image, (self.x * self.K, self.y * self.K))
        screen.blit(self.label, (self.x * self.K, self.y * self.K))
        self.rect = pygame.Rect(self.x * self.K, self.y * self.K, self.K, self.K)
        print(self.rect)
    def move_left(self):
        self.x =self.x -1
        self.move(self.x-1,self.y)
    def move_right(self):
        self.x = self.x +1
    def move_up(self):
        self.y =self.y -1
    def move_down(self):
        self.y = self.y+1

    def move(self, dx, dy):

        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0:  # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                if dx < 0:  # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0:  # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0:  # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom


class Wall(object):
    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], KRATKA, KRATKA)
print out self.rect and wall rect at the time of the collision and see what they are really instead of assumed to be. it would be easier to help with a runnable example
I don't know where the bug is. Now I have a collision. But being in the wall.
import pygame
from Settings import *
from game import *
class Player(object):
    def __init__(self,x,y,k,path):
        self.x = x
        self.y = y
        self.image = pygame.image.load(path)
        self.K =k
        self.myfont = pygame.font.SysFont("cosmicsansms", 30)

        self.rect = pygame.Rect(self.x,self.y,16,16)
        self.move(self.x,self.y)

    def draw(self,screen):
        self.label = self.myfont.render("Gracz!" + str(self.x), 32, (255, 0, 255))
        screen.blit(self.image, (self.x * self.K, self.y * self.K))
        screen.blit(self.label, (self.x * self.K, self.y * self.K))
        self.rect = pygame.Rect(self.x * self.K, self.y * self.K, 16, 16)
        print(self.rect)

    def move_left(self):

        #self.x = self.x - 1
        #self.move(self.x-1, self.y)
        self.move_single_axis(self.x-1,self.y)
    def move_right(self):
        self.x = self.x +1
    def move_up(self):
        self.y =self.y -1
    def move_down(self):
        self.y = self.y+1

    def move(self, dx, dy):
        print(dx)
        # Move each axis separately. Note that this checks for collisions both times.
        if dx != 0:
            self.move_single_axis(dx, 0)
        if dy != 0:
            self.move_single_axis(0, dy)

    def move_single_axis(self, dx, dy):

        # Move the rect
        self.rect.x += dx
        self.rect.y += dy

        # If you collide with a wall, move out based on velocity
        for wall in walls:
            if self.rect.colliderect(wall.rect):
                if dx > 0:  # Moving right; Hit the left side of the wall
                    self.rect.right = wall.rect.left
                    print("left")
                    print(wall.rect.left+wall.rect.right)
                    self.x = dx
                if dx < 0:  # Moving left; Hit the right side of the wall
                    self.rect.left = wall.rect.right
                if dy > 0:  # Moving down; Hit the top side of the wall
                    self.rect.bottom = wall.rect.top
                if dy < 0:  # Moving up; Hit the bottom side of the wall
                    self.rect.top = wall.rect.bottom

class Wall(object):
    def __init__(self, pos):
        walls.append(self)
        self.rect = pygame.Rect(pos[0], pos[1], KRATKA, KRATKA)
(Sep-04-2020, 11:34 PM)metulburr Wrote: [ -> ]it would be easier to help with a runnable example