I’m trying to make the code stop at a specific point

I am trying to make a story game with python using pygame where the text shows up on screen over time, like in pokemon. I am trying to make the text stop at a specific point in the array, 184. The problem is I don’t know how to make the code stop at that point and using a while loop will stop refreshing the text and make it overlap. Send help. Here is the code.

`#Imports the python system
import sys
import time
from time import sleep

#Imports the music system
from pygame import event, mixer, surface 

#Imports the python libary system
import pygame

#Imports randomness for dice rolls
import random

#Tells where to get pygame from
from pygame.locals import*

#initalizes the pygame (creates a shell of a pygame)
pygame.init() 

#defines the width of the display window
display_width = 700

#defines the height of the display window
display_height = 900

#sets the gameDisplay window to the desired size
gameDisplay = pygame.display.set_mode((display_width,display_height))
#optional...creates a title caption on the title bar of the graphics

#This will set the hex code green to the variable green
green = (1, 255, 50)

#This will set the hex code burgundy to the variable burgundy
burgundy = (128, 0 ,32)

#This will set a dark blue to the variable colorRandom
blue = (14, 5, 200)

#This will set the color black to the varaiable
black = (0,0,0)

#This will set the color white to the variable
white = (255,255,255)

#Creates an array for all the images
images = []

#Creates an array for all the music
musics = []

#Varaible for Strength
STR = 0

#Varaible for Health
HP = 6

#Variable for Charisma
CHS = 0

#Creates an array for the text dialog
script = ["The sun was beating down on Jenniferu0027s","exhausted figure as she trudged along","the sandy desert.","Sweat was beading on her brow and","sliding down her face, promptly forcing","her eye to jerk shut as she stopped","walking momentarily.","It was so warm outside and it seemed","like all the heat radiating from the","shining sun just targeted her head","instead of all the other vagabonds","that surrounded her.","She was the only one with actual hair","in the hot desert air and it was","starting to make her think that the","blistering heat was solely attacking","her because of it.","Maybe it was because, from a bird's eye","view, Jennifer seemed like a lone","traveler as all the others just","blended into the tan dunes. ","As she brought up her arm to wipe the","sweat from her face, she heard the","dragging footsteps of her fellow","companions stop sequentially, the","feeling of the slight warm breeze","blowing sand against the back of her","legs made ever so noticeable by her","halt in movement. The raven-haired","girl didn't even want to think of the","intangible number of sand grains","littered across her body now.","The journey so far was treacherous,","if you asked Jen. On top of having","walked through the identical landscapes","of blazing hot desert sand for days on","end, rationing water from the same","greasy canteen that she would have to","share with a bunch of strangers,","enduring the nauseatingly sweltering","heat that always seemed to cling to her","like those grains of sand, the night was","barely any better. It was cold, *way*","too cold. While it was nice in contrast","to the blistering heat (for the first","few minutes), it made Jennifer want to","huddle into a fetal position and just","hibernate for the rest of forever.","It didn't help that her family","practically flung her into the arms of","this random man, who called himself","'Moses', to escort her and many others","'of her kind' to the promised holy lands,","whatever that means. Again, if you","asked Jen, this all smelt like a scam,","but her parents were so won over by this","man's charisma and ability to talk","absolute nonsense with full conviction","that anything she could've said would","fall on deaf ears. She found out the","hard way, briefly reprimanded for being","ungrateful to this Moses guy before","being sent off. ","The first time Jennifer saw the group","she would be traveling with for the next","while, she figured out that when Moses","spoke of 'her kind', he did not mean","other Asian people and in fact, people","that needed 'saving' or that were not","believers. At this point, all Jennifer","could do is grit her teeth and live day","by day until she gets to the promised","holy lands. Hopefully, Jesus could","provide more than one communal toilet","when they finally arrive.","'Are you okay, Jennifer?' One of the men","turned around to look at her as she","stopped, wiping her face with the back","of her hand. 'It's fine, I'm just too","warm.' She sighed before she started","walking again to catch up to everyone.","The last thing she wanted was to lose","sight of them when she could barely tell","the ground from the sky with all the","sand flying around these people who","essentially camouflaged into the","landscape. She wiped her brow, drenching","the back of her hand with sweat.","'Well, we're not too far now,' Moses","spoke. 'I reckon we'll arrive by","tomorrow if we keep at this pace.'","Jennifer, and the rest of the group,","visibly perked up at this. This was","quite frankly the first piece of good","news any of them had heard since this","journey began. God, how long had it been","since she had slept in the comfort of","her bed, snoozing through the racket her","siblings were causing as they were","playing some random game? How long had","it been since she took a *shower*? She","quickly pushed that thought aside, the","last thing she wanted to do was to be","made acutely aware of how badly she","could smell right now with no access to","anything to freshen her up. 'Really?'","One of the vagabonds tilted their head","as they spoke. 'That's great! We should","keep moving then.' An unenthusiastic","chorus of 'yeah's rang out as they","continued walking behind Moses. Jennifer","internally groaned. She wondered if it","was too late for her to go home now.","Screw true enlightenment, she wanted a","bed all to herself!","It turned out, Moses was a big fat liar.","No way buying fruit from a bunch of","traveling merchants in the middle of","nowhere was more important than cutting","down on the time she would be spending","in these increasingly uncomfortable","straw sandals. It felt like her foot was","about to be cleaved in half right in","between her big toe and the one next to","it. Meanwhile, she could eye the Nike","Air Jerusalems that Moses was sporting,","making sure not to make any unnecessary","movement to prevent any creasing in his","new all-white kicks.'Shoes are made to","be worn, but also proper care will","ensure its longevity.' He had once said","as his explanation for his unusual","walking habits. Yeah, right. Jennifer","internally scoffed. I bet he scammed","some family for those. 'Eat up, the","fruit is fresh.' Moses handed the fruit","down to each of them.","Everyone politely accepted it, thanking","Moses and the merchants as they dug in.","It was sweet, juicy, and honestly really","refreshing. As Jennifer came to find out,","it was a peach. It was delightful and","she savored every bite until she was","down to its strangely hairy core. It","was a fascinating thing, and instead of","discarding it in the sand, she quietly","slipped it into her pocket. Plus, she","felt a bit bad just throwing it away","like that. She was sure she'd find","another use for it somehow. They walked","on for a while longer before Jennifer","noticed the sun dipping below the","horizon. It was time to set up camp for","the night. The moon soon entered the","sky with frigid winds accompanying it,","quickly making Jen long for the warmth","of the day once more. Thankfully, all","the vagabonds had become much more","accustomed to setting up the tents in a","timely and orderly fashion and Jennifer","could soon take refuge from the","unforgiving weather that was whipping","at her exposed skin relentlessly. Moses","seemed pretty indifferent to it as he","aided a few other vagabonds with","starting a fire for dinner. Jennifer","could hear the conversation they were","having from her position in the tent","and learned that Moses was not the guy's","real name. It was Moses B. Manbot. She","honestly had no idea how to react to","that and kept the information to","herself, turning her attention to the","efforts of her fellow travelers as they","fed the growing flame.", "As the fire sparked and the darkness", "grew across the sky, the ragtag group", "led by Moses B. Manbot settled on a", "seemingly quaint part of the desert,", "undisturbed by the various dangers living", "in the barren dunes. Jen breathed with", "relief, taking in the chilly air with", "delight. Finally, now she had some time", "to spare for herself.",]

#The counting variable to count where we are in the text array so the correct dialog plays                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
talk = 0

#This significes the change in x position of the image
x_change=0

#This is the change in y position for the image
y_change=0

#This is the variable that will activate the text dialog. It will be set to true when needed
when = True

#This is the starting location of the text
y_text = 568

#Declares the variable line space to 32
line_space = 32

#Allows for the game to turn on and off at will
gaming = True

#declares the variable to hold the png of "mouse"
mouse = pygame.image.load('mouse.png')

#Creates and image of a desert in the background
desert = pygame.image.load('desert.png')

#Creates a rectangle to act as a border from the text to the images
borderRect = pygame.Rect(0,575,700,25)

#Creates a rectangle for the starting screen
startRect = pygame.Rect(300,300,100,60)

#Creates a rectangle for the mouse
mouseRect = pygame.Rect(100,200,mouse.get_width(),mouse.get_height())

#Creates a rectangle for the starting screen
quitRect = pygame.Rect(300,500,100,60)

#Creates a background for the starting screen
backgroundRect = pygame.Rect(100,200,500,500)

#Creates a rectangle for showing important information
freeTimeRect = pygame.Rect(300,300,100,60)

#Creates a font, in this case uses the system font
#Ariel, and also sets the font size you wish to use
#in this example, it is size 32
font = pygame.font.Font('gen3.ttf',32) 

#creates a text variable, that is rendered into an image for display,always have the True as the second option, and the third option represents the color you want to paint the text in.
text = font.render("",True,(white))
#gets a rectangle that is an approprate size for the text in the font and font size that have been set
textRect = text.get_rect() 

#The text for the start button
start = font.render("START",True,(black))

#The text for the quit button
end = font.render("QUIT",True,(black))

#The text for showing you have some free time
freeTime = font.render("You have some free time! What will you do?",True,(black))

# Starting the mixer
mixer.init() 

# Loading the song
mixer.music.load("land-of-petals.mp3") 

# Setting the volume to 0.7, this is a value between 0.0 and 1.0
mixer.music.set_volume(0.7)

#sets up game clock, this is used to track time within the game
#and to control the frame rate (frames per second or FPS)
clock = pygame.time.Clock() 


#game loop
while gaming:
    when = False
#loops through all of the events that have been triggered
#if the quit event is triggered it will lend the game
    for event in pygame.event.get():
        if event.type == QUIT:
            gaming = False


#checks to see if the event type is a keydown event
        if event.type == pygame.KEYDOWN:
 #checks to see what key was pressed, separate if statement
 #and action based on the key pressed
 #if the left key was pressed
            if event.key == pygame.K_LEFT:
#moves the x point left by 5 pixels
                    x_change = -1
#if the right key was pressed
            if event.key == pygame.K_RIGHT:
#moves the x point right by 5 pixels
                x_change = 1
#if the up key was pressed
            if event.key == pygame.K_UP:
#moves the y point left by 5 pixels
                y_change = -1
#if the down key was pressed
            if event.key == pygame.K_DOWN:
#moves the y point right by 5 pixels
                y_change = 1
#checks for a key up event
        if event.type == pygame.KEYUP:
#checks to see if either up or down key was released and
#stops up and down movement because key was released.
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0 
#checks to see if either left or right key was released and
#stops left and right movement because key was released.
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                
#Dectects if the rectangles are colideing, if they are checks if a key is pressed down. If that key is the space bar, start the game
        if pygame.Rect.colliderect(mouseRect,startRect):
 #stuff that happens when they collide, eg ends game
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                     gaming = False
                     when = True

#Dectects if the rectangles are colideing, if they are checks if a key is pressed down. If that key is the space bar, quit the game
        if pygame.Rect.colliderect(mouseRect,quitRect):
 #stuff that happens when they collide, eg ends game
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    gaming = False



#Draws a the background button
    pygame.draw.rect(gameDisplay,blue,backgroundRect)

#Draws the start button
    pygame.draw.rect(gameDisplay,white,startRect)

#Draws a the quit button
    pygame.draw.rect(gameDisplay,white,quitRect)

#how we display an image in a rectangle.
    gameDisplay.blit(start,startRect) 
    
#how we display an image in a rectangle.
    gameDisplay.blit(end,quitRect) 
    
#how we display an image in a rectangle.
    gameDisplay.blit(mouse,mouseRect) 

 #checks if there is space to move left and if the player moves left. If there is no room, it stops the movement
    if (mouseRect.left > 0 and x_change < 0):
         mouseRect.move_ip(x_change, 0)
        
 #checks if there is space to move right and if the player moves right. If there is no room, it stops the movement
    if (mouseRect.right < gameDisplay.get_width() and x_change > 0):
        mouseRect.move_ip(x_change, 0) 

 #checks if there is space to move up and if the player moves up. If there is no room, it stops the movement
    if (mouseRect.top > 0 and y_change < 0):
         mouseRect.move_ip(0, y_change)
        
 #checks if there is space to move down and if the player moves down. If there is no room, it stops the movement       
    if (mouseRect.bottom < gameDisplay.get_height() and y_change > 0):
        mouseRect.move_ip(0, y_change) 





#updates the screen to show all changes made above in the update portion
    pygame.display.update()

#updates the screen to make the image not have a trail    
    gameDisplay.fill(black)




while when and talk <=184:
    gaming = False
    for event in pygame.event.get():
        if event.type == QUIT:
            when = False
            
#checks to see if the event type is a keydown event
        if event.type == pygame.KEYDOWN:
 #checks to see what key was pressed, separate if statement
 #and action based on the key pressed
 #if the left key was pressed
            if event.key == pygame.K_LEFT:
 #moves the x point left by 5 pixels
                x_change = -1
 #if the right key was pressed
            if event.key == pygame.K_RIGHT:
 #moves the x point right by 5 pixels
                x_change = 1
 #if the up key was pressed
            if event.key == pygame.K_UP:
 #moves the y point left by 5 pixels
                y_change = -1
 #if the down key was pressed
            if event.key == pygame.K_DOWN:
 #moves the y point right by 5 pixels
                y_change = 1
#checks for a key up event
        if event.type == pygame.KEYUP:
 #checks to see if either up or down key was released and
 #stops up and down movement because key was released.
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                    y_change = 0 
 #checks to see if either left or right key was released and
 #stops left and right movement because key was released.
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0
                

 #checks if there is space to move left and if the player moves left. If there is no room, it stops the movement
    if (mouseRect.left > 0 and x_change < 0):
         mouseRect.move_ip(x_change, 0)
        
 #checks if there is space to move right and if the player moves right. If there is no room, it stops the movement
    if (mouseRect.right < gameDisplay.get_width() and x_change > 0):
        mouseRect.move_ip(x_change, 0) 

 #checks if there is space to move up and if the player moves up. If there is no room, it stops the movement
    if (mouseRect.top > 0 and y_change < 0):
         mouseRect.move_ip(0, y_change)
        
 #checks if there is space to move down and if the player moves down. If there is no room, it stops the movement       
    if (mouseRect.bottom < gameDisplay.get_height() and y_change > 0):
        mouseRect.move_ip(0, y_change) 




#Displays the image of a desert 
    gameDisplay.blit(desert,(0,0))

#Draws a border between the text and everything else
    pygame.draw.rect(gameDisplay,white,borderRect)


    if talk != 184:
        def text_ani(str, tuple):
            x, y = tuple
            y += y_text+line_space ##shift text down by one line
            char = ''        ##new string that will take text one char at a time. Not the best variable name I know.
            letter = 0
            count = 0
            for i in range(len(str)):
                pygame.display.update()
                pygame.event.pump() ## this is very important if your event queue is not handled properly elsewhere. Alternativly pygame.event.pump() would work.
                time.sleep(0.000005) ##change this for faster or slower text animation
                char = char + str[letter]
                text = font.render(char, False, (white), (black)) #First tuple is text color, second tuple is background color
                textrect = text.get_rect(topleft=(x, y)) ## x, y's provided in function call. y coordinate amended by line height where needed
                gameDisplay.blit(text, textrect)
                pygame.display.update(textrect) ## update only the text just added without removing previous lines.
                count += 1
                letter += 1



        text_ani(script[talk], (0, 0)) # text string and x, y coordinate tuple.
        talk += 1
        text_ani(script[talk], (0, 46))
        talk += 1
        text_ani(script[talk], (0, 92))
        talk += 1
        text_ani(script[talk], (0, 138))
        talk += 1 
        text_ani(script[talk], (0, 184))
        talk += 1 
        text_ani(script[talk], (0, 230))
        talk += 1 
        #time.sleep(0.3)


#updates the screen to show all changes made above in the update portion
    pygame.display.update()

#updates the screen to make the image not have a trail    
    gameDisplay.fill(black)`

When I used a while loop, the text would start overlapping.

New contributor

Frank Lin is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật