Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- import os, sys
- from random import randint
- import time
- import math
- import random
- import requests
- #from piece import Piece
- pygame.init()
- class Piece:
- def __init__(self, frame, x, y):
- self.frame = frame
- self.x = x
- self.y = y
- self.selected = False
- def draw(self, screen):
- screen.blit(self.frame, (self.x, self.y))
- if self.selected == False:
- width, height = self.frame.get_size()
- pygame.draw.rect(screen, (0,0,0), (self.x,self.y,width,height), 2)
- else:
- width, height = self.frame.get_size()
- pygame.draw.rect(screen, (255,0,0), (self.x,self.y,width,height), 2)
- def swap(self, screen, item2):
- screen.blit(item2.frame, (self.x,self.y))
- screen.blit(self.frame, (item2.x,item2.y))
- store = [item2.x,item2.y,item2.frame]
- item2.x, item2.y,item2.selected = self.x, self.y, False
- self.x, self.y, self.selected = store[0], store[1], False
- def get_dog_image():
- Found=False
- while Found==False:
- photo_id = requests.get("https://random.dog/woof").text.lower()
- photo_link = f"https://random.dog/{photo_id}"
- if photo_id.split(".")[1].lower().endswith("mp4") or photo_id.split(".")[1].lower().endswith("gif"):
- pass
- else:
- photo_id=photo_id.split(".")[0]
- request = requests.get(photo_link)
- if request.status_code == 200:
- with open(f"{photo_id}.png", "wb") as photo:
- photo.write(request.content)
- print(str(photo_id+".png"))
- return str(photo_id+".png")
- def get_pokemon_image():
- return f"Pokemon{randint(0,802)}.png"
- def strip_from_sheet(sheet, start, size, columns, rows=1):
- frames = []
- for j in range(rows):
- for i in range(columns):
- location = (start[0]+size[0]*i, start[1]+size[1]*j)
- frames.append(sheet.subsurface(pygame.Rect(location, size)))
- return frames
- def commonMulti(size):
- numbers = [i for i in range(5,10)]
- factors = []
- for item in numbers:
- if size[0]%item==0 and size[1]%item==0:
- factors.append(item)
- else:
- pass
- if len(factors)>0:
- return factors[-1]
- else:
- return None
- def gameLoop(width, height, random_indicies, screen, frames, split,objects):
- clock = pygame.time.Clock()
- while True:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- if pygame.mouse.get_pressed()[0]==1:
- print(pygame.mouse.get_pressed())
- x, y = event.pos
- for item in objects:
- item.selected = False
- if item.frame.get_rect().move(item.x, item.y).collidepoint(x, y):
- item.selected = True
- break
- elif pygame.mouse.get_pressed()[2]==1:
- for item in objects:
- item.selected = False
- selected=[]
- for item in objects:
- if item.selected == True and item.selected not in selected:
- selected.append(item)
- if len(selected)==2:
- selected[0].swap(screen, selected[1])
- selected=[]
- item.draw(screen)
- pygame.display.update()
- msElapsed = clock.tick(60)
- def shuffle(split, size, frames, screen,objects):
- random_indicies=[i for i in range(split[0]*split[1])]
- random.shuffle(random_indicies)
- width = size[0]/split[0]
- height = size[1]/split[1]
- for index, frame in enumerate(frames):
- num = random_indicies[index]
- x = width * (num % split[0])
- y = height * (num // split[0])
- piece = Piece(frame, x, y)
- objects.append(piece)
- gameLoop(width, height, random_indicies, screen, frames, split,objects)
- def get_multi(size):
- if size[0]<100 or size[1]<100:
- return 3
- elif size[0]<250 or size[1]<250:
- return 2
- else:
- return 1
- def startup(objects):
- sheet = pygame.image.load(get_pokemon_image())
- size = sheet.get_size()
- multi=get_multi(size)
- sheet = pygame.transform.scale(sheet,(((round(size[0],-1)*multi)),((round(size[1],-1)*multi))))
- size = sheet.get_size()
- screen = pygame.display.set_mode((size[0], size[1]))
- if commonMulti(size)!=None:
- x = commonMulti(size)
- divider = size[0]//x
- split = [size[0]//divider,size[1]//divider]
- screen_rect = screen.get_rect()
- frames = strip_from_sheet(sheet, (0,0), (size[0]/split[0],size[1]/split[1]), split[0],split[1])
- pygame.display.set_caption('Jigsaw Game')
- shuffle(split, size, frames, screen,objects)
- if __name__ == "__main__":
- objects = []
- startup(objects)
Advertisement
Add Comment
Please, Sign In to add comment