Skip to content

Instantly share code, notes, and snippets.

@LevBravE
Created April 20, 2020 21:26
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/00912018f64e183448928572da467dcf to your computer and use it in GitHub Desktop.
Save LevBravE/00912018f64e183448928572da467dcf to your computer and use it in GitHub Desktop.
import os
import random
import pygame
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
size = width, height = 400, 300
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
# группа, содержащая все спрайты
all_sprites = pygame.sprite.Group()
# изображение должно лежать в папке data
bomb_image = load_image("bomb.png")
for i in range(50):
# можно сразу создавать спрайты с указанием группы
bomb = pygame.sprite.Sprite(all_sprites)
bomb.image = bomb_image
bomb.rect = bomb.image.get_rect()
# задаем случайное местоположение бомбочке
bomb.rect.x = random.randrange(width)
bomb.rect.y = random.randrange(height)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
screen.fill(pygame.Color("white"))
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment