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