Beginner's work (part 2)

This is the place to post any code that you want to share with the community. Only completed scripts should be posted here.

Beginner's work (part 2)

Postby Ringer » Sun Jan 26, 2014 9:05 pm

Hello people!
It's the same 'new guy' who posted some code last year. First of all, I'd like to thank those that gave me some feedback regarding my work (here's a link to my last post http://python-forum.org/viewtopic.php?f=11&t=7720). I really apreciate it.
But let's not waste any time. I'm sure that you're eager to see my new little game (it's about a college student, text adventure style).

I've divided my code into three different files and imported them.
The files are ordered accordingly.

The first one is the ex45_main.py file (the main file)
Code: Select all
import ex45_play

a_game = ex45_play.Engine()
a_game.play("college_room_scene")


After that comes the ex45_play.py file (most of the work is done here)
Code: Select all
# I'll be using the scenes I made in the previous file.
import ex45_scenes


# This class runs the game
class Engine(object):
   
   def __init__(self):
   
      self.begin = Scenes()
      
   def play(self, first_scene):
      
      self.begin.scene(first_scene)
      

# This class is a map for the scenes and also
# helps another class to select the next scene.      
class Map(object):

   scenes = {
   "fail_scene": ex45_scenes.FailScene(),
   "college_room_scene": ex45_scenes.CollegeRoomScene(),
   "corridor_scene": ex45_scenes.CorridorScene(),
   "amphiteatre_scene": ex45_scenes.AmphiteatreScene(),
   "cheat_scene": ex45_scenes.CheatScene(),
   "victory_scene": ex45_scenes.VictoryScene()
   }
   
   # This function runs the scene our actor is in and
   # returns the name of the next scene.
   def scene_select(self,current_scene):
      
      scene = Map.scenes.get(current_scene)
      next_scene ="%s" %scene.enter()
      return next_scene
   

# This class uses the Map() class to make our character
# go through different scenes of the game
class Scenes(object):
   
   def __init__(self):
   
      self.map = Map()
   
   # This function repeatedly makes our character go through
   # different scenes untill he hits either the "GAME OVER" scene
   #(FailScene) or the "endgame" scene (VictoryScene) (because of 'quit')
   def scene(self, current_scene):
      while True:
         current_scene = "%s" %self.map.scene_select(current_scene)


