help make beautiful code

Discuss how to use the Ren'Py engine to create visual novels and story-based games. New releases are announced in this section.
Forum rules
This is the right place for Ren'Py help. Please ask one question per thread, use a descriptive subject like 'NotFound error in option.rpy' , and include all the relevant information - especially any relevant code and traceback messages. Use the code tag to format scripts.
Post Reply
Message
Author
User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

help make beautiful code

#1 Post by Andredron » Mon May 07, 2018 12:34 am

Hello everyone, tell me how I can make the code more beautiful in mini-game 3 in 1 row

https://pp.userapi.com/c841630/v8416303 ... GAZMko.jpg image
https://m.vk.com/doc28338738_464355483? ... be744ba950 download project
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

User avatar
Imperf3kt
Lemma-Class Veteran
Posts: 3636
Joined: Mon Dec 14, 2015 5:05 am
Location: Your monitor
Contact:

Re: help make beautiful code

#2 Post by Imperf3kt » Mon May 07, 2018 1:38 am

I'm curious, is there any reason you exported your images to vp8 and renamed them ".w"?

Also, is this code under any license? May others use it in their game?
Warning: May contain trace amounts of gratuitous plot.
pro·gram·mer (noun) An organism capable of converting caffeine into code.

Current project: GGD Mentor
Free Android GUI - Updated occasionally
Twitter
Imperf3kt Blackjack - a WIP blackjack game for Android made using Ren'Py

User avatar
PyTom
Ren'Py Creator
Posts: 15893
Joined: Mon Feb 02, 2004 10:58 am
Completed: Moonlight Walks
Projects: Ren'Py
IRC Nick: renpytom
Github: renpytom
itch: renpytom
Location: Kings Park, NY
Contact:

Re: help make beautiful code

#3 Post by PyTom » Mon May 07, 2018 9:38 am

The #1 thing that makes for beautiful programs is good naming. I tried to take a look at your code, but kind of gave up, because of this line:

Code: Select all

        global cl1,cl2,cl3,cl4,cl5,cl6,cl7,kricol,krix,krix0,pointka,ktmr,clp1,clp2,clp3,clp4,clp5,clp6,clp7
So, I don't know what these variables mean. Short names should be reserved for local variables - things like iterators, scratch variables, etc. Everything else should have a good name, that explains what it does. I'm also generally leery of variable names with consecutive numbers in it - generally, that means a list should be used as well.

If you'd like help, can you explain what should be in these globals?
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
"Silly and fun things are important." - Elon Musk
Software > Drama • https://www.patreon.com/renpytom

User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Re: help make beautiful code

#4 Post by Andredron » Mon May 07, 2018 5:36 pm

kry.rpy

Code: Select all

#cl1, cl2, cl3, cl4, cl5, cl6, cl7 are the horizontal rows
#krix- selected stone
#krix0- 2 stone you want to change

#kricol - it seems that something superfluous
#pointka - number of points
#ktmr - timer
#clp1, clp2, clp3, clp4, clp5, clp6, clp7- this is for animation
#mcnn - Style

# image any one can write down, simply changing the value here
# imagebutton idle ("k" + str (globals () ["cl" + str (z)] [i]) + ". w")
# on
# imagebutton idle ("k" + str (globals () ["cl" + str (z)] [i]) + ". png")


