Tuesday, December 11, 2012

Lunar Lander

Just made a lunar lander game thingy based on some of the constraints found at http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html. /shruggies, took about 10 minutes.



#!/usr/bin/python
'''
lunar_lander.py
basic lunar lander simulation based on constraints found at:
http://www.cs.carleton.edu/faculty/dmusican/cs111s10/lunarlander.html
'''

import sys

class LunarLander(object):
    'Lunar lander object'
    def __init__(self):
        self.altitude = 10
        self.velocity = 4
        self.fuel = 2
    def getVelocity(self):
        return self.velocity
    def getAltitude(self):
        return self.altitude
    def getFuel(self):
        return self.fuel
    def thrust(self, fuel):
        if fuel > self.fuel:
            self.velocity -= fuel * 4
            self.fuel = 0
        self.velocity -= fuel * 4
        self.fuel -= fuel
     
    def tick(self, fuel):
        self.thrust(fuel)
        self.velocity += 2
        self.altitude -= self .velocity
        #check for landing or crash
        if self.altitude <= 0 and self.velocity < 5:
            print 'skrrttt~ Houston, we have landed. ~skrrtt~ Victory! \'Murica! Fuck yeah!'
            self.restart()
        elif self.altitude <= 0 and self.velocity > 4:
            print 'Oh no, you crashed!'
            self.restart()          
    def report(self):
        print '\nAlt = %d Vel = %d Fuel = %d' % (self.getAltitude(),
                                           self.getVelocity(),
                                           self.getFuel())
    def restart(self):
        restart = raw_input('Replay? Y/N >')
        if restart.lower() == 'y':
            main()
        sys.exit('game over!')
             
def main():
    print 'Welcome to Lunar Lander!'
    lander = LunarLander()  

    running = True
    while running:
        lander.report()
        x = 0
        x = input('input thrust: ')
        lander.tick(x)            
main()

No comments:

Post a Comment

Let me know what you think!