Advertisement
Harrix

Untitled

Jan 16th, 2022
1,355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.43 KB | None | 0 0
  1. import pygame
  2. from pathlib import Path
  3.  
  4. from PIL import Image, ImageDraw
  5.  
  6. FPS = 60
  7. WIDTH = 600
  8. HEIGHT = 400
  9. FULLSCREEN = False
  10.  
  11.  
  12. def pilImageToSurface(pilImage):
  13.     return pygame.image.fromstring(
  14.         pilImage.tobytes(), pilImage.size, pilImage.mode).convert()
  15.  
  16.  
  17. class Tile(pygame.sprite.Sprite):
  18.     def __init__(self, pos_x, pos_y):
  19.         if not hasattr(Tile, "group"):
  20.             Tile.group = pygame.sprite.Group()
  21.             Tile.tile_size = 50
  22.         super().__init__(Tile.group)
  23.  
  24.         image = Image.open("data/box.png")
  25.  
  26.         this_image = pilImageToSurface(image)
  27.  
  28.         self.image = this_image
  29.         self.rect = self.image.get_rect().move(
  30.             Tile.tile_size * pos_x, Tile.tile_size * pos_y
  31.         )
  32.  
  33.  
  34. def main(fps, width, height, fullscreen=False):
  35.     pygame.init()
  36.     if not fullscreen:
  37.         screen = pygame.display.set_mode((width, height))
  38.     else:
  39.         screen = pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
  40.  
  41.     clock = pygame.time.Clock()
  42.  
  43.     Tile(0, 0)
  44.  
  45.     running = True
  46.     while running:
  47.         screen.fill((255, 255, 255))
  48.         for event in pygame.event.get():
  49.             if event.type == pygame.QUIT:
  50.                 pygame.quit()
  51.                 return
  52.  
  53.         Tile.group.draw(screen)
  54.  
  55.         pygame.display.flip()
  56.         clock.tick(fps)
  57.     pygame.quit()
  58.  
  59.  
  60. if __name__ == "__main__":
  61.     main(FPS, WIDTH, HEIGHT, FULLSCREEN)
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement