Reputation: 189
Hello I am new to python and decided to code a simple RPG game using pygame. The commands work as intended but I need to add a delay to the text being blitted on screen. If you press the "yes" button, your player is going to attack, if you press the "no" button your player will do nothing and get attacked. After you kill the first boss your player will level up. The text that needs to blitted onto the empty black rectangle is the message congratulating you on levelling up and displays your new stats. EDIT: I am not looking for “time.sleep()”. If you could run through my program you’d understand why. I need to add that delay because the intent is to blit a line of text into a black rectangle and to add delay between each line popping up so that it doesn’t happen instantly. If i added a time.sleep() in my function it’s going to pause my TEXT(A, B, C) function for a while and then blit everything at once when I just want it to blit A, B, C with some delay in between. The full code is as follows:
from pygame import *
#from userInterface import Title, Dead
WIN_WIDTH = 640
WIN_HEIGHT = 400
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
DISPLAY = (WIN_WIDTH, WIN_HEIGHT)
DEPTH = 32
FLAGS = 0
init()
screen = display.set_mode(DISPLAY, FLAGS, DEPTH)
saveState = False
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (30, 30, 30)
FONT = font.SysFont("Courier New", 15)
heroHP = 1000
hero={'name' : 'Hero',
'height':4,
'lvl': 1,
'xp' : 0,
'reward' : 0,
'lvlNext':25,
'stats': {'str' : 12, # strength
'dex' : 4, # dexterity
'int' : 15, # intelligence
'hp' : heroHP, # health
'atk' : [250,350]}} # range of attack values
boss1={'name' : 'Imp',
'xp' : 0,
'lvlNext':25,
'reward' : 25,
'stats': {'hp' :400,
'atk' : [300,350]}}
ONE = None
TWO = None
THREE = None
FOUR = None
text = False
counter =0
def TEXT(A, B, C):
global ONE, TWO, THREE, FOUR, counter, text
text = True
if text:
if counter == 20:
ONE = A
elif counter == 40:
TWO = B
elif counter == 60:
THREE = C
elif counter == 200:
ONE = None
TWO = None
THREE = None
FOUR = None
counter = 0
text = False
counter += 1
def level(char): # level up system
#nStr, nDex, nInt=0,0,0
while char['xp'] >= char['lvlNext']:
char['lvl']+=1
char['xp']=char['xp'] - char['lvlNext']
char['lvlNext'] = round(char['lvlNext']*1.5)
nStr=0.5*char['stats']['str']+1
nDex=0.5*char['stats']['dex']+1
nInt=0.5*char['stats']['int']+1
print(f'{char["name"]} levelled up to level {char["lvl"]}!') # current level
A = (f'{char["name"]} levelled up to level {char["lvl"]}!') # current level
print(f'( INT {round((char["stats"]["int"] + nInt))} - STR {round(char["stats"]["str"] + nStr)} - DEX {round(char["stats"]["dex"] + nDex)} )') # print new stats
B = (f'( INT {round((char["stats"]["int"] + nInt))} - STR {round(char["stats"]["str"] + nStr)} - DEX {round(char["stats"]["dex"] + nDex)} )') # print new statsm
char['stats']['str'] += nStr
char['stats']['dex'] += nDex
char['stats']['int'] += nInt
TEXT(A,B,None)
from random import randint
def takeDmg(attacker, defender): # damage alorithm
dmg = randint(attacker['stats']['atk'][0], attacker['stats']['atk'][1])
defender['stats']['hp'] = defender['stats']['hp'] - dmg
print(f'{defender["name"]} takes {dmg} damage!')
#TEXT(f'{defender["name"]} takes {dmg} damage!')
if defender['stats']['hp'] <= 0:
print(f'{defender["name"]} has been slain...')
#TEXT(f'{defender["name"]} has been slain...')
attacker['xp'] += defender['reward']
level(attacker)
if defender==hero:
#Dead()
pass
else:
hero['stats']['hp']=heroHP
#Title()
pass
def Battle(player, enemy):
global ONE, TWO, THREE, FOUR
mouse.set_visible(1)
clock = time.Clock()
YES = Rect(100, 100, 50, 50)
NO = Rect(500, 100, 50, 50)
Text = Rect(70, 300, 500, 75)
#while ((enemy['stats']['hp']) > 0):
while True:
for e in event.get():
if e.type == QUIT:
exit("Quit") # if X is pressed, exit program
elif e.type == KEYDOWN:
if e.key == K_ESCAPE:
exit()
elif e.type == MOUSEBUTTONDOWN:
# 1 is the left mouse button, 2 is middle, 3 is right.
if e.button == 1:
# `event.pos` is the mouse position.
if YES.collidepoint(e.pos):
takeDmg(player, enemy)
print(f'{enemy["name"]} takes the opportunity to attack!')
#TEXT(f'{enemy["name"]} takes the opportunity to attack!')
takeDmg(enemy, player)
elif NO.collidepoint(e.pos):
print(f'{enemy["name"]} takes the opportunity to attack!')
#TEXT(f'{enemy["name"]} takes the opportunity to attack!')
takeDmg(enemy, player)
screen.fill(WHITE)
draw.rect(screen, BLACK, YES)
draw.rect(screen, BLACK, NO)
draw.rect(screen, GRAY, Text)
YES_surf = FONT.render(("YES"), True, WHITE)
NO_surf = FONT.render(("NO"), True, WHITE)
Text1_surf = FONT.render(ONE, True, WHITE)
Text2_surf = FONT.render(TWO, True, WHITE)
Text3_surf = FONT.render(THREE, True, WHITE)
Text4_surf = FONT.render(FOUR, True, WHITE)
screen.blit(YES_surf, YES)
screen.blit(NO_surf, NO)
screen.blit(Text1_surf, (80, 305))
screen.blit(Text2_surf, (80, 320))
screen.blit(Text3_surf, (80, 335))
screen.blit(Text4_surf, (80, 350))
display.update()
clock.tick(60)
Battle(hero, boss1)
I used the def TEXT(A, B, C) function to add a delay using a counter and then removing the text after a set amount of time. So far I have only implemented the TEXT function when the character levels up to test it but it does not seem to be working.
ONE = None
TWO = None
THREE = None
FOUR = None
text = False
counter =0
def TEXT(A, B, C):
global ONE, TWO, THREE, FOUR, counter, text
text = True
if text:
if counter == 20:
ONE = A
elif counter == 40:
TWO = B
elif counter == 60:
THREE = C
elif counter == 200:
ONE = None
TWO = None
THREE = None
FOUR = None
counter = 0
text = False
counter += 1
Upvotes: 0
Views: 432
Reputation: 9863
Using time.sleep is not a good idea to be used on games (this function is intended to be used on a very limited set of cases, real-time apps not being one of them).
Instead, your game should be event-driven based, that means new actions should be spawned when certain conditions are met (game state).
I see you're using pygame, so for time events you should take a look to its module time, in particular the function set_timer could help you out.
Also, I'd suggest you read this thread Why are global variables evil?
Upvotes: 1