RectNode aufleuchten lassen?

Hier werden alle anderen GUI-Toolkits sowie Spezial-Toolkits wie Spiele-Engines behandelt.
Antworten
meischder
User
Beiträge: 5
Registriert: Mittwoch 23. Mai 2012, 18:01

Hallo,

Ich habe ein Feld mit RectNodes (libavg) und würde diese gerne für eine gewisse Zeit aufleuchten lassen, sprich in einer anderen Farbe darstellen. Außerdem würde ich diese Farbänderung gerne mit einem Fade-In/Out realisieren, habe aber keine Ahnung, wie ich das realisieren soll und google war da auch nicht sonderlich erfolgreich.
Mein Code ist momentan folgender:

Code: Alles auswählen

from libavg import *
from PIL. ExifTags import TAGS
from PIL import *
import sys
import os
from Xlib import *
import pygame


class design(object):
    def __init__(self):
        player = avg.Player.get()
        pygame.init()
        screenwidth = pygame.display.list_modes()[0][0]
        screenheight = pygame.display.list_modes()[0][1]
        print(screenwidth)
        print(screenheight)
        self.x = 1066
        self.y = 600
        #self.x = screenwidth
        #self.y = screenheight
        self.wire = self.x/16
        self.heire = self.y/9
        self.bgcolorb = "A4A4A4"
        #"BE4F4F"
        self.bgcolorn = "313F89"
        self.outlc = "000000"
        canvas = player.createMainCanvas(size=(self.x,self.y))
        self.RootNode = canvas.getRootNode()
        self.left = avg.DivNode(size=(self.x,self.y), pos=(0,0), crop=False, elementoutlinecolor="123456", parent=self.RootNode)
        
        bg = avg.ImageNode(pos=(0,0), size=(self.x,self.y), href="./img1.jpg", parent=self.RootNode)

                
      
        #auswahl-rects
        r1 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(0,0), color=self.outlc, fillcolor=self.bgcolorn, fillopacity=0, parent=self.RootNode)
        r2 = avg.RectNode(size=(2*self.wire,3*self.heire), pos=(0,3*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r3 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(0,7*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r4 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(2*self.wire,self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r5 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(2*self.wire,6*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r6 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(4*self.wire,2*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r7 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(4*self.wire,5*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r8 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(5*self.wire,0*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r9 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(5*self.wire,7*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r10 = avg.RectNode(size=(2*self.wire,3*self.heire), pos=(6*self.wire,3*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r11 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(7*self.wire,0), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r12 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(7*self.wire,7*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r13 = avg.RectNode(size=(2*self.wire,3*self.heire), pos=(8*self.wire,3*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r14 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(9*self.wire,0), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r15 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(9*self.wire,7*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r16 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(10*self.wire,2*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r17 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(10*self.wire,5*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r18 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(12*self.wire,self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r19 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(12*self.wire,6*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r20 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(14*self.wire,0), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r21 = avg.RectNode(size=(2*self.wire,3*self.heire), pos=(14*self.wire,3*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        r22 = avg.RectNode(size=(2*self.wire,2*self.heire), pos=(14*self.wire,7*self.heire), opacity=1, color=self.outlc, fillcolor=self.bgcolorn, fillopacity=1, parent=self.RootNode)
        
        
        #bg-rects, verschwinden/fallen am ende des levels (runter)/werden ausgeblendet...
        f1 = avg.RectNode(size=(2*self.wire,self.heire), pos=(0,2*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=50, parent=self.RootNode)
        f2 = avg.RectNode(size=(2*self.wire,self.heire), pos=(0,6*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f3 = avg.RectNode(size=(2*self.wire,self.heire), pos=(2*self.wire,0), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f4 = avg.RectNode(size=(self.wire,2*self.heire), pos=(4*self.wire,0), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f5 = avg.RectNode(size=(2*self.wire,self.heire), pos=(2*self.wire,3*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f6 = avg.RectNode(size=(2*self.wire,self.heire), pos=(2*self.wire,4*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f7 = avg.RectNode(size=(2*self.wire,self.heire), pos=(2*self.wire,5*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f8 = avg.RectNode(size=(2*self.wire,self.heire), pos=(4*self.wire,4*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f9 = avg.RectNode(size=(2*self.wire,self.heire), pos=(2*self.wire,8*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f10 = avg.RectNode(size=(self.wire,2*self.heire), pos=(4*self.wire,7*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f11 = avg.RectNode(size=(2*self.wire,self.heire), pos=(6*self.wire,2*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f12 = avg.RectNode(size=(2*self.wire,self.heire), pos=(8*self.wire,2*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f13 = avg.RectNode(size=(2*self.wire,self.heire), pos=(6*self.wire,6*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f14 = avg.RectNode(size=(2*self.wire,self.heire), pos=(8*self.wire,6*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f15 = avg.RectNode(size=(self.wire,2*self.heire), pos=(11*self.wire,0), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f16 = avg.RectNode(size=(2*self.wire,self.heire), pos=(12*self.wire,0), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f17 = avg.RectNode(size=(2*self.wire,self.heire), pos=(10*self.wire,4*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f18 = avg.RectNode(size=(2*self.wire,self.heire), pos=(12*self.wire,3*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f19 = avg.RectNode(size=(2*self.wire,self.heire), pos=(12*self.wire,4*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f20 = avg.RectNode(size=(2*self.wire,self.heire), pos=(12*self.wire,5*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f21 = avg.RectNode(size=(self.wire,2*self.heire), pos=(11*self.wire,7*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f22 = avg.RectNode(size=(2*self.wire,self.heire), pos=(12*self.wire,8*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f23 = avg.RectNode(size=(2*self.wire,self.heire), pos=(14*self.wire,2*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)
        f24 = avg.RectNode(size=(2*self.wire,self.heire), pos=(14*self.wire,6*self.heire), color=self.outlc, fillcolor=self.bgcolorb, fillopacity=1, parent=self.RootNode)

        
        player.play()




design()
Ich habe oben schon mal alles reingeschrieben, was ich schon in habe.

Hat jmd. Ideen?


MfG
meischder
Zuletzt geändert von Hyperion am Donnerstag 21. Juni 2012, 13:32, insgesamt 1-mal geändert.
Grund: Code in Python-Code Tags gesetzt.
EyDu
User
Beiträge: 4881
Registriert: Donnerstag 20. Juli 2006, 23:06
Wohnort: Berlin

Hallo.

Deine Code hat gleich eine Menge Probleme: Ganz am Anfang sind dort die *-Importe, welche dir den ganzen Namensraum zumüllen. Man kann in deinem Code nur raten, ob Namen nun aus libavg, PIL pder Xlib kommen. Da vergeht mir bereits am Anfang die Lust im Detail auf den Code zu schauen, da ich erstmal alle Namen überprüfen müsste.

Weiter sieht es ein wenig danach aus, als würdest du die Klasse als Funktion verwenden. Die Attribute werden nirgends verwendet und einfach nur eine Instaz erstellt. Eine einfache Funktion tut es auch. Außerdem sollte bei so langen Funktionen in sinnvolle Untereinheiten geteilt werden. Funktionen die länger sind als eine Bildschirmseite hoch sind bereits relativ anstregend zu lesen.

Als eine Grundregel solltest du dir merken: Wenn du anfängst Namen mit Nummern zu versehen, dann machst du einen Fehler. Dann möchtest du eine geeignete Datenstruktur verwenden wie Listen, Tupel oder ein Dictionary.

Und noch eine weitere Regel: Wenn du anfängst Code zu kopieren, dann machst du einen noch größeren Fehler. Das heißt jetzt nicht, dass du den Code abschreiben sollst, sondern dass gemeinsame Teile zusammengefasst und verschiedene Teile ausgelagert werden. Die ganze __init__-Funktion ist doch eine einzige große Kopie und die Zeilen unterscheiden sich nur an sehr wenigen stellen. Die Unterschiede musst du in eine entsprechnde Datenstruktur auslagern und kannst dann alle Elemente in einer Schleife erzeugen. Das reduziert die Menge deines Codes auf einen Bruchteil vom aktuellen Stand und erhöht die Wartbarkeit.

Und noch eine Lektion: Wenn du Zahlen in deinen Code schreibst, welche alle irgendwie von einander abhängen, dann solltest du diese in Konstanten auslagern und voneinander herleiten. Du weißt du mittlerweile sicher selber nicht mehr für was die vier, die sechs oder die fünf in deinem Code stehen. Wenn du jetzt irgendwo an einer Stelle etwas ändern willst, zum Beispiel eine fünf in eine sieben umwandeln, dann hat das ungeahnte folgen für dein Programm. Du musst im Prinzip den ganzen Code durchgehen und per Hand alle Abhängigkeiten auflösen. Das wird dir sicher nicht gelingen und dutzende von Fehlern produzieren.

Weiter ist es wichtig, wenn du Hilfe haben möchtest, dass du deinen Code auf ein minimales LAUFFÄHIGES Beispiel zusammenschrumpfst. Dein Prolem lässt sich sicher auch mit einem einzigen RectNode demonstrieren. Häufig lösen sich Probleme auch von selbst, wenn man erstmal ein minimales Beispiel selber schreibt, dann stoplert man oft über die Lösung. Für die Motivation der Helfenden ist es allerdings nicht sehr förderlich, wenn sie sich erst duch dutzende unnütze Zeilen von Code Quälen müssen.

Und vielleicht noch als Randbemerkung: Neben den normalen Code-Tags, welche du ja bereits verwendet hast, gibt es noch Code-Tags für Python. Dann hat man noch ein wenig Syntaxhighlighting.

Sebastian
Das Leben ist wie ein Tennisball.
Antworten