Python Forum
Need help making code more efficient
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need help making code more efficient
#1
I am working on processing version 3.3.6 and I am having trouble simplifying some code. My goal is to have text that types out across the screen like in some video games. I have tried using lists but haven't found a way to get that to work. I would like to retain the exact same effect with more efficient code. My code is as follows:
story = 0
def setup():
    size(400, 400)
    
def draw():
    #slower framerate to exaggerate what I am trying to do, and make it easier for you guys
    frameRate(10)
    #I know resetting the background isn't necessary for this example but it is for a larger project this is a part of
    background(0)
    textSize(32)
    global story
    #beggining of the ineffecient code that I need help with
    story = story + 1
    if story > 1 :
        text("t", 10, 30)
    if story > 2 :
        text("th", 10, 30)
    if story > 3 :
        text("thi", 10, 30)
    if story > 4 :
        text("this", 10, 30)
    if story > 5 :
        text("this ", 10, 30)
    if story > 6 :
        text("this i", 10, 30)
    if story > 7 :
        text("this is", 10, 30)
    if story > 8 :
        text("this is ", 10, 30)
    if story > 9 :
        text("this is a", 10, 30)
    if story > 10 :
        text("this is a ", 10, 30)
    if story > 11 :
        text("this is a t", 10, 30)
    if story > 12 :
        text("this is a te", 10, 30)
    if story > 13 :
        text("this is a tes", 10, 30)
    if story > 14 :
        text("this is a test", 10, 30)
    if story > 15 :
        text("this is a test.", 10, 30)
    #end of the ineffecient code that I need help with
thanks to all who help out!
Reply
#2
You want to use a loop:
import time

def display_this(my_string):
    cur_str = []
    for character in my_string:
        cur_str.append(character)
        text(cur_str, 10, 30)
        time.sleep(.5)

display_this('this is a test.')
Reply
#3
Larz60+, I have tried what you have suggested and have received an error. I type in the following:
def setup():
    size(400, 400)
    
def draw():
    import time
 
    def display_this(my_string):
        cur_str = []
        for character in my_string:
            cur_str.append(character)
            text(cur_str, 10, 30)
            time.sleep(.5)
    
    display_this('this is a test.')
and recieve
Error:
processing.app.SketchException: TypeError: a float is required at jycessing.mode.run.SketchRunner.convertPythonSketchError(SketchRunner.java:252) at jycessing.mode.run.SketchRunner.lambda$2(SketchRunner.java:125) at java.lang.Thread.run(Thread.java:748)
Reply
#4
the function should not be embedded inside of draw, but on it's own.
also, what I show will not work verbatim in your code, you will need to make it fit.
import statement should be at start of program.
Reply
#5
Thanks, I will tell when I get it working, and if not, what I can't get to work. but I think I understand the concept
Reply
#6
finished getting the code to work. adjusted for the fact that having another def... in my code causes problems.
solution here:
character = 0
cur_str = []
my_string = "this is a test."
def setup():
    size(400, 400)
    
def draw():
    global character
    global cur_str
    global my_string
    frameRate(15)#again, added for exaggerated effect
    if character < len(my_string):
        cur_str.append(my_string[character])
        text(''.join(map(str, cur_str)), 10, 30)
        character = character + 1
for those wishing to keep the text on screen after the sentence has typed, add:
else:
        textSize(32)
        text(''.join(map(str, cur_str)), 10, 30)
to the end of the code
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 492 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,358 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,827 Oct-06-2022, 07:47 AM
Last Post: DPaul
  How would you (as an python expert) make this code more efficient/simple coder_sw99 3 1,790 Feb-21-2022, 10:52 AM
Last Post: Gribouillis
Star I was making code for collision for my character izmamonke 2 2,080 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  Making a code.py file to function, does not run hobbyist 6 2,889 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Making new lines of code AvioxyYT 1 1,801 Jan-22-2021, 07:02 PM
Last Post: buran
  I there a more efficient way of printing ? Capitaine_Flam 7 3,487 Dec-01-2020, 10:37 AM
Last Post: buran
  A more efficient way of comparing two images in a python vukan 0 2,018 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to make iterative search more efficient renergy 2 2,266 Jan-03-2020, 03:43 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020