I’ve written a simple RPG battle system where if you input “y” you get to attack but if you input “n” the enemy attacks you. The inputs are temporary until I implement an attacking system but all the of the outputs are being shown in the command prompt. How do I blit the print onto a pygame window instead of having it show up in the command prompt.
Code for battle system:
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]}}
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
print(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
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!')
if defender['stats']['hp'] <= 0:
print(f'{defender["name"]} has been slain...')
attacker['xp'] += defender['reward']
level(attacker)
if defender==hero:
print("[ G A M E O V E R ]")
print('---------------------------')
input('Press ENTER to quit. ')
exit()
else:
hero['stats']['hp']=heroHP
print('---------------------------')
def commands(player, enemy):
while ((enemy['stats']['hp'])>0): # continue algorithm unless enemy is dead
print('---------------------------')
cmd = input(f'Do you want to attack {enemy["name"]}? y/n: ').lower()
if 'y' in cmd:
takeDmg(player, enemy)
print(f'{enemy["name"]} takes the opportunity to attack!')
takeDmg(enemy, player)
elif 'n' in cmd:
print(f'{enemy["name"]} takes the opportunity to attack!')
takeDmg(enemy, player)
else:
break
commands(hero, boss1)
Code for window:
from pygame import *
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", 20)
def Title():
mouse.set_visible(1)
clock = time.Clock()
Text = Rect(70, 300, 500, 60)
while True:
for e in event.get():
if e.type == QUIT:
exit("Quit") # if X is pressed, exit program
if e.type == KEYDOWN:
if e.key == K_ESCAPE:
exit()
screen.fill(WHITE)
draw.rect(screen, GRAY, Text)
Text1_surf = FONT.render(("Test"), True, WHITE)
screen.blit(Text1_surf, Text)
display.update()
clock.tick(30)