Player Movement Using Arrow Keys

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
doctorcello
Newbie
Posts: 4
Joined: Mon Sep 22, 2014 4:07 pm
Contact:

Player Movement Using Arrow Keys

#1 Post by doctorcello »

I've been trying to figure out how to make a displayable that's similar to the way Cherry Tree High Comedy Club plays during its adventure game-y sequences (see here as an example). Right now it's looking like this instead:

Image

So right now the problems are:

- Character image refuses to update its position
- Character can only go about 200 pixels and then it snaps back to its original position and sorta jiggles a little bit (not really clear in the .gif, but trust me it happens)
- Character needs to stand in the black outlined shape above (this can be solved probably by defining the character image's anchor, but I'm not really sure how to do that).

Can anyone help me figure out how to fix these things? Here's my code that I cobbled together with other code that I've seen:

Code: Select all

init:
    
    image bg example = "bg/bgtestA.png"
    
    python:
        
        class TestDisplayable(renpy.Displayable):
            
            def __init__(self):
                
                renpy.Displayable.__init__(self)
                
                # Displayables that we will use
                self.player = Image ("char/playerexample.png")
                
                # the sizes of images maybe?
                self.PLAYER_WIDTH = 140
                self.PLAYER_HEIGHT = 319
                self.SCENE_LEFT = 248
                self.SCENE_RIGHT = 1876
                
                self.player_speed = 3
                
                self.move_left = False
                self.move_right = False
                
                # Position of player
                self.px = 250
                self.py = 840
                
            def visit(self):
                return [ self.player ]
                
            # OK, time to experiment a bit with renders
            def render(self, width, height, st, at):
                
                # The Render object we'll be drawing into.
                r = renpy.Render(width, height)
                
                player = renpy.displayable('char/playerexample.png') 
                
                player_r = renpy.render(player, 1920, 1080, 0, 0)
                
                r.blit(player_r, (self.px, self.py))
                
                # Determines the speed 
                if self.move_left:
                    self.px -= self.player_speed
                elif self.move_right:
                    self.px += self.player_speed
                
                # Ask that we be re-rendered ASAP, so we can show the next frame.
                renpy.redraw(self, 0)
            
                return r
                
            # Handles events
            def event(self, ev, x, y, st):
                
                import pygame
                
                # Keyboard controls
                if ev.type == pygame.KEYDOWN:
                    if ev.key == pygame.K_LEFT:
                        self.move_left = True
                        self.move_right = False
                    elif ev.key == pygame.K_RIGHT:
                        self.move_right = True
                        self.move_left = False
                elif ev.type == pygame.KEYUP:
                    if ev.key == pygame.K_LEFT:
                        self.move_left = False
                    elif ev.key == pygame.K_RIGHT:
                        self.move_right = False
                else:
                    raise renpy.IgnoreEvent()
                
                # Set the position of the player.
                x = max(x, self.SCENE_RIGHT)
                x = min(x, self.SCENE_LEFT)
                self.px = x
                
                
label adventure_test:
    
    window hide        
            
    scene bg example
    
    window show
    
    "Hey, are we even doing anything?"
    
    window hide
    
    pause
    
    python:
        ui.add(TestDisplayable())
        ui.interact(suppress_overlay=True, suppress_underlay=True)
        
    
    return

User avatar
feathersnake
Regular
Posts: 104
Joined: Fri Apr 13, 2012 8:05 pm
Completed: A Rose by Any Other Name
Projects: Color, You've Got Mail!
Organization: Olfix Productions
Tumblr: olfix
Deviantart: olfix
Contact:

Re: Player Movement Using Arrow Keys

#2 Post by feathersnake »

Not sure if you've already taken a look at this thread, but maybe Elmiwisa's code here will help a little?
http://lemmasoft.renai.us/forums/viewto ... 63#p290803
Olfix Productions:
Tumblr | itch.io
Complete Projects:
A Rose by Any Other Name
In-progress:
Color | You've Got Mail!

doctorcello
Newbie
Posts: 4
Joined: Mon Sep 22, 2014 4:07 pm
Contact:

Re: Player Movement Using Arrow Keys

#3 Post by doctorcello »

feathersnake wrote:Not sure if you've already taken a look at this thread, but maybe Elmiwisa's code here will help a little?
http://lemmasoft.renai.us/forums/viewto ... 63#p290803
Ah, I haven't yet. Thanks, I'll give it a shot!

User avatar
PyTom
Ren'Py Creator
Posts: 16096
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: Player Movement Using Arrow Keys

#4 Post by PyTom »

A couple of notes:

The framerate may vary, so you should always use speed * (st - old_st) to figure out how far a character has moved.

The max/min checks should be in render, not event.

You should do the blit after the move - otherwise, you'll introduce a frame of lag.
Supporting creators since 2004
(When was the last time you backed up your game?)
"Do good work." - Virgil Ivan "Gus" Grissom
Software > Drama • https://www.patreon.com/renpytom

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Google [Bot]