Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- pygame.init()
- SCREEN_WIDTH = 800
- SCREEN_HEIGH = 600
- screen_surface = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGH))
- pygame.display.set_caption("Pierwsza Gra")
- game_status = True
- clock = pygame.time.Clock()
- 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
- image, surface, rect = img_list
- screen_surface.blit(surface, rect)
- pass
- def set_position(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
- x = 0
- y = 0
- if keys[pygame.K_LSHIFT]:
- speed*=5
- if keys[pygame.K_w]:
- y-=speed
- if keys[pygame.K_s]:
- y+=speed
- if keys[pygame.K_d]:
- x+=speed
- if keys[pygame.K_a]:
- x-=speed
- return [x, y]
- def limit_position(position):
- x,y = position
- x = max(0, min(x, SCREEN_WIDTH))
- y = max(0, min(y, SCREEN_HEIGH))
- return [x,y]
- player_pos = [SCREEN_WIDTH//2, SCREEN_HEIGH//2]
- player = load_image('player.png', player_pos)
- background_color = [50, 136, 89]
- while game_status:
- events = pygame.event.get()
- for event in events:
- print(event)
- if event.type == pygame.QUIT:
- game_status = False
- 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(position=player_pos)
- player = set_position(player, player_pos)
- screen_surface.fill(background_color)
- print_image(player)
- pygame.display.update()
- clock.tick(10)
- print("Zamykanie aplikacji")
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment