From fbd4110b97e2dc7cb73a7b614dddfe30b1c5a6ed Mon Sep 17 00:00:00 2001 From: Aleksandrs Karpovs Date: Fri, 26 Apr 2024 05:53:21 +0000 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?25.02.2024.=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 25.02.2024./down.png | Bin 0 -> 523 bytes 25.02.2024./down1.png | Bin 0 -> 513 bytes 25.02.2024./lab (1).py | 147 +++++++++++++++++++++++++++++++++++++++++ 25.02.2024./left.png | Bin 0 -> 533 bytes 25.02.2024./left1.png | Bin 0 -> 533 bytes 5 files changed, 147 insertions(+) create mode 100644 25.02.2024./down.png create mode 100644 25.02.2024./down1.png create mode 100644 25.02.2024./lab (1).py create mode 100644 25.02.2024./left.png create mode 100644 25.02.2024./left1.png diff --git a/25.02.2024./down.png b/25.02.2024./down.png new file mode 100644 index 0000000000000000000000000000000000000000..3f257411e8b117c47e726eb7bdc4ce0591021a55 GIT binary patch literal 523 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnF3?v&v(vO2U$sR$z3=CCj3=9n|3=F@3LJcn% z7)lKo7+xhXFj&oCU=S~uvn$XBD4`YL6XNP#;3F$6w=2hIVu-b&p`BMicw=|>`T95& z6rG*RoG^Apjot*9vS`O*W0MlLRa?Cm2dup-B_-9H`T95& z6pU|?lnAnTbKjiMnpKP5A*61RpNk&DlP8Z_WGlw{_n7MCRE a7GTk10 literal 0 HcmV?d00001 diff --git a/25.02.2024./lab (1).py b/25.02.2024./lab (1).py new file mode 100644 index 0000000..b08ec05 --- /dev/null +++ b/25.02.2024./lab (1).py @@ -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() diff --git a/25.02.2024./left.png b/25.02.2024./left.png new file mode 100644 index 0000000000000000000000000000000000000000..9db982986f2b3dedcaa2b212fa97ed57e5428d2f GIT binary patch literal 533 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnF3?v&v(vO2U$sR$z3=CCj3=9n|3=F@3LJcn% z7)lKo7+xhXFj&oCU=S~uvn$XBD4`qR6XNP#;3F$6w=2hIVu-a@K)9iyU1N85t%q@h zv5Ky)?&BlfDk{43R;@Z;ANT+N|5Jy*n*cR3mIV0)GdMiEkp|)1<2x~7$BUqn+6&BU1AvYcd>#KtXhz1f%D0iypO;S(w6JE7XHzPc za5G?9$uMa>6PMpj&be|8F0UuPa6TuUt38j;zo2@;bTdai&&8+2HauUgvW%-G+)VX} z*z$MxJk}ZiJG0}OOyIpgKIVI-uMthXI5)!N>;Ka+zIS6v|CC);zpy|5EPvfAIiBq6 z-#`JYTH+c}l9E`GYL#4+3Zxi}42(>44UBb-EJ6${tW1opObxUR46FyP^%$!{F)a=d#Wzp$Pz+2CX6h literal 0 HcmV?d00001 diff --git a/25.02.2024./left1.png b/25.02.2024./left1.png new file mode 100644 index 0000000000000000000000000000000000000000..606e056de6adf1ac14760461bb20cf1f4d40f3ec GIT binary patch literal 533 zcmeAS@N?(olHy`uVBq!ia0vp^3LwnF3?v&v(vO2U$sR$z3=CCj3=9n|3=F@3LJcn% z7)lKo7+xhXFj&oCU=S~uvn$XBD4`qR6XNP#;3F$6w=2hIVu-a@K)9iyU1N85t%q@h zv5Ky)?&BlfDk{43R;@Z;ANT+N|5Jy*n*cR3mIV0)GdMiEkp|);t=^sMgg zZibN7?re^Z*N+}?h$WPKENN^I>1gOUEwrM+Tgjs#@aWN_qFN6$QaD5m$_hE}G6`~o zuvz-J8H5B_iSU>?)l665C{dG8_``OITlt{hZtk)Px`#Fk9QBBrz`(FITkg&&*9Z%s zQ&me`BT7;dOH!?pi&B9UgOP!eiLQaMu8~EEfrXWcv6ZQTwt<0_fq|@NW;BY1-29Zx wv`X9>azrjZ2WrrO+fb63n_66wm|K8Fj|s$*^L|&ffqED`UHx3vIVCg!0B|>+4gdfE literal 0 HcmV?d00001