Skip to content

Instantly share code, notes, and snippets.

@LevBravE
Created April 20, 2020 21:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LevBravE/9c2774d227c7e1e3af732413ede8b500 to your computer and use it in GitHub Desktop.
Save LevBravE/9c2774d227c7e1e3af732413ede8b500 to your computer and use it in GitHub Desktop.
import os
import pygame
pygame.init()
size = width, height = 800, 400
screen = pygame.display.set_mode(size)
def load_image(name, color_key=None):
fullname = os.path.join('data', name)
try:
image = pygame.image.load(fullname).convert()
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)
if color_key is not None:
if color_key == -1:
color_key = image.get_at((0, 0))
image.set_colorkey(color_key)
else:
image = image.convert_alpha()
return image
class Mountain(pygame.sprite.Sprite):
image = load_image("mountains.png")
def __init__(self):
super().__init__(all_sprites)
self.image = Mountain.image
self.rect = self.image.get_rect()
# вычисляем маску для эффективного сравнения
self.mask = pygame.mask.from_surface(self.image)
# располагаем горы внизу
self.rect.bottom = height
class Landing(pygame.sprite.Sprite):
image = load_image("pt.png")
def __init__(self, pos):
super().__init__(all_sprites)
self.image = Landing.image
self.rect = self.image.get_rect()
# вычисляем маску для эффективного сравнения
self.mask = pygame.mask.from_surface(self.image)
self.rect.x = pos[0]
self.rect.y = pos[1]
def update(self):
# если еще в небе
if not pygame.sprite.collide_mask(self, mountain):
self.rect = self.rect.move(0, 1)
# группа, содержащая все спрайты
all_sprites = pygame.sprite.Group()
mountain = Mountain()
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.MOUSEBUTTONDOWN:
Landing(event.pos)
screen.fill(pygame.Color("black"))
all_sprites.draw(screen)
all_sprites.update()
pygame.display.flip()
clock.tick(50)
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment