Sep-04-2020, 09:23 PM
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)