Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
663e355
Whee
arpanrau Oct 26, 2016
2c1786d
Wheeeee
arpanrau Oct 27, 2016
14ba80c
Finished landerclass and controller
arpanrau Oct 29, 2016
4745708
added background image and view + main changes
Oct 29, 2016
6f5d30e
fixed merg confilicts
Oct 29, 2016
340135d
done
arpanrau Oct 29, 2016
cc041a5
updated to view
Oct 29, 2016
d69fad6
merged
arpanrau Oct 29, 2016
1f6763b
Merge branch 'master' of https://github.com/arpanrau/InteractiveProgr…
arpanrau Oct 29, 2016
200f28d
updatign view and main
Oct 29, 2016
2287f6d
Merge branch 'master' of https://github.com/arpanrau/InteractiveProgr…
Oct 29, 2016
2555eaf
Whee
arpanrau Oct 29, 2016
068fdcb
fixing indents
Oct 29, 2016
2901dbf
whee
arpanrau Oct 29, 2016
29876a0
added lander image
Oct 29, 2016
447159e
Added file mvcindent with fixed indents
arpanrau Oct 29, 2016
7f1ef77
edited read me
Oct 29, 2016
7573260
whee
arpanrau Oct 29, 2016
c6c031c
image shows up
Oct 29, 2016
6df32ef
Merge branch 'master' of https://github.com/arpanrau/InteractiveProgr…
arpanrau Oct 29, 2016
9ddcfb8
Lander now flies!
arpanrau Oct 29, 2016
5be0ca5
updates to rotation
Oct 30, 2016
3eca740
Minor Changes made with sound
arpanrau Oct 30, 2016
fb71b2f
changing background
Oct 30, 2016
7dd2715
whee
arpanrau Oct 30, 2016
a0904e3
Merge branch 'master' of https://github.com/arpanrau/InteractiveProgr…
arpanrau Oct 30, 2016
6573e5f
Done
arpanrau Oct 30, 2016
e93db19
Cleaned up code and repository for final submit
arpanrau Oct 31, 2016
6721d28
Done
arpanrau Oct 31, 2016
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
161 changes: 161 additions & 0 deletions Mars_lander.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
"""MVC mars lander game
Land dragon on Mars with arrow keys"""

import pygame
import math

WindowWidth = 1366 # width of the program's window, in pixels
WindowHeight = 768 # height of the program's window, in pixels

class Lander(object):
"""Model of Lander, with attributes, X,Y,Rotation,Fuel,dX, and dY
and methods gravity_update ,roll, and fire_thrusters"""
def __init__(self):
self.X = 100 #Lander X coordinate in Pixels

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally, it's stylistically better to name your class attributes beginning with a lowercase letter. Generally, it's convention to name instances of a class with a lowercase letter, and name the class itself with an uppercase letter. In this case, X, Y, Fuel, etc. are all instances and not a class themselves, so they should all be lowercase

self.Y = 100 #Lander Y coordinate in Pixels
self.Rotation = 0 #Lander vertical axis orientation in degrees (from -180 to 180)
self.Fuel = 1200 #Lander fuel in milliseconds of thruster time
self.Dx = 100 #Lander velocity in pixels/second to the right
self.Dy = 0 #Lander velocity in pixels/second up
def thruster_fire_right(self,duration):
"Given a duration since last tick in ms and itself, translates right"
#Checks if there is fuel, if there is translates and updates remaining fuel
if self.Fuel >= 0:
self.Dx += 1000/duration
self.Fuel -= duration
def thruster_fire_left(self,duration):
"Given a duration since last tick in ms and itself, translates left"
#Checks if there is fuel, if there is translates and updates remaining fuel
if self.Fuel >= 0:
self.Dx -= 1000/duration
self.Fuel -= duration
def thruster_fire_up(self,duration):
"Given a duration since last tick in ms and itself, translates up"
#Checks if there is fuel, if there is translates and updates remaining fuel
if self.Fuel >= 0:
self.Dy -= 1000/duration
self.Fuel -= duration
def update(self,duration):
self.Dy += 1 * duration #Updates Dy to Account for gravity
self.X += self.Dx * duration/1000 #Updates X according to Dx
self.Y += self.Dy * duration/1000 #Updates Y according to DY
def reset(self):
" resets lander position and velocity to initial settings"
self.X = 100 #Lander X coordinate in Pixels
self.Y = 100 #Lander Y coordinate in Pixels
self.Rotation = 0 #Lander vertical axis orientation in degrees (from -180 to 180)
self.Fuel = 1200 #Lander fuel in milliseconds of thruster time
self.Dx = 100 #Lander velocity in pixels/second to the right
self.Dy = 0 #Lander velocity in pixels/second up

