[Solved] Creator-Defined Displayable help

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
backansi
Veteran
Posts: 224
Joined: Sun May 31, 2009 7:15 am
Location: Korea, Republic of
Contact:

[Solved] Creator-Defined Displayable help

#1 Post by backansi »

Hi. I'm writing a minigame script for my project and there is some problem.
(It's a kind of dodger game. When you are hit by enemy the game ends.)

Code: Select all

init python:
    
    class Blink(renpy.Displayable):
        
        def __init__(self):
            
            renpy.Displayable.__init__(self)
            
            self.WIDTH = 1024
            self.HEIGHT = 640
            
            self.LANE_WIDTH = 194            
            
            self.COURT_LEFT = 223
            self.COURT_RIGHT = 805
            
            # enemies
            self.EnemySprite = [] # enemy sprite list.
             
            for i in range(0, 5):
                t = Text("Text for test", size = 36) #Transform("RideEnemy" + str(i), zoom = 0.6)
                self.EnemySprite.append(t)
                
            # need to calculate when the enemy should be added.
            self.AddNewEnemyRate = 30
            self.EnemyCount = 0
             
            self.Enemies = []
            
            self.cspeed = 0
            self.oldSt = None
            
        def visit(self):
             
            sprites = self.EnemySprite
             
            return sprites
            
        def render(self, width, height, st, at):
             
            r = renpy.Render(width, height)
             
            if not self.oldSt:
                self.oldSt = st
             
            duration = st-self.oldSt
             
            self.oldSt = st
             
            if st <= 3:
                self.cspeed = 100
                 
            elif st <= 6:
                self.cspeed = 200
                 
            elif st <= 9:
                self.cspeed = 400
                 
            elif st <= 12:
                self.cspeed = 500
             
            elif st >= 15:
                self.cscpeed = 0
                self.AddNewEnemyRate = 0
                
            speed = duration * self.cspeed
            
            ### Enemies #######################################
            # add new enemies
            self.EnemyCount += 1
             
            if self.EnemyCount == self.AddNewEnemyRate:
                 
                self.EnemyCount = 0
                # Randomize enemy sprite
                i = renpy.random.randint(1, 4)
                enemy = self.EnemySprite[i] 
                 
                # randomize enemy x position
                EnemyX = self.COURT_LEFT + renpy.random.randint(0, 2) * self.LANE_WIDTH
                 
                # Enemy's starting y point.
                EnemyY = -300
                 
                NewEnemy = {'img': enemy, 'x': EnemyX, 'y': EnemyY}
                 
                self.Enemies.append(NewEnemy)
             
            for e in self.Enemies:
 
                # Enemy goes down.
                e['y'] = e['y'] + speed
                 
                enemy_render = renpy.render(e['img'], width, height, st, at)
                     
                # Delete if enemy is out of screen
                ###################
                # when do this the last enemy sprite blinks.
                if e['y'] > self.HEIGHT:
                    self.Enemies.remove(e)
                    
                # Draw enemy
                r.blit(enemy_render, (e['x'], e['y']))
                
            renpy.redraw(self, 0)
            return r
init:

    screen riding:
        add Blink()
        
label start:
    
    call screen riding
    
    #if _return == True:
        #":D"
    #if _return == False:
        #"T_T"
    
    return
    
If you copy and paste the script above, you can see when the enemy which goes out of the screen is removed, the last enemy sprite on the screen blinks.

Code: Select all

                 ###################
                # when do this the last enemy sprite one screen blinks.
                if e['y'] > self.HEIGHT:
                    self.Enemies.remove(e)
I know the problem is because of these lines but I have no idea how could I solve it.
Moreover it seems a bit slow at first when the sprites are drawn.

I would be grateful for any advices on how could I find a solution.
Thanks in advance.
Last edited by backansi on Sat Jul 20, 2013 4:23 am, edited 1 time in total.

KimiYoriBaka
Miko-Class Veteran
Posts: 636
Joined: Thu May 14, 2009 8:15 pm
Projects: Castle of Arhannia
Contact:

Re: Creator-Defined Displayable help

#2 Post by KimiYoriBaka »

that sounds like lag.

are you making sure that the images are either predicted or cached beforehand?

User avatar
backansi
Veteran
Posts: 224
Joined: Sun May 31, 2009 7:15 am
Location: Korea, Republic of
Contact:

Re: Creator-Defined Displayable help

#3 Post by backansi »

I'm not sure but I think I did...
I changed the script a bit so when new enemy image is drawn, it is appended to self.EnemySprite list and the visit method returns it.
Is it wrong? or Is there anything to do for the images to be predicted or cached?
I don't know about caching or predicting at all. @_@

User avatar
backansi
Veteran
Posts: 224
Joined: Sun May 31, 2009 7:15 am
Location: Korea, Republic of
Contact:

Re: Creator-Defined Displayable help

#4 Post by backansi »

Okay I got it now. :D

I added a new key 'out' to each enemy dictionary, and lines that prevent to draw the enemy when its key 'out' is 1.

And about the lag, when I used renpy.predit and set config.image_cache_size to 32 as mentioned in following thread the problem was solved, though loading process(?) was a bit slow for my ATL images are too big and animated transform.
http://lemmasoft.renai.us/forums/viewto ... tl+cashing

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Majestic-12 [Bot]