Здравствуйте, я делаю roguelike-игру в pygame, и я все еще на первых шагах игры, но у меня возникла проблема. Я не знаю почему, но существо, которое я создал, в данном случае «скелет», продолжает проходить через каждую стену. У него случайное горизонтальное движение, но я добавил тот же код, который создал для своего главного героя, но пока мой персонаж не может проходить сквозь стены, скелет просто проходит через них.
import pygame
image = pygame.image.load('model/standing1.png')
Standing = pygame.transform.scale(image, (32, 32))
image = pygame.image.load('map/floor.jpg')
Floor = pygame.transform.scale(image, (32, 32))
image = pygame.image.load('map/rightwall.png')
wallRight = pygame.transform.scale(image, (32, 32))
image = pygame.image.load('enemy/enemyr1.png')
enemyStanding = pygame.transform.scale(image, (32, 32))
walkLeft = []
walkRight = []
walkUp = []
walkDown = []
##### character sprite ######
for i in range(1, 7):
image = pygame.image.load('model/r' + str(i) + '.png')
walkRight.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/l' + str(i) + '.png')
walkLeft.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/u' + str(i) + '.png')
walkUp.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('model/d' + str(i) + '.png')
walkDown.append(pygame.transform.scale(image, (32, 32)))
class struc_Tile():
def __init__(self, block_path):
self.block_path = block_path
###### MOBS ###########
class skeleton(object):
enemyLeft = []
enemyRight = []
for i in range (1,7):
image = pygame.image.load('Skeleton/walk_0' + str(i) + '.png')
enemyRight.append(pygame.transform.scale(image, (32, 32)))
image = pygame.image.load('Skeleton/walkl_0' + str(i) + '.png')
enemyLeft.append(pygame.transform.scale(image, (32, 32)))
def __init__(self,x,y,width,height,endx,endy):
self.x = x
self.y = y
self.width = width
self.height = height
self.endx = endx
self.endy = endy
self.pathx = [self.x, self.endx]
self.pathy = [self.y, self.endy]
self.vel = 3
self.walkCount = 0
def draw(self, screen):
self.move()
if self.walkCount + 1 >= 12:
self.walkCount = 0
if self.vel > 0:
screen.blit(self.enemyRight[self.walkCount//3], (self.x, self.y))
self.walkCount += 1
else:
screen.blit(self.enemyLeft[self.walkCount//3], (self.x, self.y))
self.walkCount += 1
def can_move(self, dx, dy):
new_x = self.x + dx
new_y = self.y + dy
if gamemap[new_x][new_y].block_path == False:
if gamemap[new_x + cellWidth][new_y].block_path == False:
if gamemap[new_x][new_y + cellHeight].block_path == False:
if gamemap[new_x + cellWidth][new_y + cellHeight].block_path == False:
self.x += dx
self.y += dy
return True
def move(self):
if self.vel > 0:
if self.x < self.pathx[1] + self.vel:
self.x += self.vel
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
else:
if self.x > self.pathx[0] - self.vel:
self.x += self.vel
else:
self.vel = self.vel * -1
self.x += self.vel
self.walkCount = 0
class creature(object):
def __init__(self, name, hp = 10):
self.name = name
self.hp = hp
#### Character ###############
class player(object):
def __init__(self,x,y,width,height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.walkCount = 0
self.images = {}
self.images['walkleft'] = walkLeft[:]
self.images['walkright'] = walkRight[:]
self.images['walkup'] = walkUp[:]
self.images['walkdown'] = walkDown[:]
def draw(self, screen, direction):
if self.walkCount + 1 >= 18:
self.walkCount = 0
if direction == 'standing':
screen.blit(Standing, (self.x,self.y))
self.walkCount = 0
else:
screen.blit(self.images[direction][self.walkCount//3], (self.x,self.y))
self.walkCount += 1
def can_move(self, dx, dy):
new_x = self.x + dx
new_y = self.y + dy
if gamemap[new_x][new_y].block_path == False:
if gamemap[new_x + cellWidth][new_y].block_path == False:
if gamemap[new_x][new_y + cellHeight].block_path == False:
if gamemap[new_x + cellWidth][new_y + cellHeight].block_path == False:
self.x += dx
self.y += dy
return True
####
def createmap():
newmap = [[struc_Tile(False) for y in range(0, mapHeight)] for x in range (0,mapWidth)]
for x in range(0, mapWidth):
for y in range (0, mapHeight):
if y < 32 or y + cellWidth >= mapHeight:
newmap[x][y].block_path = True
elif x < 32 or x + cellWidth >= mapWidth:
newmap[x][y].block_path = True
return newmap
##### Draw map ####
def drawmap(maptodraw):
for x in range(0, mapWidth, cellWidth):
for y in range(0, mapHeight, cellHeight):
if maptodraw[x][y].block_path == True:
screen.blit(wallRight, (x, y))
else:
screen.blit(Floor, (x, y))
###### Colision with walls ########
def block_element(x, y, maptoblock, block=True):
x_cells = int(mapWidth / cellWidth)
y_cells = int(mapHeight / cellHeight)
start_x = int(x * cellWidth)
start_y = int(y * cellHeight)
end_x = start_x + cellWidth
end_y = start_y + cellHeight
print(start_x, end_x)
if x >= 0 and x < x_cells:
if y >= 0 and y < y_cells:
for x in range(start_x, end_x):
for y in range(start_y, end_y):
maptoblock[x][y].block_path = block
return maptoblock
pygame.init()
mapHeight = 960
mapWidth = 960
cellWidth = 32
cellHeight = 32
screen = pygame.display.set_mode((mapWidth, mapHeight))
####### add or remove walls ##########
gamemap = createmap()
gamemap = block_element(8, 6,gamemap) #add walls
gamemap = block_element(5, 10,gamemap)
gamemap = block_element(0, 8, gamemap, block=False) ### remove walls
gamemap = block_element(0, 9, gamemap, block=False)
clock = pygame.time.Clock()
character = player(64, 64, 32, 32)
skeletonmob = skeleton(200,200,32,32,500,500)
run = True
while run:
clock.tick(18)
pygame.display.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
drawmap(gamemap)
skeletonmob.draw(screen)
keys = pygame.key.get_pressed() #### movement keys
if keys[pygame.K_LEFT] and character.can_move(0 - character.vel, 0):
character.draw(screen, 'walkleft') #move left
elif keys[pygame.K_RIGHT] and character.can_move(character.vel, 0):
character.draw(screen, 'walkright') #move right
elif keys[pygame.K_UP] and character.can_move(0, 0 - character.vel):
character.draw(screen, 'walkup') #move up
elif keys[pygame.K_DOWN] and character.can_move(0, character.vel):
character.draw(screen, 'walkdown') #move down
else:
character.draw(screen, 'standing')