Python Forum
[PyGame] Collision in not happening
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Collision in not happening
#1
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)
Reply
#2
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
Recommended Tutorials:
Reply
#3
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)
Reply
#4
(Sep-04-2020, 11:34 PM)metulburr Wrote: it would be easier to help with a runnable example
Recommended Tutorials:
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [PyGame] Problem with collision of player and enemy Nekotrooper 1 601 Dec-08-2023, 03:29 PM
Last Post: deanhystad
  can't get collision detection to work in platform game chairmanme0wme0w 10 3,663 Aug-19-2022, 03:51 PM
Last Post: deanhystad
  [PyGame] No collision detection onizuka 6 3,596 Aug-18-2020, 01:29 PM
Last Post: onizuka
  Problem with collision detection... michael1789 4 3,219 Nov-12-2019, 07:49 PM
Last Post: michael1789
  Arcade Collision Problem randor 0 2,656 Oct-28-2019, 11:17 PM
Last Post: randor
  Multiple wall collision in pacman rustyjoe 4 4,030 Aug-11-2019, 08:08 AM
Last Post: rustyjoe
  drawing, moving, and collision problems (pygame) SheeppOSU 26 14,441 Apr-22-2019, 03:09 AM
Last Post: SheeppOSU

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020