Advertisement
Guest User

Mitch New

a guest
Mar 19th, 2019
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.17 KB | None | 0 0
  1. import pygame
  2. from random import randint
  3. import random
  4.  
  5. import requests
  6.  
  7. # from piece import Piece
  8.  
  9. pygame.init()
  10.  
  11.  
  12. class Piece:
  13.     def __init__(self, frame, x, y, goal_x, goal_y):
  14.         self.frame = frame
  15.         self.goal_x = goal_x
  16.         self.goal_y = goal_y
  17.         self.x = x
  18.         self.y = y
  19.         self.selected = False
  20.  
  21.     def draw(self, screen):
  22.         screen.blit(self.frame, (self.x, self.y))
  23.         if self.isComplete():
  24.             width, height = self.frame.get_size()
  25.             pygame.draw.rect(screen, (0, 255, 0, 50), (self.x, self.y, width, height), 2)
  26.         elif self.selected:
  27.             width, height = self.frame.get_size()
  28.             pygame.draw.rect(screen, (255, 0, 0, 50), (self.x, self.y, width, height), 2)
  29.         else:
  30.             width, height = self.frame.get_size()
  31.             pygame.draw.rect(screen, (0, 0, 0, 50), (self.x, self.y, width, height), 2)
  32.  
  33.     def swap(self, other):
  34.         self.x, other.x = other.x, self.x
  35.         self.y, other.y = other.y, self.y
  36.  
  37.     def collision(self, x, y):
  38.         rect = self.frame.get_rect()
  39.         rect = rect.move(self.x, self.y)
  40.         return rect.collidepoint(x, y)
  41.  
  42.     def isComplete(self):
  43.         return (self.x == self.goal_x) and (self.y == self.goal_y)
  44.  
  45.  
  46.  
  47. def get_dog_image():
  48.     while True:
  49.         photo_id = requests.get("https://random.dog/woof").text.lower()
  50.         photo_link = f"https://random.dog/{photo_id}"
  51.         if photo_id.split(".")[1].lower().endswith("mp4") or photo_id.split(".")[1].lower().endswith("gif") or \
  52.                 photo_id.split(".")[1].lower().endswith("webm"):
  53.             pass
  54.         else:
  55.             request = requests.get(photo_link)
  56.  
  57.             if request.status_code == 200:
  58.                 with open(f"{photo_id}", "wb") as photo:
  59.                     photo.write(request.content)
  60.             return str(photo_id)
  61.  
  62.  
  63. def get_pokemon_image():
  64.     return f"Pokemon{randint(0,802)}.png"
  65.  
  66.  
  67. def strip_from_sheet(sheet, start, size, columns, rows=1):
  68.     frames = []
  69.     for j in range(rows):
  70.         for i in range(columns):
  71.             location = (start[0] + size[0] * i, start[1] + size[1] * j)
  72.             frames.append(sheet.subsurface(pygame.Rect(location, size)))
  73.     return frames
  74.  
  75.  
  76. def commonMulti(size):
  77.     numbers = [i for i in range(3, 10)]
  78.  
  79.     factors = []
  80.     for item in numbers:
  81.         if size[0] % item == 0 and size[1] % item == 0:
  82.             one = size[0] // item
  83.             two = size[1] // item
  84.             if one < 125 and two < 100:
  85.                 factors.append(item)
  86.         else:
  87.             pass
  88.     if len(factors) > 0:
  89.         return factors[-1]
  90.  
  91.  
  92. def gameLoop(screen, objects, frames):
  93.     game = True
  94.     while game:
  95.         screen.fill((255, 255, 255))
  96.         # Check events
  97.         for event in pygame.event.get():
  98.             # Quit event
  99.             if event.type == pygame.QUIT:
  100.                 pygame.quit()
  101.                 exit()
  102.  
  103.             # Mouse click event
  104.             if event.type == pygame.MOUSEBUTTONDOWN:
  105.                 mx, my = event.pos
  106.                 if event.button == 1:  # Left
  107.  
  108.                     # Try to find selected
  109.                     selectPiece = None
  110.                     for item in objects:
  111.                         if item.selected:
  112.                             selectPiece = item
  113.  
  114.                     # Look for click collision
  115.                     for item in objects:
  116.                         if item.collision(mx, my):
  117.                             if selectPiece:
  118.                                 # Another piece is selected: swap
  119.                                 selectPiece.swap(item)
  120.                                 selectPiece.selected = False
  121.                             else:
  122.                                 # No other pieces are selected: select
  123.                                 item.selected = True
  124.  
  125.                 if event.button == 3:  # Right
  126.  
  127.                     # Deselect all pieces
  128.                     for item in objects:
  129.                         item.selected = False
  130.  
  131.         # Draw items
  132.         for item in objects:
  133.             item.draw(screen)
  134.  
  135.         count = 0
  136.         for object in objects:
  137.             if object.isComplete():
  138.                 count += 1
  139.             else:
  140.                 break
  141.         if count == len(objects):
  142.             print("GG")
  143.             pygame.quit()
  144.             quit()
  145.             game = False
  146.  
  147.         pygame.display.update()
  148.  
  149.  
  150. def shuffle(split, size, frames, screen, objects):
  151.     random_indicies = [i for i in range(split[0] * split[1])]
  152.     random.shuffle(random_indicies)
  153.     width = size[0] / split[0]
  154.     height = size[1] / split[1]
  155.     for index, frame in enumerate(frames):
  156.         num = random_indicies[index]
  157.         x = width * (num % split[0])
  158.         y = height * (num // split[0])
  159.         goal_x = width * (index % split[0])
  160.         goal_y = height * (index // split[0])
  161.         piece = Piece(frame, x, y, goal_x, goal_y)
  162.         objects.append(piece)
  163.     gameLoop(screen, objects, frames)
  164.  
  165.  
  166. def get_multi(size):
  167.     if size[0] < 100 or size[1] < 100:
  168.         return 3
  169.     elif size[0] < 250 or size[1] < 250:
  170.         return 2
  171.     else:
  172.         return 1
  173.  
  174.  
  175. def startup():
  176.     objects = []
  177.     found = False
  178.     sheet = ""
  179.     while not found:
  180.         try:
  181.             sheet = pygame.image.load(get_dog_image())
  182.             found = True
  183.         except pygame.error:
  184.             pass
  185.  
  186.     sheet.set_alpha(255)
  187.     size = sheet.get_size()
  188.     multi = get_multi(size)
  189.  
  190.     sheet = pygame.transform.scale(sheet, ((round(size[0], -1) // multi), (round(size[1], -1) // multi)))
  191.  
  192.     size = sheet.get_size()
  193.     screen = pygame.display.set_mode((size[0], size[1]))
  194.     screen.fill((255, 255, 255))
  195.     if commonMulti(size):
  196.         x = commonMulti(size)
  197.         divider = size[0] // x
  198.         split = [size[0] // divider, size[1] // divider]
  199.     else:
  200.         split = ""
  201.         startup()
  202.     frames = strip_from_sheet(sheet, (0, 0), (size[0] / split[0], size[1] / split[1]), split[0], split[1])
  203.     pygame.display.set_caption('Jigsaw Game')
  204.  
  205.     shuffle(split, size, frames, screen, objects)
  206.  
  207.  
  208. if __name__ == "__main__":
  209.     startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement