New dragable copy to infinite

New dragable copy to infinite

Postby IvyMike » Fri May 03, 2013 6:58 pm

Hi,

I work on a project for my baccalaureat. And that is ,unfortunately for me, the most complicated part of the program.

In my alchemy program I will create a library for store my elements and after, make my combinations ...
I have already my surfaces and my dragable's pictures. My pictures are already in my library but I have only the quantity necessary of images to make only one combination. I would like to be able to make hundreds of them ! So, I need to to create news dragables copy has each time I select an element. Like a "palett". And I would like to apply this function for each new element created.

If you can halp me to make that and explain steps to me it will be beautiful !!!

This is the program.
Code: Select all
import pygame as pg
import sys
import itertools

ELE_LIST = ["feu","eau",]

TRANS_DICT = {("feu" ,"eau")   : "vapeur",
              ("vapeur" ,"eau") : "alcool",
              ("alcool","feu") : "molotov"}
                   
class Dragable(object):

    def __init__(self,kind,location):
       
        self.kind = kind
        self.img = self.get_img()
        self.rect = self.img.get_rect(center=location)
        self.selected = False
    def get_img(self):
       
        return pg.image.load(self.kind+"-icon.png").convert_alpha()

    def events(self,ev):
        if ev.type == pg.MOUSEBUTTONDOWN and self.rect.collidepoint(ev.pos):
            if ev.button == 1:
                self.selected = True

        elif ev.type == pg.MOUSEBUTTONUP:
            if ev.button == 1:
                self.selected = False

    def update(self,Surf):
        if self.selected:
            self.rect.center = pg.mouse.get_pos()
        Surf.blit(self.img,self.rect)



class Control(object):

    def __init__(self):
        pg.init()
        self.Screen = pg.display.set_mode((1250,850))
        self.bg = pg.image.load("interface.iter.jaune.png").convert()
        self.elements = [Dragable(name,(1150,350+90*i)) for i,name in enumerate(ELE_LIST*2)]
        self.done = False

    def event_loop(self):
        for ev in pg.event.get():
            if ev.type==pg.QUIT or (ev.type==pg.KEYDOWN and ev.key==pg.K_ESCAPE):
                self.done = True

            for Ele in self.elements:
                Ele.events(ev)

    def update(self):
        self.Screen.fill(0)
        self.Screen.blit(self.bg,(0,0))
        [Ele.update(self.Screen) for Ele in self.elements if not Ele.selected]
        [Ele.update(self.Screen) for Ele in self.elements if Ele.selected]

    def alchemize(self):
        for Eles in itertools.permutations(self.elements,2):
            if not Eles[0].selected and not Eles[1].selected:
                if Eles[0].rect.colliderect(Eles[1]):
                    self.elements.remove(Eles[0])
                    key = Eles[0].kind,Eles[1].kind
                    if key[0] != key[1]:
                        if key not in TRANS_DICT:
                            key = key[::-1]
                        Eles[1].kind = TRANS_DICT[key]
                        Eles[1].img = Eles[1].get_img()
                    break

    def main(self):
        while not self.done:
            self.event_loop()
            self.alchemize()
            self.update()
            pg.display.update()


if __name__ == "__main__":
   
    RunIt = Control()
    RunIt.main()
    pg.quit();sys.exit()
IvyMike
 
Posts: 11
Joined: Tue Apr 02, 2013 6:39 pm

Re: New dragable copy to infinite

Postby Mekire » Tue May 07, 2013 12:24 am

If you look back at the original post and at the first snippet I wrote it really should be the exact same.

You need a new class; call it PalletItem or whatever. The class must have a rect, an image, and as we are writing it a kind. It will also need a function to handle events that pertain to it. All this function will do is tell the caller to add an item of its kind to the dragable list when clicked (the new item should be created with self.selected = True). Now in your main control class you will now need to keep a list of PalletItems as well as Dragables. As the Dragables did, these PalletItems will also need to be updated and passed events every frame.

I would really like to see you try to get it working before I write it for you. Your previous code hasn't changed much since I last wrote you a snippet.

-Mek
User avatar
Mekire
 
Posts: 979
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New dragable copy to infinite

Postby IvyMike » Fri May 10, 2013 8:31 pm

I still try it's difficult ... It don't work but i think it's almost that.
How can i updated and passed events every frame in Palettitems like you say ?

Code: Select all
import pygame as pg
import sys
import itertools

ELE_LIST = ["feu","eau",]

PALETT_LIST = ["feu","eau", self.selected = True]

TRANS_DICT = {("feu" ,"eau")   : "vapeur",
              ("vapeur" ,"eau") : "alcool",
              ("alcool","feu") : "molotov"}

class Palett(object):

    def __init__(self,img,kind,loc):
       
        self.kind = kind
        self.img = [(name,(500;500+100*i)) for i,name in enumerate(ELE_LIST)]
        self.selected = False

    def events(self,ev):

        if ev.type == pg.MOUSEBUTTONDOWN and self.rect.collidepoint(ev.pos):
            if ev.button == 1:
                self.selected = True
                return "create a new dragable element"

            elif ev.type == pg.MOUSEBUTTONUP:
            if ev.button == 1:
                self.selected = False

       
    def update(self,surf):

        self.element = [Dragable
        s
       
   
                   
class Dragable(object):

    def __init__(self,kind,location):

        self.kind = kind
        self.img = self.get_img()
        self.rect = self.img.get_rect(center=location)
        self.selected = False
               
    def get_img(self):
       
        return pg.image.load(self.kind+"-icon.png").convert_alpha()

    def events(self,ev)
               :
        if ev.type == pg.MOUSEBUTTONDOWN and self.rect.collidepoint(ev.pos):
            if ev.button == 1:
                self.selected = True

        elif ev.type == pg.MOUSEBUTTONUP:
            if ev.button == 1:
                self.selected = False

    def update(self,Surf):
        if self.selected:
            self.rect.center = pg.mouse.get_pos()
        Surf.blit(self.img,self.rect)



class Control(object):

    def __init__(self):
               
        pg.init()
        self.Screen = pg.display.set_mode((1250,850))
        self.bg = pg.image.load("interface.iter.jaune.png").convert()
        self.elements = [Dragable(name,(1150,350+90*i)) for i,name in enumerate(ELE_LIST*2)]
        self.done = False

    def event_loop(self):
               
        for ev in pg.event.get():
            if ev.type==pg.QUIT or (ev.type==pg.KEYDOWN and ev.key==pg.K_ESCAPE):
                self.done = True

            for Ele in self.elements:
                Ele.events(ev)

    def update(self):
               
        self.Screen.fill(0)
        self.Screen.blit(self.bg,(0,0))
        [Ele.update(self.Screen) for Ele in self.elements if not Ele.selected]
        [Ele.update(self.Screen) for Ele in self.elements if Ele.selected]

    def alchemize(self):
               
        for Eles in itertools.permutations(self.elements,2):
            if not Eles[0].selected and not Eles[1].selected:
                if Eles[0].rect.colliderect(Eles[1]):
                    self.elements.remove(Eles[0])
                    key = Eles[0].kind,Eles[1].kind
                    if key[0] != key[1]:
                        if key not in TRANS_DICT:
                            key = key[::-1]
                        Eles[1].kind = TRANS_DICT[key]
                        Eles[1].img = Eles[1].get_img()
                    break

    def main(self):
               
        while not self.done:
            self.event_loop()
            self.alchemize()
            self.update()
            pg.display.update()


if __name__ == "__main__":
   
    RunIt = Control()
    RunIt.main()
    pg.quit();sys.exit()
IvyMike
 
Posts: 11
Joined: Tue Apr 02, 2013 6:39 pm

Re: New dragable copy to infinite

Postby Mekire » Sat May 11, 2013 3:30 am

Seems glitch free, who knows. Uses the old files and filenames. Adapt to your needs:
Code: Select all
import pygame as pg
import sys,os
import itertools

TRANS_DICT = {("fire" ,"water")   : "sulphur",
              ("fire" ,"sulphur") : "water",
              ("water","sulphur") : "fire"}

class Pallet(object):
    def __init__(self,kind,loc):
        self.kind = kind
        self.image = GFX[self.kind]
        self.rect = self.image.get_rect(center=loc)
    def events(self,ev):
        if ev.type == pg.MOUSEBUTTONDOWN and self.rect.collidepoint(ev.pos):
            if ev.button == 1:
                return self.kind
    def update(self,Surf):
        Surf.blit(self.image,self.rect)

class Dragable(object):
    def __init__(self,kind,loc):
        self.kind = kind
        self.image = GFX[self.kind]
        self.rect = self.image.get_rect(center=loc)
        self.selected = True
    def events(self,ev):
        if ev.type == pg.MOUSEBUTTONDOWN and self.rect.collidepoint(ev.pos):
            if ev.button == 1:
                self.selected = True
        elif ev.type == pg.MOUSEBUTTONUP:
            if ev.button == 1:
                self.selected = False
    def update(self,Surf):
        if self.selected:
            self.rect.center = pg.mouse.get_pos()
        Surf.blit(self.image,self.rect)

class Control(object):
    def __init__(self):
        self.Screen = pg.display.get_surface()
        self.Clock = pg.time.Clock()
        self.bg = pg.image.load("interface.jpg").convert()
        self.draggables = []
        self.base_eles = [Pallet(name,(950,50+90*i)) for i,name in enumerate(GFX)]
        self.done = False

    def event_loop(self):
        for ev in pg.event.get():
            if ev.type==pg.QUIT or (ev.type==pg.KEYDOWN and ev.key==pg.K_ESCAPE):
                self.done = True

            for BaseEle in self.base_eles:
                result = BaseEle.events(ev)
                if result:
                    self.draggables.append(Dragable(result,pg.mouse.get_pos()))

            for Ele in self.draggables:
                Ele.events(ev)

    def update(self):
        self.Screen.fill(0)
        self.Screen.blit(self.bg,(0,0))
        [Ele.update(self.Screen) for Ele in self.base_eles]
        [Ele.update(self.Screen) for Ele in self.draggables if not Ele.selected]
        [Ele.update(self.Screen) for Ele in self.draggables if Ele.selected]

    def alchemize(self):
        for Eles in itertools.permutations(self.draggables[:],2):
            if not Eles[0].selected and not Eles[1].selected:
                if Eles[0].rect.colliderect(Eles[1]):
                    self.draggables.remove(Eles[0])
                    key = Eles[0].kind,Eles[1].kind
                    if key[0] != key[1]:
                        if key not in TRANS_DICT:
                            key = key[::-1]
                        Eles[1].kind = TRANS_DICT[key]
                        Eles[1].image = GFX[Eles[1].kind]
                    break

    def main(self):
        while not self.done:
            self.event_loop()
            self.alchemize()
            self.update()
            pg.display.update()
            self.Clock.tick(60)

####
def load_images(directory):
    gfx = {}
    for image in os.listdir(directory):
        if "-icon" in image:
            myfile = os.path.join(directory,image)
            gfx[image[:-9]] = pg.image.load(myfile).convert_alpha()
    return gfx

####
if __name__ == "__main__":
    os.environ['SDL_VIDEO_CENTERED'] = '1'  #Center screen.
    pg.init()
    pg.display.set_mode((1000,640))

    GFX = load_images(".") #pass directory where images found.

    RunIt = Control()
    RunIt.main()
    pg.quit();sys.exit()

-Mek
User avatar
Mekire
 
Posts: 979
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: New dragable copy to infinite

Postby IvyMike » Sat May 11, 2013 12:05 pm

Ah ok ... i'm too far ...
Thank you so much ! Thank you.
I will training now
IvyMike
 
Posts: 11
Joined: Tue Apr 02, 2013 6:39 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 4 guests