Advertisement
ewalkowka

fdsf

Dec 22nd, 2022
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.97 KB | None | 0 0
  1. # Import modulu pygame, dzieki ktoremu tworzymy aplikacje okienkowa
  2. import pygame
  3. # Inicjalizacja modułu
  4. pygame.init()
  5. # Utworzenie okna o określonych wymiarach
  6. SCREEN_WIDTH = 600
  7. SCREEN_HEIGHT = 600
  8.  
  9. screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  10. # Nadanie nazwy oknu
  11. pygame.display.set_caption('Pierwsza Gra')
  12.  
  13. # Utworzenie zegara, który nadzoruje stałe wartości fps
  14. clock = pygame.time.Clock()
  15.  
  16.  
  17. def load_image(img_path: str, position):
  18.     image = pygame.image.load(img_path)
  19.     surface = image.convert()
  20.  
  21.     transparent_color = (0, 0, 0)
  22.     surface.set_colorkey(transparent_color)
  23.  
  24.     # Pozycja wyświetlania obiektu zapisana jest w rect
  25.     rect = surface.get_rect(center=position)
  26.  
  27.     return [image, surface, rect]
  28.  
  29.  
  30. def print_image(img_list) -> None:
  31.     # [image, surface, rect]
  32.     image, surface, rect = img_list
  33.     screen_surface.blit(surface, rect)
  34.     pass
  35.  
  36.  
  37. def set_position_image(img_list, position):
  38.     image, surface, rect = img_list
  39.     rect = surface.get_rect(center=position)
  40.     return [image, surface, rect]
  41.  
  42.  
  43. def calculate_player_movement(keys):
  44.     # Poruszanie postacią
  45.     speed = 10
  46.     delta_x = 0
  47.     delta_y = 0
  48.  
  49.     if keys[pygame.K_LSHIFT]:
  50.         speed *= 5
  51.     if keys[pygame.K_w]:
  52.         delta_y -= speed
  53.     if keys[pygame.K_s]:
  54.         delta_y += speed
  55.     if keys[pygame.K_d]:
  56.         delta_x += speed
  57.     if keys[pygame.K_a]:
  58.         delta_x -= speed
  59.  
  60.     return [delta_x, delta_y]
  61.  
  62.  
  63. def limit_position(position):
  64.     x, y = position
  65.     x = max(0, min(x, SCREEN_WIDTH))
  66.     y = max(0, min(y, SCREEN_HEIGHT))
  67.     return [x, y]
  68.  
  69.  
  70. # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
  71. player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
  72. player = load_image('player.png', player_pos)
  73. background_color = [9, 42, 121]
  74.  
  75.  
  76. # Zmienna określająca, czy należy zamknąć okno
  77. game_status = True
  78. # Kod wykonywany póki aplikacja jest uruchomiona
  79. while game_status:
  80.  
  81.     # Odczytanie zdarzeń zarejestrowanych przez komputer
  82.     events = pygame.event.get()
  83.  
  84.     for event in events:
  85.         # Naciśnięto X - zamykanie aplikacji
  86.         if event.type == pygame.QUIT:
  87.             game_status = False
  88.         pass  # for event
  89.  
  90.     pressed_keys = pygame.key.get_pressed()
  91.  
  92.     delta_x, delta_y = calculate_player_movement(pressed_keys)
  93.  
  94.     # Zmiana wartości współrzędnych
  95.     player_pos[0] += delta_x
  96.     player_pos[1] += delta_y
  97.     # Sprawdzenie limitów współrzędnych
  98.     player_pos = limit_position(player_pos)
  99.  
  100.     # Zmiana współrzędnych obrazu
  101.     player = set_position_image(player, player_pos)
  102.  
  103.     # Uzupełnij tło
  104.     screen_surface.fill(background_color)
  105.     # Wyświetl gracza
  106.     print_image(player)
  107.  
  108.     # Odświeżenie wyświetlanego okna
  109.     pygame.display.update()
  110.  
  111.     clock.tick(60)
  112.     pass
  113.  
  114. print("Zamykanie aplikacji")
  115. # Zamknięcie aplikacji
  116. pygame.quit()
  117. # Zamknięcie skryptu
  118. quit()
  119.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement