Simple game with some basic concepts

0 Juan Grasso · April 6, 2015
This is a simple room-game i did to apply all the concepts i'd learn. I hope u like it and i look forward for your opinions. I'm sorry if my english is not good but it's not my maternal language.

actions.py

import map
import sys
import random
import time

current_room = 0
limits = []
pos = [0,0]
key = False
lives = 50
available_keys = ['a','s','d','w','exit','open','clue','key']

def next_room():
global current_room
global pos
global limits
global key
key = False
map.new_room(random.randint(5,10))
current_room += 1
limits = map.get_limits(current_room)
pos = map.get_inDoor(current_room)

def back_room():
global current_room
global pos
global limits
current_room -= 1
limits = map.get_limits(current_room)
pos = map.get_outDoor(current_room)
def action(a):
def move_up():
if pos[1] < limits:
pos[1] +=1
else:
print('There is a wall!')
def move_down():
if pos[1] > 0:
pos[1] -=1
else:
print('There is a wall!')
def move_right():
if pos[0] < limits:
pos[0] += 1
else:
print('There is a wall!')
def move_left():
if pos[0] > 0:
pos[0] -=1
else:
print('There is a wall!')
def openDoor():
if pos == map.get_inDoor(current_room):
if current_room != 1:
back_room()
print('Welcome to room n: '+str(current_room))
else:
print('You are at room n: 1 !!!')
if pos == map.get_outDoor(current_room):
if key:
next_room()
print('Welcome to room n: '+str(current_room))
else:
print('You have to find the key')
def readClue():
global lives
lives -= 1
print('You loose a live, now you have: ', lives)
if pos in map.get_Clues(current_room):
cords_key = map.get_key(current_room)
dist_key  = abs(pos[0] - cords_key[0]) + abs(pos[1] - cords_key[1])
print('Your distance to the key is: ',str(dist_key))
def pickKey():
global key
global lives 
lives -= 1
print('You loose a live, now you have: ', lives)
if pos ==map.get_key(current_room):
key = True
print('You found the key!!!')

def quit_game():
sys.exit()

actions = {'w':move_up,
's':move_down,
'd':move_right,
'a':move_left,
'open':openDoor,
'clue':readClue,
'key':pickKey,
'exit':quit_game}

return actions[a]
def user_input():
while True:
key = input('>')
if key in available_keys:
a = action(key)
a()
event_scanner()
if lives == 0:
print('You loose :)')
time.sleep(2)
sys.exit()
print(pos)
else:
print('Enter an available key')
def event_scanner():
if pos == map.get_inDoor(current_room):
print('You are in the inDoor')
if pos == map.get_outDoor(current_room):
print('You are in the outDoor')
if pos in map.get_Clues(current_room):
print('You found a clue')



print('''
You are in a room, use a,s,d,w to move arrownd ir.
You must find the key to open the outDoor
open to open a door.
clue to read a clue 
key to pick the key
exit to exit the quit the game 
Each time you read a clue or try to pick the key, you loose a live
If you loose all your lives you are dead
Have fun :)
''')
next_room()
user_input()


map.py


import pickle
import random
import os


pickle.dump('0',open('number_of_rooms','wb'))

def new_room(clues_number):
#  Add one room to the total

old_number = pickle.load(open('number_of_rooms','rb'))
new_number = int(old_number) + 1
pickle.dump(new_number,open('number_of_rooms','wb'))

#  Crate the new room and save it as a list

dimensios = 10 #  10x10 square rooms
inDoor = [random.randint(0,dimensios),random.randint(0,dimensios)]
outDoor = [random.randint(0,dimensios),random.randint(0,dimensios)]
clues = []
for i in range(clues_number):
clues_coords = [random.randint(0,dimensios),random.randint(0,dimensios)]
while (clues_coords == inDoor) or (clues_coords == outDoor):
clues_coords = [random.randint(0,dimensios),random.randint(0,dimensios)]
clues.append(clues_coords)
key = random.choice(clues)
room = [dimensios,inDoor,outDoor,clues,key]
file_name = 'room_n'+str(new_number)
pickle.dump(room,open(file_name,'wb'))

def get_room_number():
number = pickle.load(open('number_of_rooms','rb'))
return(int(number))
def get_limits(room_number):
room = pickle.load(open('room_n'+str(room_number),'rb'))
limits = room[0]
return(limits)
def get_inDoor(room_number):
room = pickle.load(open('room_n'+str(room_number),'rb'))
coords = room[1]
return(coords)
def get_outDoor(room_number):
room = pickle.load(open('room_n'+str(room_number),'rb'))
coords = room[2]
return(coords)
def get_Clues(room_number):
room = pickle.load(open('room_n'+str(room_number),'rb'))
coords = room[3]
return(coords)
def get_key(room_number):
room = pickle.load(open('room_n'+str(room_number),'rb'))
coords = room[4]
return(coords)

def clean_room():
number = get_room_number()
if number != 0:
for i in range(number):
os.remove('room_n'+str(i+1))
pickle.dump('0',open('number_of_rooms','wb'))

clean_room()

You have to run 'actions.py' to play.

Post a Reply

Replies

Oldest  Newest  Rating
0 Ian Wheeler · April 9, 2015
It keeps giving me errors I don't know why. It says there is a syntax error in both
0 Juan Grasso · April 10, 2015
It's strange, try executing it with cmd console
0 Nupur Garg · April 10, 2015
Nice. it is amazing..but it is not working.There is errors in syntax
0 Juan Grasso · April 10, 2015
I think that the problem is because the webpage change the  :) faces with a link. Try to use the code at : https://github.com/juangrassob/LearningPython/tree/master/BasicRoomGame and executing it with the cmd console. And be sure u have python 3.
  • 1

Python

107,237 followers
About

This section is all about snakes! Just kidding.

Links
Moderators
Bucky Roberts Administrator