Okonar

Лабіринт

Oct 14th, 2023
7
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import os
  2. import sys
  3. import random
  4. import pygame
  5.  
  6.  
  7. # Class for the orange dude
  8. class Player(object):
  9.  
  10. def __init__(self):
  11. self.rect = pygame.Rect(32, 32, 16, 16)
  12.  
  13. def move(self, dx, dy):
  14.  
  15. # Move each axis separately. Note that this checks for collisions both times.
  16. if dx != 0:
  17. self.move_single_axis(dx, 0)
  18. if dy != 0:
  19. self.move_single_axis(0, dy)
  20.  
  21. def move_single_axis(self, dx, dy):
  22.  
  23. # Move the rect
  24. self.rect.x += dx
  25. self.rect.y += dy
  26.  
  27. # If you collide with a wall, move out based on velocity
  28. for wall in walls:
  29. if self.rect.colliderect(wall.rect):
  30. if dx > 0: # Moving right; Hit the left side of the wall
  31. self.rect.right = wall.rect.left
  32. if dx < 0: # Moving left; Hit the right side of the wall
  33. self.rect.left = wall.rect.right
  34. if dy > 0: # Moving down; Hit the top side of the wall
  35. self.rect.bottom = wall.rect.top
  36. if dy < 0: # Moving up; Hit the bottom side of the wall
  37. self.rect.top = wall.rect.bottom
  38.  
  39.  
  40. # Nice class to hold a wall rect
  41. class Wall(object):
  42.  
  43. def __init__(self, pos):
  44. walls.append(self)
  45. self.rect = pygame.Rect(pos[0], pos[1], 16, 16)
  46.  
  47.  
  48. # Initialise pygame
  49. os.environ["SDL_VIDEO_CENTERED"] = "1"
  50. pygame.init()
  51.  
  52. # Set up the display
  53. pygame.display.set_caption("Get to the red square!")
  54. screen = pygame.display.set_mode((320, 240))
  55.  
  56. clock = pygame.time.Clock()
  57. walls = [] # List to hold the walls
  58. player = Player() # Create the player
  59.  
  60. # Holds the level layout in a list of strings.
  61. level = [
  62. "WWWWWWWWWWWWWWWWWWWWWWWWWW",
  63. "W W",
  64. "W WWWWWW W",
  65. "W WWWW W W",
  66. "W W WWWW W",
  67. "W WWW WWWW W",
  68. "W W W W W",
  69. "W W W WWW W W",
  70. "W WWW WWW W W W",
  71. "W W W W W W W",
  72. "WWW W WWWWW W W",
  73. "W W WW W",
  74. "W W WWWW WWW W",
  75. "W W E W W W",
  76. "WWWWWWWWWWWWWWWWWWWWWWWWWW",
  77. ]
  78.  
  79. # Parse the level string above. W = wall, E = exit
  80. x = y = 0
  81. for row in level:
  82. for col in row:
  83. if col == "W":
  84. Wall((x, y))
  85. if col == "E":
  86. end_rect = pygame.Rect(x, y, 16, 16)
  87. x += 16
  88. y += 16
  89. x = 0
  90.  
  91. running = True
  92. while running:
  93.  
  94. clock.tick(60)
  95.  
  96. for e in pygame.event.get():
  97. if e.type == pygame.QUIT:
  98. running = False
  99. if e.type == pygame.KEYDOWN and e.key == pygame.K_ESCAPE:
  100. running = False
  101.  
  102. # Move the player if an arrow key is pressed
  103. key = pygame.key.get_pressed()
  104. if key[pygame.K_LEFT]:
  105. player.move(-2, 0)
  106. if key[pygame.K_RIGHT]:
  107. player.move(2, 0)
  108. if key[pygame.K_UP]:
  109. player.move(0, -2)
  110. if key[pygame.K_DOWN]:
  111. player.move(0, 2)
  112.  
  113. # Just added this to make it slightly fun ;)
  114. if player.rect.colliderect(end_rect):
  115. pygame.quit()
  116. sys.exit()
  117.  
  118. # Draw the scene
  119. screen.fill((0, 0, 0))
  120. for wall in walls:
  121. pygame.draw.rect(screen, (255, 255, 255), wall.rect)
  122. pygame.draw.rect(screen, (255, 0, 0), end_rect)
  123. pygame.draw.rect(screen, (255, 200, 0), player.rect)
  124. # gfxdraw.filled_circle(screen, 255, 200, 5, (0,128,0))
  125. pygame.display.flip()
  126. clock.tick(360)
Advertisement
Add Comment
Please, Sign In to add comment