- Player-controlled walking using arrow keys (Credit for this one goes to Per K Grok! Remember to check out his posts and his channel! Also, this code includes his third method - using arrow keys and a movable background, so if you'd like to use a mouse click to move or include a still background and a movable character, you should look into the other methods his post provides)
- Ability to run if the player holds down shift
- A clickable object
- A "label" over the object that pops up when the player is near it
(Don't mind the random panorama background I ripped off google images)
Code: Select all
image bg panorama = "images/panorama.jpg"
image j right = "images/jright1.png"
image j left = "images/jleft1.png"
#Walking and running animations:
image j walkr:
"images/jright1.png"
0.25
"images/jright2.png"
0.25
repeat
image j walkl:
"images/jleft1.png"
0.25
"images/jleft2.png"
0.25
repeat
image j runr:
"images/jright2.png"
0.2
"images/jright1.png"
0.1
"images/jrightrun1.png"
0.2
repeat
image j runl:
"images/jleft2.png"
0.2
"images/jleft1.png"
0.1
"images/jleftrun1.png"
0.2
repeat
transform transparent:
alpha 0.0
default object_encounter = False #explained later on
image button text = Text("Object", size=24, color="#ffffff", outlines=[ (2, "#000", 0, 0) ]) #label for our object
image buttontest = "images/note.png"
image buttontest text = Composite((260,260),
(0, 37), "images/note.png",
(58, -5), "button text")
screen buttonscreen(): #The reason I didn't put the image on this screen in the first place, instead separating it into a transparent imagebutton portion and visible regular image portion was because there was a slight delay in the motion of the imagebutton
hbox xpos int(bX) yalign 0.7:
imagebutton:
idle "images/note.png" at transparent
hover "images/note.png"
action Jump('objectlabel')
default dirt = 0
default dir2 = 1
#Character's position:
define jX= 440
define jY= 130
default rX= -1000.0 #background's position
default bX= -500.0 #our clickable object's position
init python:
import math
import pygame, os
from pygame.locals import *
def keys():
kdir=0
pressed_key=pygame.key.get_pressed()
if pressed_key[K_RIGHT]:
kdir=1
if pressed_key[K_LEFT]:
kdir=-1
if pressed_key[K_RIGHT] and pressed_key[K_RSHIFT] or pressed_key[K_RIGHT] and pressed_key[K_LSHIFT]:
kdir=2
if pressed_key[K_LEFT] and pressed_key[K_RSHIFT] or pressed_key[K_LEFT] and pressed_key[K_LSHIFT]:
kdir=-2
return kdir
label walklabel:
$ quick_menu = False #disables the quick menu
scene bg panorama
show screen keyCheck
show screen buttonscreen
show buttontest
jump walk
label walk:
$ object_encounter = False #explained later on
$ renpy.pause(delay=0.02, hard=True)
scene bg panorama:
xpos int(rX) #position of the background
show buttontest:
xpos int(bX) yalign 0.7
if rX>-250 and rX<290: #makes the label on the object appear when you're near it
show buttontest text
label upDate:
$ dirt = keys()
# Controls the "speed of the character" - or rather, our moving background
if dirt==-1:
$ rX += 4.0
$ bX += 4.0
elif dirt==1:
$ rX -= 4.0
$ bX -= 4.0
# Running speed
elif dirt==-2:
$ rX += 8.0
$ bX += 8.0
elif dirt==2:
$ rX -= 8.0
$ bX -= 8.0
# Where the character stops
if rX>400:
$ rX=400
$ bX=rX+500
$ dirt=0
$ dir2=1
if rX<-4000:
$ rX=-4000
$ bX= rX+500
$ dirt=0
$ dir2=-1
jump walk
screen keyCheck():
if dirt==-1:
$ dir2=-1
elif dirt==1:
$ dir2=1
if dirt==-1:
add "j walkl" xpos jX ypos jY
elif dirt==1:
add "j walkr" xpos jX ypos jY
elif dirt==-2:
add "j runl" xpos jX ypos jY
elif dirt==2:
add "j runr" xpos jX ypos jY
else:
if dir2==1:
add "j right" xpos jX ypos jY
elif dir2==-1 or dir2==-2:
add "j left" xpos jX ypos jY
label objectlabel:
if object_encounter==False: #The object_encounter thing doesn't need to be here. What is does is basically make the line act as if you clicked away from it if you click on the object again before it disappears. It just bothered me
$ object_encounter = True
"This sure is an object."
jump walk