Guest User

Mitch!

a guest
Mar 14th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.92 KB | None | 0 0
  1. import pygame
  2. import os, sys
  3. from random import randint
  4. import time
  5. import math
  6. import random
  7.  
  8. import requests
  9.  
  10. #from piece import Piece
  11.  
  12. pygame.init()
  13.  
  14. class Piece:
  15.     def __init__(self, frame, x, y):
  16.         self.frame = frame
  17.         self.x = x
  18.         self.y = y
  19.         self.selected = False
  20.     def draw(self, screen):
  21.         screen.blit(self.frame, (self.x, self.y))
  22.         if self.selected == False:
  23.             width, height = self.frame.get_size()
  24.             pygame.draw.rect(screen, (0,0,0), (self.x,self.y,width,height), 2)
  25.         else:
  26.             width, height = self.frame.get_size()
  27.             pygame.draw.rect(screen, (255,0,0), (self.x,self.y,width,height), 2)
  28.  
  29.     def swap(self, screen, item2):
  30.         screen.blit(item2.frame, (self.x,self.y))
  31.         screen.blit(self.frame, (item2.x,item2.y))
  32.         store = [item2.x,item2.y,item2.frame]
  33.         item2.x, item2.y,item2.selected = self.x, self.y, False
  34.         self.x, self.y, self.selected = store[0], store[1], False
  35.        
  36.  
  37. def get_dog_image():
  38.     Found=False
  39.     while Found==False:
  40.         photo_id = requests.get("https://random.dog/woof").text.lower()
  41.         photo_link = f"https://random.dog/{photo_id}"
  42.         if photo_id.split(".")[1].lower().endswith("mp4") or photo_id.split(".")[1].lower().endswith("gif"):
  43.             pass
  44.         else:
  45.             photo_id=photo_id.split(".")[0]
  46.             request = requests.get(photo_link)
  47.  
  48.             if request.status_code == 200:
  49.                 with open(f"{photo_id}.png", "wb") as photo:
  50.                     photo.write(request.content)
  51.             print(str(photo_id+".png"))
  52.             return str(photo_id+".png")
  53.  
  54. def get_pokemon_image():
  55.     return f"Pokemon{randint(0,802)}.png"
  56.    
  57. def strip_from_sheet(sheet, start, size, columns, rows=1):
  58.     frames = []
  59.     for j in range(rows):
  60.         for i in range(columns):
  61.             location = (start[0]+size[0]*i, start[1]+size[1]*j)
  62.             frames.append(sheet.subsurface(pygame.Rect(location, size)))
  63.     return frames
  64.  
  65. def commonMulti(size):
  66.     numbers = [i for i in range(5,10)]
  67.     factors = []
  68.     for item in numbers:
  69.         if size[0]%item==0 and size[1]%item==0:
  70.             factors.append(item)
  71.         else:
  72.             pass
  73.     if len(factors)>0:
  74.         return factors[-1]
  75.     else:
  76.         return None
  77.  
  78. def gameLoop(width, height, random_indicies, screen, frames, split,objects):
  79.     clock = pygame.time.Clock()
  80.     while True:
  81.         for event in pygame.event.get():
  82.             if event.type == pygame.QUIT:
  83.                 pygame.quit()
  84.                 exit()
  85.         if pygame.mouse.get_pressed()[0]==1:
  86.             print(pygame.mouse.get_pressed())
  87.             x, y = event.pos      
  88.             for item in objects:
  89.                 item.selected = False
  90.                 if item.frame.get_rect().move(item.x, item.y).collidepoint(x, y):
  91.                     item.selected = True
  92.                     break
  93.         elif pygame.mouse.get_pressed()[2]==1:
  94.             for item in objects:
  95.                 item.selected = False
  96.                
  97.         selected=[]
  98.         for item in objects:
  99.             if item.selected == True and item.selected not in selected:
  100.                 selected.append(item)
  101.                 if len(selected)==2:
  102.                     selected[0].swap(screen, selected[1])
  103.                     selected=[]
  104.             item.draw(screen)
  105.  
  106.         pygame.display.update()
  107.         msElapsed = clock.tick(60)
  108.        
  109. def shuffle(split, size, frames, screen,objects):
  110.     random_indicies=[i for i in range(split[0]*split[1])]
  111.     random.shuffle(random_indicies)
  112.     width = size[0]/split[0]
  113.     height = size[1]/split[1]
  114.     for index, frame in enumerate(frames):
  115.         num = random_indicies[index]
  116.         x = width * (num % split[0])
  117.         y = height * (num // split[0])
  118.        
  119.         piece = Piece(frame, x, y)
  120.         objects.append(piece)
  121.     gameLoop(width, height, random_indicies, screen, frames, split,objects)
  122.  
  123. def get_multi(size):
  124.     if size[0]<100 or size[1]<100:
  125.         return 3
  126.     elif size[0]<250 or size[1]<250:
  127.         return 2
  128.     else:
  129.         return 1
  130.    
  131. def startup(objects):
  132.     sheet = pygame.image.load(get_pokemon_image())
  133.     size = sheet.get_size()
  134.     multi=get_multi(size)
  135.    
  136.     sheet = pygame.transform.scale(sheet,(((round(size[0],-1)*multi)),((round(size[1],-1)*multi))))
  137.     size = sheet.get_size()
  138.     screen = pygame.display.set_mode((size[0], size[1]))
  139.     if commonMulti(size)!=None:
  140.         x = commonMulti(size)
  141.         divider = size[0]//x
  142.         split = [size[0]//divider,size[1]//divider]
  143.     screen_rect = screen.get_rect()
  144.     frames = strip_from_sheet(sheet, (0,0), (size[0]/split[0],size[1]/split[1]), split[0],split[1])
  145.     pygame.display.set_caption('Jigsaw Game')
  146.  
  147.     shuffle(split, size, frames, screen,objects)
  148.  
  149.  
  150. if __name__ == "__main__":
  151.     objects = []
  152.     startup(objects)
Advertisement
Add Comment
Please, Sign In to add comment