![]() |
Need help making code more efficient - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Need help making code more efficient (/thread-9493.html) |
Need help making code more efficient - dhbergen - Apr-12-2018 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 withthanks to all who help out! RE: Need help making code more efficient - Larz60+ - Apr-12-2018 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.') RE: Need help making code more efficient - dhbergen - Apr-12-2018 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
RE: Need help making code more efficient - Larz60+ - Apr-12-2018 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. RE: Need help making code more efficient - dhbergen - Apr-12-2018 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 RE: Need help making code more efficient - dhbergen - Apr-17-2018 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 + 1for 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 |