class LanderView(pygame.sprite.Sprite):
"""Handles display of lander"""
def __init__(self, model, surface):
self.surface = surface #surface to display sprite on
self.model = model #model that the sprite should display
pygame.sprite.Sprite.__init__(self)
#Load an image from a file and set the Sprite's rect attribute to the size of the image
self.image = pygame.image.load('lander.png')
self.image = self.image.convert_alpha()
self.rect = self.image.get_rect()
def update(self):
"""Update the position of this sprite by setting the values of rect.center.x and rect.center.y"""
self.rect.center = (self.model.X, self.model.Y)
def reset(self, outcome):
"Takes input of the outcome ('WIN' or 'LOSE' strings) and then flashes screen color to outcome state (red for lose, green for win)"
surface = self.surface
if outcome == 'WIN':
#If win, fill screen green, update display, and wait a second so user can see green box
pygame.draw.line(surface, (0,255,0), (WindowWidth/2, WindowHeight), (WindowWidth/2, 0), 1366)
pygame.display.flip()
pygame.display.update()
pygame.time.delay(1000)
elif outcome == 'LOSE':
#If lose, fill screen red, update display, and wait a second so user can see green box
pygame.draw.line(surface, (255,0,0), (WindowWidth/2, WindowHeight), (WindowWidth/2 ,0), 1366)
pygame.display.flip()
pygame.display.update()
pygame.time.delay(1000)


class Gauge(object):
"""handles display of fuel gauge"""
def __init__(self, model,surface):
"""sets model and view attributes based on model and view that is passed in"""
self.model = model
self.surface = surface
def draw(self):
"""draws gauge"""
model = self.model
surface = self.surface
#draws a red line for the fuel guage , arbitrarily scaled
pygame.draw.line(surface, (255,0,0), (35, (WindowHeight - 50)), (35, (WindowHeight - 50) - int(model.Fuel * .2)), 20)


class LanderController(object):
"""Controls key-presses to rotate lander and fire thrusters
(calls lander methods)"""
def __init__(self,model,view):
"""sets model and view attributes based on model and view that is passed in"""
self.model = model
self.view = view
def handle_update(self,duration):
"""controls model based on key presses,detects collison with the ground and commands "WIN" or "LOSE" reset"""
model = self.model
view = self.view
#On contact with ground, check velocity
if model.Y >= (WindowHeight - 90) and model.Dx <= 200 and model.Dy <= 200 and model.Dx >= -200 and model.Dy >= -10:
#if velocity is low enough, flash screen green and reset
view.reset("WIN")
model.reset()
elif model.Y >= (WindowHeight - 90):
#otherwise, flash screen red and reset
view.reset("LOSE")
model.reset()
else:
#if no contact with the ground,
keys=pygame.key.get_pressed()
#if A pressed, fire left
if keys[pygame.K_a]:
model.thruster_fire_left(duration)
#if D pressed, fire right
if keys[pygame.K_d]:
model.thruster_fire_right(duration)
#if W pressed, fire up
if keys[pygame.K_w]:
model.thruster_fire_up(duration)
model.update(duration)

def main():
"""Main function for the code"""
pygame.init() #start pygame
screen = pygame.display.set_mode((WindowWidth, WindowHeight)) #make screen of proper window size
lander = Lander() #initialize lander class
lander_view = LanderView(lander,screen) #initialize lander view with lander class
lander_sprite = pygame.sprite.Group(lander_view) #initialize lander sprite with lander view
gauge = Gauge(lander,screen) #initialize gauge object
controller = LanderController(lander,lander_view)#initialize controller object
clock = pygame.time.Clock() #start pygame clock to keep track of frame duration and framerate
background = pygame.image.load('background.jpg')
background = pygame.transform.smoothscale(background, (WindowWidth, WindowHeight)) # Use smoothscale() to stretch the background image to fit the entire window
running = True
while running == True:
clock.tick(50) #sets framerate to 50 fps
#handle pygame quit
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
controller.handle_update(20) #call controller update with duration of 20 ms (50 FPS)
#Draw game and update display
screen.blit(background,(0,0))
lander_sprite.clear(screen,background)
lander_sprite.update()
lander_sprite.draw(screen)
gauge.draw()
pygame.display.flip()
pygame.display.update()
pygame.quit()


if __name__ == '__main__':
main()
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# InteractiveProgramming

This is the base repo for the interactive programming project for Software Design, Fall 2016 at Olin College.
This is the code for a Mars Dragon capsule landing game.

This file is not a good project README.
Replace the contents of this file before you submit your project.
To get it to work you will need to install Python 2.7 as well as py.game - a module
for creating simple raster games using Python

You then run mars_lander_mvc.py to start and play the game

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be Mars_lander.py.


You use the a and d keys to control the sideways translation of the lander and the up

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should actually be the w key instead of the up arrow

arrow to thrust upwards. Thrust is a constant and you have unlimitted restarts
of the engines. TO successfully land you must contact the ground will traveling
at a velocity less then 100 px/second

Have fun!
Binary file added background.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added lander.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.