# import files
from random import randint
import os.path
import yaml
import json
yaml.warnings({'YAMLLoadWarning': False})
# Global Variables
player = {'Name': 'The Hero', 'Min Damage': 2, 'Max Damage': 4, 'Defence': 1, 'HP': 20, 'Inventory': [],
'fought': False, 'Y': 0, 'X': 0, 'day': 1}
ifh = {}
day = 1
col = 0
row = 0
col = player['Y']
row = player['X']
day = player['day']
save = open('C:/test/savegame.csv', 'r+')
# +------------------------
# | Text for various menus
# +------------------------
main_text = ["New Game",
"Resume Game",
"Exit Game"]
town_text = ["View Character",
"View Map",
"Move",
"Rest",
"Save Game",
"Exit Game"]
open_text = ["View Character",
"View Map",
"Move",
"Sense Orb",
"Exit Game"]
fight_text = ["Attack",
"Run"]
default_map = [['T', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', 'T', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', 'T', ' ', ' ', ' '],
[' ', 'T', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', 'T', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K', ' ']]
world_map = [['H/T', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', 'T', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', 'T', ' ', ' ', ' '],
[' ', 'T', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', 'T', ' ', ' ', ' ', ' '],
[' ', ' ', ' ', ' ', ' ', ' ', ' ', 'K', ' ']]
print("Welcome to Ratventure!")
print("----------------------")
# Code your main program here
# main text
def displaymenu():
global player
for i in range(len(main_text)):
print('{}) {}'.format(i + 1, main_text[i]))
choice = int(input('Enter choice: '))
if choice == 1:
displaytown()
elif choice == 2:
player = resumegame()
displaytown()
elif choice == 3:
print('Exiting game')
else:
print('\nInvalid choice please try again')
displaymenu()
def resumegame():
global ifh
filename = 'C:/test/savegame.csv'
if not os.path.isfile(filename):
return {}
with open(filename) as ifh:
return yaml.load(ifh)
# town text
def displaytown():
print()
global day
global player
print('Day {}: You are in a town.'.format(day))
for i in range(len(town_text)):
print('{}) {}'.format(i + 1, town_text[i]))
choice = int(input('Enter choice: '))
if choice == 1:
print(player.get('Name'))
print('{:>8} {}-{}'.format("Damage:", player.get('Min Damage'), player.get('Max Damage')))
print('{:>8} {}'.format('Defence:', player.get('Defence')))
print('{:>8} {}'.format('HP:', player.get('HP')))
displaytown()
if choice == 2:
displaymap()
displaytown()
if choice == 3:
movehero()
if choice == 4:
print('You are fully healed.')
day += 1
player['HP'] = 20
displaytown()
if choice == 5:
print('Game saved.')
with open('C:/test/savegame.csv', 'r+') as data:
data.write(str(player))
displaytown()
if choice == 6:
'Exiting game.'
# world map
def displaymap():
global world_map
for r in world_map:
print('\n{}+'.format('+---' * 8))
for c in r:
print('|{:^3}'.format(c), end='')
print('\n{}+'.format('+---' * 8))
# move hero
def movehero():
displaymap()
global row
global col
global world_map
move = False
while not move:
print('W = up; A = left; S = down; D = right')
move = str(input('Your move: '))
if move == 'W' or move == 'w':
while player['Y'] > 0:
if world_map[col][row] == 'H/T':
world_map[col][row] = 'T'
col -= 1
world_map[col][row] += 'H'
elif world_map[col][row] == 'H':
world_map[col][row] = ' '
col -= 1
world_map[col][row] += 'H'
player['Y'] -= 1
move = True
displaymap()
displaytown()
else:
print('End of world please try again')
move = False
elif move == 'A' or move == 'a':
while player['X'] > 0:
if world_map[col][row] == 'H/T':
world_map[col][row] = 'T'
row -= 1
world_map[col][row] += 'H'
elif world_map[col][row] == 'H':
world_map[col][row] = ' '
row -= 1
world_map[col][row] += 'H'
player['X'] -= 1
move = True
displaymap()
displaytown()
else:
print('End of world please try again')
move = False
elif move == 'S' or move == 's':
while player['Y'] < 7:
if world_map[col][row] == 'H/T':
world_map[col][row] = 'T'
col += 1
world_map[col][row] += 'H'
elif world_map[col][row] == 'H':
world_map[col][row] = ' '
col += 1
world_map[col][row] += 'H'
player['Y'] += 1
move = True
displaymap()
displaytown()
else:
print('End of world please try again')
move = False
elif move == 'D' or move == 'd':
while player['X'] < 7:
if world_map[col][row] == 'H/T':
world_map[col][row] = 'T'
row += 1
world_map[col][row] += 'H'
elif world_map[col][row] == 'H':
world_map[col][row] = ' '
row += 1
world_map[col][row] += 'H'
player['X'] += 1
move = True
displaymap()
displaytown()
else:
print('End of world please try again')
move = False
def ratfight():
global day
global player
rathp = 10
dam = randint(1, 3)
print('Day {}: You are out in the open.'.format(day))
print('Encounter! - Rat')
print('Damage: 1 - 3')
print('Defence: 1')
print('HP: {}'.format(rathp))
for i in range(len(fight_text)):
print('{}) {}'.format(i + 1, fight_text[i]))
choice = int(input('Enter choice: '))
while choice == 1:
print('you deal {} damage to the rat'.format(randint(player.get('Min Damage'), player.get('Max damage'))))
print('Ouch! The rat hi you for {} damage!'.format(dam))
print('You have {} HP left.'.format(player.get('HP') - dam))
if rathp != 0 or player.get('HP') != 0:
ratfight()
elif rathp == 0:
print('you deal {} damage to the rat'.format(randint(player.get('Min Damage'), player.get('Max damage'))))
print('The Rat is dead! You are victorious!')
openworld()
elif player.get('HP') == 0:
print('GAME OVER! You are dead')
break
def openworld():
for i in range(len(open_text)):
print('{}) {}'.format(i + 1, open_text[i]))
choice = int(input('Enter choice: '))
while choice == 1:
print()
displaymenu()
save.close()
this is a game I created for my school project, but when I want to move the hero, I can only move it once, but when I move the second time the map doesn’t show the hero moving