thorpy exercice 2

For posting your projects involving ThorPy
buffalo
Posts: 44
Joined: Thu Feb 25, 2016 10:34 am

thorpy exercice 2

Postby buffalo » Tue Jan 30, 2018 3:09 am

Image
https://mega.nz/#!g2BSWQ4Q!XC8mKVzG8Jz2QsRxkTDKUNxwfy5QzzJ1uY0xg-NqH4M
http://www.pygame.org/project/3359/5425

Code: Select all

#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import thorpy
from thorpy.elements._wrappers import *
import pygame
import random
#print(pygame.__file__[:-1])
#print(thorpy.__file__[:-1])


"""                   ___ AUTO DETECT FOR FULL SCREEN ___
#uncomment to get full screen, you have to install mss first , try:    sudo pip3 install mss

from mss import mss
screen = mss().monitors[1]
print(screen)
WIDTH = screen['width']
HEIGHT = screen['height']
"""

WIDTH = 1200
HEIGHT = 640


print(" WIDTH :  ", WIDTH)
print(" HEIGHT :  ", HEIGHT)

application = thorpy.Application((WIDTH, HEIGHT), "Thorpy Exo 2 V2")

activ = "rousse.png"

activdump = thorpy.load_image(activ).convert()
transColor = activdump.get_at((0, 0)) # pixel color at 0,0
activdump.set_colorkey(transColor) # apply set_colorkey for transparency

activsize = activdump.get_size()
print("activsize  :  ", activsize)

violet_surface = pygame.Surface(activsize)
violet_color = (155, 55, 200)
violet_surface.fill(violet_color)
violet_surface.set_alpha(50) # transparency level
violet_image = thorpy.Image.make(violet_surface) # make image from surface


class MonsterKeeper():
    def __init__(self, picfile, source_columns, source_lines, target):

        """
        my spritesheet have 9 ladies per line, and 4 lines
        here , source_columns = 9 ; source_lines = 4
        target is the image where i will put the lady graph
        """

        self.picfile = picfile

        self.loaded_pic = thorpy.load_image(picfile).convert()
        transColor = self.loaded_pic.get_at((0, 0))
        self.loaded_pic.set_colorkey(transColor)

        self.source_size = self.loaded_pic.get_size()
        self.picWidth = self.source_size[0]
        self.picHeight = self.source_size[1]

        self.source_columns = source_columns
        self.source_lines = source_lines
        self.crop_positions = []

        self.target = target

    @property
    def tile_width(self):

        return (self.picWidth / self.source_columns)

    @property
    def tile_height(self):

        return (self.picHeight / self.source_lines)

    def fill_evolutions(self):
        """
        This is my algorythm to calculate crops from spritesheet,
        you need regular intervals on your spritesheet otherwise it shifts
        """

        for j in range(0, self.source_lines):
            for i in range(0, self.source_columns):

                self.crop_positions.append(
                    (i * self.tile_width, j * self.tile_height,
                     self.tile_width, self.tile_height))

    def change_crop(self):

        """
        When you press bar, the lady get a random crop on spriteshhet.
        You give a new rect, calculated here from columns and lines number on you spritesheet

        """

        rsuf = random.choice(self.crop_positions)
        print(rsuf)

        cropped = pygame.Surface((self.tile_width, self.tile_height))
        print("\n cropped = pygame.Surface((self.tile_width, self.tile_height))")
        print("type(cropped)  :  ", type(cropped))

        cropped.blit(self.loaded_pic, (0, 0), (rsuf))

        cropped.convert() # canal alpha
        transColor = cropped.get_at((0, 0)) # canal alpha
        cropped.set_colorkey(transColor) # canal alpha

        self.target.set_image(cropped)
        self.target.unblit_and_reblit()

        thorpy.functions.refresh_current_menu()


myMKp = MonsterKeeper(activ, 9, 4, violet_image)

myMKp.fill_evolutions()

pic1_loaded = thorpy.load_image("dk_top.png").convert()
transColor = pic1_loaded.get_at((0, 0))
pic1_loaded.set_colorkey(transColor)

#normal, pressed, hover = pic1_loaded, pic1_loaded, pic1_loaded #

image1 = thorpy.Image.make(path=pic1_loaded)

pic2_loaded = thorpy.load_image("dk_top_NEG.png").convert()
transColor = pic2_loaded.get_at((0, 0))
pic2_loaded.set_colorkey(transColor)

