Mam problem nie działa mi kolizja. Ściany udało mi się dodać i je narysować ale postać może cały czas przez nie przechodzić.Próbuje na razie w jednym kierunku. Widać gdzieś tu błąd?
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):
#
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):
#
self.rect.x += dx
self.rect.y += dy
#
for wall in walls:
if self.rect.colliderect(wall.rect):
if dx > 0: #
self.rect.right = wall.rect.left
if dx < 0: #
self.rect.left = wall.rect.right
if dy > 0: #l
self.rect.bottom = wall.rect.top
if dy < 0: #
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)