Мне нужна помощь с python и pygame он долго считает столкновение с стенами
Когда персонаж проходит между узких стен, то он застревает углом
Вот код:
main.py
from fonts import *
from map import *
pygame.init()
press_e = False
screen = pygame.display.set_mode((1000, 800))
clock = pygame.time.Clock()
x, y = 100, 510
npc_talk = 0
main = True
while main:
clock.tick(60)
keys = pygame.key.get_pressed()
screen.fill((0, 0, 0))
for row in range(len(map_data)):
for col in range(len(map_data[row])):
if map_data[row][col] == 1:
pygame.draw.rect(screen, (255, 0, 0), pygame.Rect(col * tile_size, row * th, tile_size, th))
npc1_r = pygame.Rect((175, 175, 100, 100))
npc1 = pygame.Rect((200, 200, 50, 50))
pl = pygame.Rect((x - 10, y, 10, 50))
pr = pygame.Rect((x + 50, y, 10, 50))
pu = pygame.Rect((x, y - 10, 50, 10))
pd = pygame.Rect((x, y + 50, 50, 10))
#
# # pygame.draw.rect(screen, (255, 0, 0), pu)
# # pygame.draw.rect(screen, (255, 0, 0), pd)
# # pygame.draw.rect(screen, (255, 0, 0), pl)
# # pygame.draw.rect(screen, (255, 0, 0), pr)
# # pygame.draw.rect(screen, (0, 255, 255), npc1_r)
#
player_rect = pygame.Rect((x, y, 50, 50))
pygame.draw.rect(screen, (0, 0, 255), npc1)
pygame.draw.rect(screen, (0, 255, 0), player_rect)
alls = pr.colliderect(npc1_r) or pu.colliderect(npc1_r) or pl.colliderect(npc1_r) or pd.colliderect(npc1_r)
if npc_talk == 0:
if not pu.collidelistall([pygame.Rect(col * tile_size, row * th, tile_size, th)
for row in range(len(map_data))
for col in range(len(map_data[row])) if map_data[row][col] == 1]):
if keys[pygame.K_w]:
if y >= 10 and not pu.colliderect(npc1): # and pwcu
y2 = y + 10
y = y - 10
if not pd.collidelistall([pygame.Rect(col * tile_size, row * th, tile_size, th)
for row in range(len(map_data))
for col in range(len(map_data[row])) if map_data[row][col] == 1]):
if keys[pygame.K_s]:
if y <= 690 and not pd.colliderect(npc1): # and pwcd
y2 = y - 10
y = y + 10
if not pl.collidelistall([pygame.Rect(col * tile_size, row * th, tile_size, th)
for row in range(len(map_data))
for col in range(len(map_data[row])) if map_data[row][col] == 1]):
if keys[pygame.K_a]:
if x >= 10 and not pl.colliderect(npc1): # and pwcl
x2 = x + 10
x = x - 10
if not pr.collidelistall([pygame.Rect(col * tile_size, row * th, tile_size, th)
for row in range(len(map_data))
for col in range(len(map_data[row])) if map_data[row][col] == 1]):
if keys[pygame.K_d]:
if x <= 890 and not pr.colliderect(npc1): # and pwcr
x2 = x - 10
x = x + 10
if npc_talk == 1:
if alls:
pygame.draw.rect(screen, (64, 61, 57), (0, 400, 1000, 400))
pygame.draw.rect(screen, (0, 0, 255), (50, 420, 100, 100))
screen.blit(npc_name1, (200, 420))
screen.blit(npc_text, (200, 470))
screen.blit(your_name, (200, 530))
if not player_rect.colliderect(npc1_r):
npc_talk = 0
if player_rect.colliderect(npc1_r) and npc_talk == 0:
screen.blit(talk, (x - 140, y + 20))
for event in pygame.event.get():
if event.type == pygame.QUIT:
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_e:
npc_talk = not npc_talk
if event.key == pygame.K_e:
print(x, y)
pygame.display.update()
map.py
map_data = [
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
]
tile_size = 50
th = 50
fonts.py
import pygame
pygame.init()
font1 = pygame.font.Font('fonts/font1.otf', 40)
font2 = pygame.font.Font('fonts/font1.otf', 20)
# font1 = pygame.font.SysFont('arial', 40)
# font2 = pygame.font.SysFont('arial', 20)
npc_name1 = font1.render("Джоунси:", True, (255, 255,255))
your_name= font1.render("Ты:", True, (255,255,255))
talk = font2.render('для разговора нажмите E', True, (255, 255, 255))
npc_text = font2.render('Привет, как дела?', True, (255, 255, 255))