#normal, pressed, hover = pic2_loaded, pic2_loaded, pic2_loaded
image2 = thorpy.Image.make(path=pic2_loaded)


class Perso:
    def __init__(self, right, left, top, bottom, target):

        self.right = pygame.image.load(right).convert_alpha()
        self.left = pygame.image.load(left).convert_alpha()
        self.top = pygame.image.load(top).convert_alpha()
        self.bottom = pygame.image.load(bottom).convert_alpha()

        self.target = target

        self.direction = self.top

    def give_direction(self):
        print("self.direction  :  ", self.direction)

    def turn(self, direction):

        if direction == 'right':
            self.direction = self.right
        if direction == 'left':
            self.direction = self.left
        if direction == 'top':
            self.direction = self.top
        if direction == 'bottom':
            self.direction = self.bottom

    def refreshpic(self):
        self.target.set_image(self.direction)
        self.target.unblit_and_reblit()
        thorpy.functions.refresh_current_menu()


dk = Perso("dk_right.png", "dk_left.png", "dk_top.png", "dk_bottom.png",
           image1)

dk_NEG = Perso("dk_right_NEG.png", "dk_left_NEG.png", "dk_top_NEG.png",
               "dk_bottom_NEG.png", image2)


def lateralbtn_build():

    buttons_left = thorpy.make_button("<", Lclik)
    buttons_left.set_size((50, 50))

    buttons_up = thorpy.make_button("^", Uclik)
    buttons_up.set_size((50, 50))

    LU_box = thorpy.Box.make([buttons_left, buttons_up])

    buttons_right = thorpy.make_button(">", Rclik)
    buttons_right.set_size((50, 50))

    buttons_bottom = thorpy.make_button("v", Bclik)
    buttons_bottom.set_size((50, 50))

    RB_box = thorpy.Box.make([buttons_right, buttons_bottom])

    buttons_left.user_params = {}
    buttons_up.user_params = {}
    buttons_right.user_params = {}
    buttons_bottom.user_params = {}

    bouton_group = thorpy.make_group([LU_box, image2, RB_box], "h")
    return bouton_group


def SPACE_push():

    background.unblit_and_reblit()
    myMKp.change_crop()
    background.unblit_and_reblit()


def Rpush():

    dk.turn('right')
    dk.give_direction()
    dk.refreshpic()
    background.unblit_and_reblit()


def Lpush():

    dk.turn('left')
    dk.give_direction()
    dk.refreshpic()
    background.unblit_and_reblit()


def Upush():

    dk.turn('top')
    dk.give_direction()
    dk.refreshpic()
    background.unblit_and_reblit()


def Bpush():

    dk.turn('bottom')
    dk.give_direction()
    dk.refreshpic()
    background.unblit_and_reblit()


def Rclik():

    dk_NEG.turn('right')
    dk_NEG.give_direction()
    dk_NEG.refreshpic()


def Lclik():

    dk_NEG.turn('left')
    dk_NEG.give_direction()
    dk_NEG.refreshpic()


def Uclik():

    dk_NEG.turn('top')
    dk_NEG.give_direction()
    dk_NEG.refreshpic()


def Bclik():

    dk_NEG.turn('bottom')
    dk_NEG.give_direction()
    dk_NEG.refreshpic()


bouton_group = lateralbtn_build()

reac_R = thorpy.ConstantReaction(pygame.KEYDOWN, Rpush, {
    "key": pygame.K_RIGHT
})
reac_L = thorpy.ConstantReaction(pygame.KEYDOWN, Lpush, {"key": pygame.K_LEFT})
reac_U = thorpy.ConstantReaction(pygame.KEYDOWN, Upush, {"key": pygame.K_UP})
reac_B = thorpy.ConstantReaction(pygame.KEYDOWN, Bpush, {"key": pygame.K_DOWN})
reac_SpaceBar = thorpy.ConstantReaction(pygame.KEYDOWN, SPACE_push, {
    "key": pygame.K_SPACE
})

elements = [violet_image, image1, bouton_group]
image_background = thorpy.load_image("rocky_forest.jpg")
background = thorpy.Background.make(image=image_background, elements=elements)
background.add_reactions([reac_R, reac_L, reac_U, reac_B, reac_SpaceBar])

thorpy.store(background)
menu = thorpy.Menu(background)
menu.play()
application.quit()


Return to “Projects”

Who is online

Users browsing this forum: No registered users and 1 guest