Загрузить файлы в «25.02.2024.»
parent
4bb3aa189c
commit
fbd4110b97
Binary file not shown.
After Width: | Height: | Size: 523 B |
Binary file not shown.
After Width: | Height: | Size: 513 B |
|
@ -0,0 +1,147 @@
|
|||
import pygame
|
||||
import sys
|
||||
|
||||
pygame.init()
|
||||
|
||||
width, height = 800, 670
|
||||
screen = pygame.display.set_mode((width, height))
|
||||
pygame.display.set_caption('Labirints')
|
||||
|
||||
BLACK = (0, 0, 0)
|
||||
BLUE = (0, 0, 255)
|
||||
|
||||
maze = [
|
||||
"#########################",
|
||||
"#......#..........#.....#",
|
||||
"######.########.#.#.#####",
|
||||
"#...##.#........#.#.....#",
|
||||
"#.#.##...########.#####.#",
|
||||
"#.#.#########.....#...#.#",
|
||||
"#.#.......#.#.#####.#.#.#",
|
||||
"#.##..###.#.#.#.....#...#",
|
||||
"#.....###.#.#.#.#######.#",
|
||||
"#.##....#...#...#.#.....#",
|
||||
"#.##.##.###.#####.#.##.##",
|
||||
"#.....#...#...#...#.#...#",
|
||||
"#.#.#.###.###.#.###.#.#.#",
|
||||
"#.#.#...........#...#.#.#",
|
||||
"#.#.##.###.####.#.###.#.#",
|
||||
"#.#.#...##.##...#.#...#.#",
|
||||
"#.#.#.#.##....###.#.###.#",
|
||||
"#.#.#.#....##.#...#.#...#",
|
||||
"#.#.#.#.##..###.###.#.###",
|
||||
"#.......................#",
|
||||
"#########################"
|
||||
]
|
||||
|
||||
class Player(pygame.sprite.Sprite):
|
||||
def __init__(self, x, y):
|
||||
super().__init__()
|
||||
self.images = {
|
||||
"up": [pygame.image.load('up.png').convert_alpha(),
|
||||
pygame.image.load('up1.png').convert_alpha()],
|
||||
"down": [pygame.image.load('down.png').convert_alpha(),
|
||||
pygame.image.load('down1.png').convert_alpha()],
|
||||
"left": [pygame.image.load('left.png').convert_alpha(),
|
||||
pygame.image.load('left1.png').convert_alpha()],
|
||||
"right": [pygame.image.load('right.png').convert_alpha(),
|
||||
pygame.image.load('right1.png').convert_alpha()]
|
||||
}
|
||||
self.direction = "down" # Initial direction
|
||||
self.image_index = 0 # Index of the current image in the animation
|
||||
self.image = self.images[self.direction][self.image_index]
|
||||
self.rect = self.image.get_rect()
|
||||
self.rect.topleft = (x, y)
|
||||
self.speed = 10
|
||||
|
||||
def move(self, dx, dy, walls):
|
||||
old_rect = self.rect.copy()
|
||||
self.rect.x += dx
|
||||
self.rect.y += dy
|
||||
for wall in walls:
|
||||
if self.rect.colliderect(wall):
|
||||
if dx > 0:
|
||||
self.rect.right = wall.left
|
||||
elif dx < 0:
|
||||
self.rect.left = wall.right
|
||||
elif dy > 0:
|
||||
self.rect.bottom = wall.top
|
||||
elif dy < 0:
|
||||
self.rect.top = wall.bottom
|
||||
if self.rect.collidelist(walls) != -1:
|
||||
self.rect = old_rect
|
||||
|
||||
def update_image(self, dx, dy):
|
||||
if dx > 0:
|
||||
self.direction = "right"
|
||||
elif dx < 0:
|
||||
self.direction = "left"
|
||||
elif dy > 0:
|
||||
self.direction = "down"
|
||||
elif dy < 0:
|
||||
self.direction = "up"
|
||||
|
||||
# Update the image index for animation
|
||||
self.image_index = (self.image_index + 1) % len(self.images[self.direction])
|
||||
self.image = self.images[self.direction][self.image_index]
|
||||
|
||||
def draw(self, surface):
|
||||
surface.blit(self.image, self.rect)
|
||||
|
||||
player = Player(32, 32)
|
||||
|
||||
def draw_maze(maze):
|
||||
walls = []
|
||||
for y, row in enumerate(maze):
|
||||
for x, char in enumerate(row):
|
||||
if char == '#':
|
||||
pygame.draw.rect(screen, BLACK, (x * 32, y * 32, 32, 32))
|
||||
walls.append(pygame.Rect(x * 32, y * 32, 32, 32))
|
||||
return walls
|
||||
|
||||
clock = pygame.time.Clock()
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
keys = pygame.key.get_pressed()
|
||||
dx, dy = 0, 0
|
||||
|
||||
# Check horizontal movement
|
||||
if keys[pygame.K_LEFT]:
|
||||
dx = -player.speed
|
||||
elif keys[pygame.K_RIGHT]:
|
||||
dx = player.speed
|
||||
|
||||
# Check vertical movement
|
||||
if keys[pygame.K_UP]:
|
||||
dy = -player.speed
|
||||
elif keys[pygame.K_DOWN]:
|
||||
dy = player.speed
|
||||
|
||||
# Ensure that the player can only move in one direction at a time
|
||||
if dx != 0 and dy != 0:
|
||||
# Diagonal movement detected, prioritize one direction
|
||||
if keys[pygame.K_LEFT]:
|
||||
dy = 0 # Prevent vertical movement
|
||||
elif keys[pygame.K_RIGHT]:
|
||||
dy = 0 # Prevent vertical movement
|
||||
elif keys[pygame.K_UP]:
|
||||
dx = 0 # Prevent horizontal movement
|
||||
elif keys[pygame.K_DOWN]:
|
||||
dx = 0 # Prevent horizontal movement
|
||||
|
||||
walls = draw_maze(maze)
|
||||
player.move(dx, dy, walls)
|
||||
player.update_image(dx, dy)
|
||||
|
||||
screen.fill(BLUE)
|
||||
draw_maze(maze)
|
||||
player.draw(screen)
|
||||
pygame.display.flip()
|
||||
clock.tick(10) # Reduce animation speed for better visibility
|
||||
|
||||
pygame.quit()
|
||||
sys.exit()
|
Binary file not shown.
After Width: | Height: | Size: 533 B |
Binary file not shown.
After Width: | Height: | Size: 533 B |
Loading…
Reference in New Issue