image kg="kg.w"   #background image
init python:
    import time
    style.mcnn=Style(style.default)
    style.mcnn.set_parent(style.button)
    style.mcnn.color="#beddff"
    style.mcnn.size=70
    style.mcnn.outlines=[(2,"#6e6e6e",0,0)]
    def krib():
        global cl1,cl2,cl3,cl4,cl5,cl6,cl7,kricol,krix,krix0,pointka,ktmr,clp1,clp2,clp3,clp4,clp5,clp6,clp7
        for i in range(0,9):
            if cl1[i]>0:
                clzz=1
                if i<7:
                    if cl1[i]==cl1[i+1]==cl1[i+2]:
                        pointka+=(cl1[i]+cl1[i+1]+cl1[i+2])*2
                        if i<6 and cl1[i]==cl1[i+3]:
                            if i<5 and cl1[i]==cl1[i+4]:
                                pointka+=cl1[i+4]*4
                                ktmr+=4
                                cl1[i+4]=0
                            pointka+=cl1[i+3]*3
                            ktmr+=2
                            cl1[i+3]=0
                        clzz,cl1[i+1],cl1[i+2]=0,0,0
                if cl1[i]==cl2[i]==cl3[i]:
                    pointka+=(cl1[i]+cl2[i]+cl3[i])*2
                    if cl1[i]==cl4[i]:
                        if cl1[i]==cl5[i]:
                            pointka+=cl5[i]*4
                            ktmr+=4
                            cl5[i]=0
                        pointka+=cl4[i]*3
                        ktmr+=2
                        cl4[i]=0
                    clzz,cl2[i],cl3[i]=0,0,0
                if clzz==0:
                    cl1[i]=0
        renpy.pause(0.05)
        for i in range(0,9):
            if cl2[i]>0:
                clzz=1
                if i<7:
                    if cl2[i]==cl2[i+1]==cl2[i+2]:
                        pointka+=(cl2[i]+cl2[i+1]+cl2[i+2])*2
                        if i<6 and cl2[i]==cl2[i+3]:
                            if i<5 and cl2[i]==cl2[i+4]:
                                pointka+=cl2[i+4]*4
                                ktmr+=4
                                cl2[i+4]=0
                            pointka+=cl2[i+3]*3
                            ktmr+=2
                            cl2[i+3]=0
                        clzz,cl2[i+1],cl2[i+2]=0,0,0
                if cl2[i]==cl3[i]==cl4[i]:
                    pointka+=(cl2[i]+cl3[i]+cl4[i])*2
                    if cl2[i]==cl5[i]:
                        if cl2[i]==cl6[i]:
                            pointka+=cl6[i]*4
                            ktmr+=4
                            cl6[i]=0
                        pointka+=cl5[i]*3
                        ktmr+=2
                        cl5[i]=0
                    clzz,cl3[i],cl4[i]=0,0,0
                if clzz==0:
                    cl2[i]=0
        renpy.pause(0.05)
        for i in range(0,9):
            if cl3[i]>0:
                clzz=1
                if i<7:
                    if cl3[i]==cl3[i+1]==cl3[i+2]:
                        pointka=(cl3[i]+cl3[i+1]+cl3[i+2])*2
                        if i<6 and cl3[i]==cl3[i+3]:
                            if i<5 and cl3[i]==cl3[i+4]:
                                pointka+=cl3[i+4]*4
                                ktmr+=4
                                cl3[i+4]=0
                            pointka+=cl3[i+3]*3
                            ktmr+=2
                            cl3[i+3]=0
                        clzz,cl3[i+1],cl3[i+2]=0,0,0
                if cl3[i]==cl4[i]==cl5[i]:
                    pointka+=(cl3[i]+cl4[i]+cl5[i])*2
                    if cl3[i]==cl6[i]:
                        pointka+=cl6[i]*3
                        ktmr+=2
                        cl6[i]=0
                    clzz,cl4[i],cl5[i]=0,0,0
                if clzz==0:
                    cl3[i]=0
        renpy.pause(0.05)
        for i in range(0,9):
            if cl4[i]>0:
                clzz=1
                if i<7:
                    if cl4[i]==cl4[i+1]==cl4[i+2]:
                        pointka+=(cl4[i]+cl4[i+1]+cl4[i+2])*2
                        if i<6 and cl4[i]==cl4[i+3]:
                            if i<5 and cl4[i]==cl4[i+4]:
                                pointka+=cl4[i+4]*4
                                ktmr+=4
                                cl4[i+4]=0
                            pointka+=cl4[i+3]*3
                            ktmr+=2
                            cl4[i+3]=0
                        clzz,cl4[i+1],cl4[i+2]=0,0,0
                if cl4[i]==cl5[i]==cl6[i]:
                    pointka+=(cl4[i]+cl5[i]+cl6[i])*2
                    clzz,cl5[i],cl6[i]=0,0,0
                if clzz==0:
                    cl4[i]=0
        renpy.pause(0.05)
        for i in range(0,7):
            if cl5[i]>0:
                if cl5[i]==cl5[i+1]==cl5[i+2]:
                    pointka+=(cl5[i]+cl5[i+1]+cl5[i+2])*2
                    if i<6 and cl5[i]==cl5[i+3]:
                        if i<5 and cl5[i]==cl5[i+4]:
                            pointka+=cl5[i+4]*4
                            ktmr+=4
                            cl5[i+4]=0
                        pointka+=cl5[i+3]*3
                        ktmr+=2
                        cl5[i+3]=0
                    cl5[i],cl5[i+1],cl5[i+2]=0,0,0
        renpy.pause(0.05)
        for i in range(0,7):
            if cl6[i]>0:
                if cl6[i]==cl6[i+1]==cl6[i+2]:
                    pointka+=(cl6[i]+cl6[i+1]+cl6[i+2])*2
                    if i<6 and cl6[i]==cl6[i+3]:
                        if i<5 and cl6[i]==cl6[i+4]:
                            pointka+=cl6[i+4]*4
                            ktmr+=4
                            cl6[i+4]=0
                        pointka+=cl6[i+3]*3
                        ktmr+=2
                        cl6[i+3]=0
                    cl6[i],cl6[i+1],cl6[i+2]=0,0,0
        for i in range(0,9):
            if cl6[i]==0:
                clp5[i]-=32
                if cl5[i]>0:
                    cl6[i]=cl5[i]
                    cl5[i]=0
                elif cl4[i]>0:
                    cl6[i]=cl4[i]
                    cl4[i]=0
                elif cl3[i]>0:
                    cl6[i]=cl3[i]
                    cl3[i]=0
                elif cl2[i]>0:
                    cl6[i]=cl2[i]
                    cl2[i]=0
                elif cl1[i]>0:
                    cl6[i]=cl1[i]
                    cl1[i]=0
                else:
                    cl6[i]=renpy.random.randint(1,6)
                for q in range(4):
                    clp5[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        for i in range(0,9):
            if cl5[i]==0:
                clp5[i]-=32
                if cl4[i]>0:
                    cl5[i]=cl4[i]
                    cl4[i]=0
                elif cl3[i]>0:
                    cl5[i]=cl3[i]
                    cl3[i]=0
                elif cl2[i]>0:
                    cl5[i]=cl2[i]
                    cl2[i]=0
                elif cl1[i]>0:
                    cl5[i]=cl1[i]
                    cl1[i]=0
                else:
                    cl5[i]=renpy.random.randint(1,6)
                for q in range(4):
                    clp5[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        for i in range(0,9):
            if cl4[i]==0:
                clp4[i]-=32
                if cl3[i]>0:
                    cl4[i]=cl3[i]
                    cl3[i]=0
                elif cl2[i]>0:
                    cl4[i]=cl2[i]
                    cl2[i]=0
                elif cl1[i]>0:
                    cl4[i]=cl1[i]
                    cl1[i]=0
                else:
                    cl4[i]=renpy.random.randint(1,6)
                for q in range(4):
                    clp4[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        for i in range(0,9):
            if cl3[i]==0:
                clp3[i]-=32
                if cl2[i]>0:
                    cl3[i]=cl2[i]
                    cl2[i]=0
                elif cl1[i]>0:
                    cl3[i]=cl1[i]
                    cl1[i]=0
                else:
                    cl3[i]=renpy.random.randint(1,6)
                for q in range(4):
                    clp3[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        for i in range(0,9):
            if cl2[i]==0:
                clp2[i]-=32
                if cl1[i]>0:
                    cl2[i]=cl1[i]
                    cl1[i]=0
                else:
                    cl2[i]=renpy.random.randint(1,6)
                for q in range(4):
                    clp2[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        for i in range(0,9):
            if cl1[i]==0:
                clp1[i]-=32
                cl1[i]=renpy.random.randint(1,6)
                for w in range(4):
                    clp1[i]+=8
                    renpy.pause(.0000001)
                ktmr+=0.1
        return
screen krix:
    text"[pointk]"style"mcnn"
    text str(int(ktmr))style"mcnn"xalign.99
    timer 1 repeat True action If(ktmr>0,true=SetVariable('ktmr',ktmr-1),false=Return())
    for z in range(1,7):
        for i in range(0,9):
            imagebutton idle("k"+str(globals()["cl"+str(z)][i])+".w")xpos(i*80+260)ypos(z*80+20)action[SetVariable('krix',i),SetVariable('kriy',z),ui.callsinnewcontext("krich")]
screen krixx:
    text"[pointk]"style"mcnn"
    text str(int(ktmr)) style"mcnn"xalign.99
    timer 1 repeat True action If(ktmr>0,true=SetVariable('ktmr',ktmr-1),false=Return())
    for z in range(1,7):
        for i in range(0,9):
            image("k"+str(globals()["cl"+str(z)][i])+".w")xpos(i*80+260)ypos(z*80+20+globals()["clp"+str(z)][i])
label krich:
    show screen krixx
    if krix>=0:
        if krix in(krix0+1,krix0-1)and kriy==kriy0:
            pass
        elif kriy in(kriy0+1,kriy0-1)and krix==krix0:
            pass
        else:
            jump krich3
        if krix0>=0:
            $krixxx=globals()["cl"+str(kriy)][krix]
            $globals()["cl"+str(kriy)][krix]=globals()["cl"+str(kriy0)][krix0]
            $globals()["cl"+str(kriy0)][krix0]=krixxx
            label krich2:
                $krib()
                if pointka>0:
                    $pointloop+=1
                    $pointk+=pointka
                    $pointka=0
                    jump krich2
                if pointloop==0:
                    $krixxx=globals()["cl"+str(kriy0)][krix0]
                    $globals()["cl"+str(kriy0)][krix0]=globals()["cl"+str(kriy)][krix]
                    $globals()["cl"+str(kriy)][krix]=krixxx
                $pointloop=0
                $krix=-1
                $krix0=-1
        else:
            label krich3:
                $krix0=krix
                $kriy0=kriy
    return
label krix:
    scene kg
    python:
        cl0,cl1,cl2,cl3,cl4,cl5,cl6,cl7,krix,kriy,kris,krin,kriw,krie,kricol,krix,krix0,kriy,kriy0,pointk,pointloop,ktmr,pointka=[],[],[],[],[],[],[],[],0,0,0,0,0,0,0,-1,-1,-1,-1,0,0,120,0
        for z in range(1,7):
            for i in range(0,9):
                globals()["cl"+str(z)].append(renpy.random.randint(1,6))
        clp0,clp1,clp2,clp3,clp4,clp5,clp6,clp7=[],[],[],[],[],[],[],[]
        for z in range(1,7):
            for i in range(0,9):
                globals()["clp"+str(z)].append(0)
    call screen krix
    return
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Re: help make beautiful code

#5 Post by Andredron » Mon May 07, 2018 5:42 pm

Imperf3kt wrote:
Mon May 07, 2018 1:38 am
I'm curious, is there any reason you exported your images to vp8 and renamed them ".w"?

Also, is this code under any license? May others use it in their game?
you can generally without an extension, renpy reads and recognizes any image format from the file itself.

image any one can write down, simply changing the value here

imagebutton idle ("k" + str (globals () ["cl" + str (z)] ) + ". w")

on

imagebutton idle ("k" + str (globals () ["cl" + str (z)] ) + ". png")

The license is free, but I will be glad if I specify the author - = a = Firestorm (https://vk.com/a_firestorm)
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

User avatar
Andredron
Miko-Class Veteran
Posts: 535
Joined: Thu Dec 28, 2017 2:37 pm
Completed: Kimi ga nozomu renpy-port(demo), Albatross Koukairoku(demo)
Projects: Sisters ~Natsu no Saigo no Hi~(renpy-port)
Location: Russia
Contact:

Re: help make beautiful code

#6 Post by Andredron » Mon May 07, 2018 5:45 pm

PyTom wrote:
Mon May 07, 2018 9:38 am
The #1 thing that makes for beautiful programs is good naming. I tried to take a look at your code, but kind of gave up, because of this line:

Code: Select all

        global cl1,cl2,cl3,cl4,cl5,cl6,cl7,kricol,krix,krix0,pointka,ktmr,clp1,clp2,clp3,clp4,clp5,clp6,clp7
So, I don't know what these variables mean. Short names should be reserved for local variables - things like iterators, scratch variables, etc. Everything else should have a good name, that explains what it does. I'm also generally leery of variable names with consecutive numbers in it - generally, that means a list should be used as well.

If you'd like help, can you explain what should be in these globals?
I wrote
I'm writing a Renpy textbook (in Russian). https://disk.yandex.ru/i/httNEajU7iFWHA (all information is out of date) Update 22.06.18

Help me to register in QQ International

Honest Critique

Post Reply

Who is online

Users browsing this forum: No registered users