And now, the ex45_scenes.py file (I'm using this one in the ex45_play.py file to import some scenes my character is going through)
Code: Select all
# I'm importing exit because I'll need to get out of a while loop later on
# without getting an error mesage.
from sys import exit
# I will be needing a random number for the FailScene in order
# to get different mesages.
from random import randint


# This class is used when you lose the game.
class FailScene(object):
   
   fails = ["College is not for you mate. You could consider going home.\n",
           "Don't worry! You'll have another chance to fail.\n",
         "Well, at least you're not the dumbest guy!\n",
         "At least you still have your dignity! What's left of it...\n"
         ]
         
   # Randomly displays a funny "GAME OVER" message.      
   def enter(self):
   
      print FailScene.fails[randint (0, len(self.fails)-1)]
      exit(1)
      

# This is the first scene of the game.      
class CollegeRoomScene(object):
   
   def enter(self):
   
      print "You wake up at 8:10 a.m. and hear your phone ringing."
      print "It's your pal who helped you pass your exam using a"
      print "special in-ear headphone. Today, you'll be the one"
      print "helping him to pass his exam. In order to do this,"
      print "you should get up from your bed and answer the phone."
      print "Will you answer the phone or keep on sleeping?\n"
      print "-" * 10
       
      answer = raw_input('> ')
      
      print "-" * 10
      print " "
   
      if "answer" in answer:
   
         print "You succesfully woke up and answered the phone."
         print "Your friend reminded you that he'll need your help"
         print "to pass the exam. You put on some clothes, and go"
         print "to his room. Together, you manage to equip him with"
         print "the cheating gear. He notices that the exam starts in"
         print "ten minutes, and leaves you alone in the room to help"
         print "him by dictating the lessons and the problems.\n"
         print "-" * 10
         print " "
         return "corridor_scene"
      
      elif "sleep" in answer:
   
         print "You didn't wake up untill your friend came into your"
         print "room, mad at you because he failed his exam and you"
         print "did nothing to help him. Hell of a friend you are!\n"
         print "-" * 10
         print " "
         return "fail_scene"
      
      else:
   
         print "That's not an option\n"
         print "-" * 10
         print " "
         return "college_room_scene"
      

# This is the second scene of the game.      
class CorridorScene(object):

   def enter(self):
   
      print "This time, you're the guy who needs to pass the exam."
      print "You got into the university just in time and are running"
      print "on the corridor towards the amphiteatre so you can cheat on"
      print "the exam. On your way to the amphiteatre you meet a nice girl."
      print "Well, are you going to turn your attention towards the girl or"
      print "keep on going so you can cheat on the hardest exam ever?\n"
      print "-" * 10
   
      answer = raw_input('> ')
      
      print "-" * 10
      print " "
   
      if "go" in answer:
   
         print "Good choice mate! You'll get 'em girls after passing the"
         print "exam. Now get in that amphiteatre and show me how professional"
         print "cheaters make extra hard exams look like a piece of cake\n"
         print "-" * 10
         print " "
         return "amphiteatre_scene"
         
      elif "girl" in answer:
   
         print "You start flirting with the girl, but a huge buffed up"
         print "angry gorilla-like guy (her boyfriend) appears."
         print "He punches you unconscious and you wake up in a hospital."
         print "Looks like you didn't manage to get to the exam.\n"   
         print "-" * 10
         print " "
         return "fail_scene"
      
      else:
   
         print "That's not an option"
         print "-" * 10
         print " "
         return "corridor_scene"
      

# This is third scene of the game.      
class AmphiteatreScene(object):
   
   def enter(self):
   
      print "You got into the amphiteatre just before the exam had begun."
      print "The only free seat is in the last row. You take an exam paper,"
      print "Go to your seat and turn on the headphone and the microphone."
      print "Looks like your friend is sleeping, because you can hear him"
      print "snoring. Will you risk calling him so he wakes up or will"
      print "you try and solve the exam by yourself?\n"
      print "-" * 10
      
      answer = raw_input('> ')
      
      print "-" * 10
      print " "
   
      if "call" in answer:
   
         print "You call your friend, and wake him up. He's pretty mad at"
         print "you, but then you remind him what he was supposed to do."
         print "You end the call and start using the cheating equipment."
         print "After telling him the content of the exam, he starts"
         print "dictating the required lessons and the problems. You manage"
         print "to write everything and finis the exam in less than an hour.\n"
         print "-" * 10
         print " "
         return "cheat_scene"
      
      elif "solve" in answer:
      
         print "You try to solve the problems by yourself. The whole exam"
         print "looks like one big hieroglyph. It would've been much better"
         print "if you learned for the exam. This time, cheating didn't work!\n"
         print "-" * 10
         print " "
         return "fail_scene"
      
      else:
   
         print "That's not an option!"
         print "-" * 10
         print " "
         return "amphiteatre_scene"
      

# This is the fourth scene of the game.      
class CheatScene(object):
   
   def enter(self):
   
      print "The teacher realised that you cheated, because he asked you to"
      print "explain something and you couldn't. But he decided to give you"
      print "another chance to prove your innocence. Now you have to solve one"
      print "of the problems from the exam. Are you going to try and solve them"
      print "by yourself or try and cheat again?\n"
      print "-" * 10
   
      answer = raw_input('> ')
      
      print "-" * 10
      print " "
   
      if "solve" in answer:
      
         print "Fortunately, the problem you have to solve is the only one"
         print "you actually managed to learn. You start writing it on the"
         print "new exam paper. You finish it in like ten minutes, hand over"
         print "the paper, and pass the exam. Now it's time to PARTY!!!\n"
         print "-" * 10
         print " "
         return "victory_scene"
      
      elif "cheat" in answer:
         print "You try cheating again, but the teacher sees you talking"
         print "and takes your exam paper. Fortunately, he's a good guy,"
         print "so you won't get thrown out of college. But you still"
         print "failed your exam. Should've tried solving your problems"
         print "without the help of others this time!\n"
         print "-" * 10
         print " "
         return "fail_scene"
      
      else:
         print "That's not an option!"
         print "-" * 10
         print " "
         return "cheat_scene"
      

# This is the "endgame" scene.      
class VictoryScene(object):
   
   def enter(self):
   
      print "You managed to pass the hardest exam ever made!"
      print "After going back into your room, you called your"
      print "friends and threw a huge party to celebrate this"
      print "event. Congratulations! You are the master cheater!\n"
      exit(1)


Once again, I'll apreciate any kind of feedback so don't hesitate to reply!
User avatar
Ringer
 
Posts: 2
Joined: Fri Oct 18, 2013 7:36 pm

Re: Beginner's work (part 2)

Postby metulburr » Sun Jan 26, 2014 9:46 pm

In your scenes module, you havea lot of redundancy with the spacers:
Code: Select all
          print "-" * 10
          print " "


Also with so much text printed to the console, you would be better off having it in files (JSON, whatever), and loading the files, instead of hardcoding it into the actual code.
New Users, Read This
OS Ubuntu 14.04, Arch Linux, Gentoo, Windows 7/8
https://github.com/metulburr
steam
User avatar
metulburr
 
Posts: 1470
Joined: Thu Feb 07, 2013 4:47 pm
Location: Elmira, NY

Re: Beginner's work (part 2)

Postby XndrK » Wed Jan 29, 2014 7:33 pm

For the random game over message, you could have used random.choice() instead. Would have made that section of code a lot easier to read.

I do applaud you for the extensive comments (something I have trouble with).

Keep up the good work!
Proverbs 26:14 describes me a bit too well.

Version: Python 2.7.5

https://github.com/4of92000
https://github.com/PythonForum/
User avatar
XndrK
 
Posts: 172
Joined: Sat Jun 15, 2013 7:57 pm


Return to Completed Scripts

Who is online

Users browsing this forum: No registered users and 0 guests