Advertisement
MCreeper12731

Lesson 4 - Classes

Mar 24th, 2025
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.92 KB | None | 0 0
  1. import pygame
  2. pygame.init()
  3.  
  4. width = 1200
  5. height = 600
  6. window = pygame.display.set_mode((width, height))
  7. pygame.display.set_caption("Platformer Game")
  8.  
  9. # Spremenljivke, ki jih bomo potrebovali za igralca
  10. # ---
  11. player_image = pygame.image.load("images/scratch-cat.png") # SPREMENI NA SVOJO SLIKO
  12. player_width = 75
  13. player_height = 75
  14. player_image = pygame.transform.scale(player_image, (player_width, player_height))
  15. player = pygame.Rect(200, 200, player_width, player_height)
  16.  
  17. speed_x = 0
  18. speed_y = 0
  19.  
  20. platforms = [ pygame.Rect(300, 500, 200, 20), pygame.Rect(600, 350, 200, 20), pygame.Rect(900, 400, 200, 20)]
  21.  
  22. # ---
  23.  
  24. def resolve_collision(rect_a, rect_b):
  25.     # Compute overlap distances in x and y
  26.     dx1 =  - (rect_a.left - rect_b.right)
  27.     dx2 = rect_a.right - rect_b.left
  28.     dy1 = rect_b.bottom - rect_a.top
  29.     dy2 = rect_a.bottom - rect_b.top
  30.  
  31.     # Find the smallest displacement to separate them
  32.     min_dx = min(dx1, dx2)
  33.     min_dy = min(dy1, dy2)
  34.  
  35.     if min_dx < min_dy:
  36.         # Move rect_a left or right
  37.         if dx1 < dx2:
  38.             rect_a.x += dx1  # Move right
  39.         else:
  40.             rect_a.x -= dx2  # Move left
  41.     else:
  42.         # Move rect_a up or down
  43.         if dy1 < dy2:
  44.             rect_a.y += dy1  # Move down
  45.         else:
  46.             rect_a.y -= dy2  # Move up
  47.    
  48.     return True  # Collision was resolved
  49.  
  50. # Glavna zanka igre
  51. clock = pygame.time.Clock()
  52. running = True
  53. while running:
  54.     for event in pygame.event.get():
  55.         if event.type == pygame.QUIT:
  56.             running = False
  57.  
  58.     window.fill((255, 255, 255))
  59.     # background_image = pygame.image.load("images/New Piskel.png")
  60.     # background_image = pygame.transform.scale(background_image, (width, height))
  61.     # window.blit(background_image, (0, 0))
  62.     # Premikanje igralčevega lika
  63.     # ---
  64.  
  65.     # Vsako ponovitev zanke (vsak frame) dobimo pritisnjene tipke
  66.     # in se glede na njih odločimo, kako bomo igralca premaknili
  67.     keys = pygame.key.get_pressed()
  68.     if keys[pygame.K_d]:
  69.         speed_x = 5
  70.     elif keys[pygame.K_a]:
  71.         speed_x = -5
  72.     else:
  73.         speed_x = 0
  74.  
  75.     if keys[pygame.K_SPACE]:
  76.         speed_y = -15
  77.  
  78.     speed_y += 1
  79.  
  80.     player.x += speed_x
  81.     player.y += speed_y
  82.  
  83.     if player.x < 0:
  84.         player.x = 0
  85.     if player.x > width - player_width:
  86.         player.x = width - player_width
  87.     if player.y < 0:
  88.         player.y = 0
  89.     if player.y > height - player.height:
  90.         player.y = height - player.height
  91.    
  92.     for platform in platforms:
  93.         if player.colliderect(platform):
  94.             resolve_collision(player, platform)
  95.        
  96.     # ---
  97.     # Izris likov
  98.     # ---
  99.  
  100.     window.blit(player_image, player)
  101.     for platform in platforms:
  102.         pygame.draw.rect(window, (0, 127, 0), platform)
  103.         # window.blit(slika_ovire, platform)
  104.  
  105.     # ---
  106.  
  107.     pygame.display.update()
  108.     clock.tick(60)
  109.  
  110. pygame.quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement