Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame # Import biblioteki pygame
- import random
- from time import sleep
- pygame.init() # Inicjalizacja biblioteki
- SCREEN_WIDTH = 800
- SCREEN_HEIGHT = 600
- clock = pygame.time.Clock()
- frames_per_second = 60
- bonus_images = [
- "bonus_1.png",
- "bonus_2.png",
- "bonus_3.png"
- ]
- bonus_object = []
- screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
- pygame.display.set_caption("Moja pierwsza gra")
- def generate_bonus_object():
- # indeks = random.randint(0, len(bonus_images) - 1)
- # print(bonus_images[indeks])
- image_name = random.choice(bonus_images)
- x = random.randint(0, SCREEN_WIDTH)
- y = random.randint(0, SCREEN_HEIGHT)
- position = [x, y]
- new_obj = load_image(image_name, position)
- bonus_object.append(new_obj)
- def print_bonus_object():
- for obj in bonus_object:
- print_image(obj)
- def load_image(img_path, position):
- image = pygame.image.load(img_path)
- surface = image.convert()
- transparent_color = (0, 0, 0)
- surface.set_colorkey(transparent_color)
- rect = surface.get_rect(center=position)
- return [image, surface, rect]
- def print_image(img_list):
- image, surface, rect = img_list
- screen_surface.blit(surface, rect)
- def set_position_image(img_list, position):
- image, surface, rect = img_list
- rect = surface.get_rect(center=position)
- return [image, surface, rect]
- def calculate_player_movement(keys):
- speed = 10
- delta_x = 0
- delta_y = 0
- if keys[pygame.K_LSHIFT]:
- speed = 20
- if keys[pygame.K_w]:
- delta_y -= speed
- if keys[pygame.K_s]:
- delta_y += speed
- if keys[pygame.K_a]:
- delta_x -= speed
- if keys[pygame.K_d]:
- delta_x += speed
- return [delta_x, delta_y]
- def limit_position(position):
- x, y = position
- x = max(0, min(x, SCREEN_WIDTH))
- y = max(0, min(y, SCREEN_HEIGHT))
- return [x, y]
- def check_collisions():
- # rect.colliderect(rect_player)
- global score
- rect_player = player[2]
- for index in range(len(bonus_object) - 1, -1, -1):
- obj = bonus_object[index]
- rect = obj[2]
- if rect.colliderect(rect_player):
- bonus_object.pop(index)
- score += 1
- def print_points():
- global score
- text = f"Score: {score}"
- font = pygame.font.SysFont("verdana", 20)
- color = [255, 255, 255]
- position = [0, 0]
- label = font.render(text, False, color)
- screen_surface.blit(label, position)
- player_pos = [SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2]
- player = load_image("player.png", player_pos)
- frames_counter = 0
- score = 0
- game_loop = True
- while game_loop:
- frames_counter += 1
- pressed_keys = pygame.key.get_pressed()
- delta_x, delta_y = calculate_player_movement(pressed_keys)
- player_pos[0] += delta_x
- player_pos[1] += delta_y
- player_pos = limit_position(player_pos)
- events = pygame.event.get()
- for event in events:
- if event.type == pygame.QUIT:
- game_loop = False
- player = set_position_image(player, player_pos)
- sekund = random.randint(1, 10)
- sekund = 1
- if frames_counter % (frames_per_second * sekund) == 0:
- generate_bonus_object()
- check_collisions()
- screen_surface.fill((9, 42, 141))
- print_image(player)
- print_bonus_object()
- print_points()
- pygame.display.update()
- print(f"Twoj wynik to {score}")
- clock.tick(frames_per_second)
- pygame.quit() # Koniec pygame
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement