Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame
- from random import randint
- import random
- import requests
- # from piece import Piece
- pygame.init()
- class Piece:
- def __init__(self, frame, x, y, goal_x, goal_y):
- self.frame = frame
- self.goal_x = goal_x
- self.goal_y = goal_y
- self.x = x
- self.y = y
- self.selected = False
- def draw(self, screen):
- screen.blit(self.frame, (self.x, self.y))
- if self.isComplete():
- width, height = self.frame.get_size()
- pygame.draw.rect(screen, (0, 255, 0, 50), (self.x, self.y, width, height), 2)
- elif self.selected:
- width, height = self.frame.get_size()
- pygame.draw.rect(screen, (255, 0, 0, 50), (self.x, self.y, width, height), 2)
- else:
- width, height = self.frame.get_size()
- pygame.draw.rect(screen, (0, 0, 0, 50), (self.x, self.y, width, height), 2)
- def swap(self, other):
- self.x, other.x = other.x, self.x
- self.y, other.y = other.y, self.y
- def collision(self, x, y):
- rect = self.frame.get_rect()
- rect = rect.move(self.x, self.y)
- return rect.collidepoint(x, y)
- def isComplete(self):
- return (self.x == self.goal_x) and (self.y == self.goal_y)
- def get_dog_image():
- while True:
- 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") or \
- photo_id.split(".")[1].lower().endswith("webm"):
- pass
- else:
- request = requests.get(photo_link)
- if request.status_code == 200:
- with open(f"{photo_id}", "wb") as photo:
- photo.write(request.content)
- return str(photo_id)
- 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(3, 10)]
- factors = []
- for item in numbers:
- if size[0] % item == 0 and size[1] % item == 0:
- one = size[0] // item
- two = size[1] // item
- if one < 125 and two < 100:
- factors.append(item)
- else:
- pass
- if len(factors) > 0:
- return factors[-1]
- def gameLoop(screen, objects, frames):
- game = True
- while game:
- screen.fill((255, 255, 255))
- # Check events
- for event in pygame.event.get():
- # Quit event
- if event.type == pygame.QUIT:
- pygame.quit()
- exit()
- # Mouse click event
- if event.type == pygame.MOUSEBUTTONDOWN:
- mx, my = event.pos
- if event.button == 1: # Left
- # Try to find selected
- selectPiece = None
- for item in objects:
- if item.selected:
- selectPiece = item
- # Look for click collision
- for item in objects:
- if item.collision(mx, my):
- if selectPiece:
- # Another piece is selected: swap
- selectPiece.swap(item)
- selectPiece.selected = False
- else:
- # No other pieces are selected: select
- item.selected = True
- if event.button == 3: # Right
- # Deselect all pieces
- for item in objects:
- item.selected = False
- # Draw items
- for item in objects:
- item.draw(screen)
- count = 0
- for object in objects:
- if object.isComplete():
- count += 1
- else:
- break
- if count == len(objects):
- print("GG")
- pygame.quit()
- quit()
- game = False
- pygame.display.update()
- 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])
- goal_x = width * (index % split[0])
- goal_y = height * (index // split[0])
- piece = Piece(frame, x, y, goal_x, goal_y)
- objects.append(piece)
- gameLoop(screen, objects, frames)
- 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 = []
- found = False
- sheet = ""
- while not found:
- try:
- sheet = pygame.image.load(get_dog_image())
- found = True
- except pygame.error:
- pass
- sheet.set_alpha(255)
- 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]))
- screen.fill((255, 255, 255))
- if commonMulti(size):
- x = commonMulti(size)
- divider = size[0] // x
- split = [size[0] // divider, size[1] // divider]
- else:
- split = ""
- startup()
- 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__":
- startup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement