Advertisement
Isaacmm

Untitled

May 19th, 2022
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. import pygame # Import biblioteki pygame
  2. import random
  3. from time import sleep
  4. pygame.init() # Inicjalizacja biblioteki
  5.  
  6. SCREEN_WIDTH = 800
  7. SCREEN_HEIGHT = 600
  8.  
  9. clock = pygame.time.Clock()
  10. frames_per_second = 60
  11.  
  12. bonus_images = [
  13. "bonus_1.png",
  14. "bonus_2.png",
  15. "bonus_3.png"
  16. ]
  17.  
  18. bonus_object = []
  19.  
  20. screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
  21. pygame.display.set_caption("Moja pierwsza gra")
  22.  
  23. def generate_bonus_object():
  24. # indeks = random.randint(0, len(bonus_images) - 1)
  25. # print(bonus_images[indeks])
  26. image_name = random.choice(bonus_images)
  27. x = random.randint(0, SCREEN_WIDTH)
  28. y = random.randint(0, SCREEN_HEIGHT)
  29. position = [x, y]
  30. new_obj = load_image(image_name, position)
  31. bonus_object.append(new_obj)
  32.  
  33. def print_bonus_object():
  34. for obj in bonus_object:
  35. print_image(obj)
  36.  
  37. def load_image(img_path, position):
  38. image = pygame.image.load(img_path)
  39. surface = image.convert()
  40.  
  41. transparent_color = (0, 0, 0)
  42. surface.set_colorkey(transparent_color)
  43.  
  44. rect = surface.get_rect(center=position)
  45. return [image, surface, rect]
  46.  
  47. def print_image(img_list):
  48. image, surface, rect = img_list
  49. screen_surface.blit(surface, rect)
  50.  
  51. def set_position_image(img_list, position):
  52. image, surface, rect = img_list
  53. rect = surface.get_rect(center=position)
  54. return [image, surface, rect]
  55.  
  56. def calculate_player_movement(keys):
  57. speed = 10
  58. delta_x = 0
  59. delta_y = 0
  60.  
  61. if keys[pygame.K_LSHIFT]:
  62. speed = 20
  63.  
  64. if keys[pygame.K_w]:
  65. delta_y -= speed
  66. if keys[pygame.K_s]:
  67. delta_y += speed
  68. if keys[pygame.K_a]:
  69. delta_x -= speed
  70. if keys[pygame.K_d]:
  71. delta_x += speed
  72. return [delta_x, delta_y]
  73.  
  74. def limit_position(position):
  75. x, y = position
  76. x = max(0, min(x, SCREEN_WIDTH))
  77. y = max(0, min(y, SCREEN_HEIGHT))
  78. return [x, y]
  79.  
  80. def check_collisions():
  81. # rect.colliderect(rect_player)
  82. global score
  83. rect_player = player[2]
  84. for index in range(len(bonus_object) - 1, -1, -1):
  85. obj = bonus_object[index]
  86. rect = obj[2]
  87. if rect.colliderect(rect_player):
  88. bonus_object.pop(index)
  89. score += 1
  90.  
  91. def print_points():
  92. global score
  93. text = f"Score: {score}"
  94. font = pygame.font.SysFont("verdana", 20)
  95. color = [255, 255, 255]
  96. position = [0, 0]
  97. label = font.render(text, False, color)
  98. screen_surface.blit(label, position)
  99.  
  100. player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
  101. player = load_image("player.png", player_pos)
  102.  
  103. frames_counter = 0
  104. score = 0
  105.  
  106. game_loop = True
  107. while game_loop:
  108. frames_counter += 1
  109. pressed_keys = pygame.key.get_pressed()
  110. delta_x, delta_y = calculate_player_movement(pressed_keys)
  111. player_pos[0] += delta_x
  112. player_pos[1] += delta_y
  113. player_pos = limit_position(player_pos)
  114.  
  115. events = pygame.event.get()
  116. for event in events:
  117. if event.type == pygame.QUIT:
  118. game_loop = False
  119.  
  120. player = set_position_image(player, player_pos)
  121. sekund = random.randint(1, 10)
  122. sekund = 1
  123. if frames_counter % (frames_per_second * sekund) == 0:
  124. generate_bonus_object()
  125. check_collisions()
  126. screen_surface.fill((9, 42, 141))
  127. print_image(player)
  128. print_bonus_object()
  129. print_points()
  130. pygame.display.update()
  131. print(f"Twoj wynik to {score}")
  132. clock.tick(frames_per_second)
  133.  
  134. pygame.quit() # Koniec pygame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement