From c633d3f06c2faea5c8da18171ca6e9ab3aeec258 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Wed, 8 Mar 2017 22:07:35 -0500 Subject: [PATCH 01/15] playfile now both falls and jumps --- main.py | 2 +- playfile.py | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/main.py b/main.py index b262cf5..b97eeca 100644 --- a/main.py +++ b/main.py @@ -159,7 +159,7 @@ def handle_event(self): model.x += model.shiftdx else: model.x += model.dx - if keys[pygame.K_UP] and player.dy == 0: + if keys[pygame.K_UP]: player.dy = player.jumpdy def main(): diff --git a/playfile.py b/playfile.py index 3abb265..51840a8 100644 --- a/playfile.py +++ b/playfile.py @@ -47,7 +47,13 @@ def go_back(self): return self.x < 130 def hit_ground(self,ground): - return (self.y + self.height) > ground.y + return (self.y + self.height) > ground.y and self.x < (ground.x+ground.width) and self.x > ground.x + + def fall_to_death(self): + return self.y > 480 + + def on_ground(self,ground): + return self.x < (ground.x+ground.width) and self.x > ground.x class PainTrain(object): def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): @@ -151,7 +157,7 @@ def main(): # models # level models: - ground = Ground() + ground = Ground(width=700) #x=0? platform1 = Platform(10,10) platform2 = Platform(800,10) platform3 = Platform(1600,10) @@ -159,7 +165,7 @@ def main(): platform5 = Platform(2400,10) # player/NPC models: player = Player(300,300) - train = PainTrain(0,300) + train = PainTrain(0,300,constdx=0) #models = [train, player, ground, platform1] controlled_models = [player,train,ground,platform1,platform2,platform3,platform4,platform5] level_models = [ground,platform1] @@ -185,7 +191,7 @@ def main(): while running == True: # Pretty awful way to slow player down. - counter += 1 # adjust this if it's running to slow. Sorry. + counter += 1 # adjust this if it's running too slow. Sorry. if counter%5 == 0: controller.handle_event() @@ -193,14 +199,17 @@ def main(): if event.type == pygame.QUIT: running = False - if player.train_wreck(train): + if player.train_wreck(train) or player.fall_to_death(): train.constdx = 0 player.dx = 0 running = False if player.hit_ground(ground): player.dy = 0 - player.y = ground.y - player.height + #player.y = ground.y - player.height + + if not player.on_ground(ground) and player.dy==0: + player.dy = .001 # keep train moving From b152d3573d585987a92ad51f7bad7cbad7c49cb1 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Wed, 8 Mar 2017 22:09:56 -0500 Subject: [PATCH 02/15] fixed accidental change --- main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.py b/main.py index b97eeca..b262cf5 100644 --- a/main.py +++ b/main.py @@ -159,7 +159,7 @@ def handle_event(self): model.x += model.shiftdx else: model.x += model.dx - if keys[pygame.K_UP]: + if keys[pygame.K_UP] and player.dy == 0: player.dy = player.jumpdy def main(): From 46632978c5321cb66bdf1d6ef98effbef176b074 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Wed, 8 Mar 2017 22:13:14 -0500 Subject: [PATCH 03/15] playfile can actually only jump once, fixing now --- playfile.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/playfile.py b/playfile.py index 51840a8..c6d9832 100644 --- a/playfile.py +++ b/playfile.py @@ -157,7 +157,7 @@ def main(): # models # level models: - ground = Ground(width=700) #x=0? + ground = Ground(width=1500) #x=0? platform1 = Platform(10,10) platform2 = Platform(800,10) platform3 = Platform(1600,10) @@ -165,7 +165,7 @@ def main(): platform5 = Platform(2400,10) # player/NPC models: player = Player(300,300) - train = PainTrain(0,300,constdx=0) + train = PainTrain(0,300) #models = [train, player, ground, platform1] controlled_models = [player,train,ground,platform1,platform2,platform3,platform4,platform5] level_models = [ground,platform1] From f65f3286b64e374e108438b694db96f308d65089 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Wed, 8 Mar 2017 22:15:24 -0500 Subject: [PATCH 04/15] easy fix --- playfile.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/playfile.py b/playfile.py index c6d9832..5d8d5b5 100644 --- a/playfile.py +++ b/playfile.py @@ -206,7 +206,7 @@ def main(): if player.hit_ground(ground): player.dy = 0 - #player.y = ground.y - player.height + player.y = ground.y - player.height if not player.on_ground(ground) and player.dy==0: player.dy = .001 From bb6c7446cb36be8f4e7aeffee50875dc966d9cc5 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Sat, 18 Mar 2017 19:26:01 -0400 Subject: [PATCH 05/15] Update README.md --- README.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/README.md b/README.md index 85fefeb..b666011 100644 --- a/README.md +++ b/README.md @@ -4,3 +4,23 @@ This is the base repo for the interactive programming project for Software Desig To run the game, run the file main.py in your terminal. + +# Title +clear, descriptive, unambiguous, memorable + +# Description +What does it do, including some context + +# Authors +Include attribution for code we didn't write + +# Getting Started +- Download instructions, optionally including versioning information and/or checksums +- Installation instructions, inclduing any required dependencies. Assume users start with same environment we had Day 1 of clas.s Provide list of required packages/programs, along with helpful sudo apt-get install x y z on-liner. List additionally required Python packages in requirements.txt file. + +# Usage +- Provide examples of how to run the code and expected results +- Describe any required input/output files + +# License +Your final project represents a fair amount of your hard work, so you should think a bit about the terms on which you want to release it into the world. Check out http://choosealicense.com From 90735b41628fd1df38fc1587b20215af040ac8d7 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:05:00 -0400 Subject: [PATCH 06/15] Updated README --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 85fefeb..b7c4af8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,12 @@ -# InteractiveProgramming +# "Pain Train" the Video Game +### by Charlie Weiss and Diego Garcia -This is the base repo for the interactive programming project for Software Design, Fall 2016 at Olin College. +## What is this? +This is a video game created using Python's pygame library and an MVP (model-viewer-player) framework. It is a platform game with a player, a chaser, and some obstacles. The player loses by either being overtaken by the chaser, or by falling to their death. +## How does it work? -To run the game, run the file main.py in your terminal. + +## How do I play? +## Other questions +## License/Attribution From b265fcc33b7fd7e01727212990095b8966c7d167 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:18:56 -0400 Subject: [PATCH 07/15] testing code quotes --- README.md | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/README.md b/README.md index b7c4af8..98c073f 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,28 @@ This is a video game created using Python's pygame library and an MVP (model-viewer-player) framework. It is a platform game with a player, a chaser, and some obstacles. The player loses by either being overtaken by the chaser, or by falling to their death. ## How does it work? +The code is split into three collections of classes: The Model classes, the View classes, and the Controller classes. The functions in these classes are called by the main function (found at the bottom of the file), which runs the game with a while loop. Here's some more detail about those sections: +### Model Classes +These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: + +> class PainTrain(object): + def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): + # places train centered above coordinate given + self.x = x + self.y = y-height + self.width = width + self.height = height + self.constdx = constdx + self.dx = dx + self.shiftdx = shiftdx + + def step(self): + self.x += self.constdx + +### View Classes +### Controller Classes +### Main Function ## How do I play? ## Other questions From c24c83952acef7d92a963d55e440cef988c404c5 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:19:45 -0400 Subject: [PATCH 08/15] testing code quote --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 98c073f..e22647c 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The code is split into three collections of classes: The Model classes, the View ### Model Classes These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: -> class PainTrain(object): +class PainTrain(object): def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): # places train centered above coordinate given self.x = x From 9a4b334934b513e69b67994505b9fd0c09bd2f29 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:21:18 -0400 Subject: [PATCH 09/15] testing code quotes --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e22647c..20bed07 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The code is split into three collections of classes: The Model classes, the View ### Model Classes These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: -class PainTrain(object): +'''class PainTrain(object): def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): # places train centered above coordinate given self.x = x @@ -23,6 +23,7 @@ class PainTrain(object): def step(self): self.x += self.constdx +''' ### View Classes ### Controller Classes From 7b8a2648e4c9a40f9c09363fa5b11f5e70c491f7 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:21:57 -0400 Subject: [PATCH 10/15] testing code quotes --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 20bed07..82afff4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ The code is split into three collections of classes: The Model classes, the View ### Model Classes These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: -'''class PainTrain(object): +```class PainTrain(object): def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): # places train centered above coordinate given self.x = x @@ -23,7 +23,7 @@ These contain the definition of the models of each type of object in the game, a def step(self): self.x += self.constdx -''' +``` ### View Classes ### Controller Classes From 4142ae71f65ab267fc79a326bd972915405f87a8 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:22:38 -0400 Subject: [PATCH 11/15] testing code quotes --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 82afff4..444e864 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,8 @@ The code is split into three collections of classes: The Model classes, the View ### Model Classes These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: -```class PainTrain(object): +``` +class PainTrain(object): def __init__(self,x=0,y=0,width=200,height=200,constdx=.05,dx=0,shiftdx=-1): # places train centered above coordinate given self.x = x From 8ba43617e153b9d3240f005b8bc0854db0097cf1 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 14:28:58 -0400 Subject: [PATCH 12/15] update README --- README.md | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 444e864..35265bf 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ This is a video game created using Python's pygame library and an MVP (model-vie The code is split into three collections of classes: The Model classes, the View classes, and the Controller classes. The functions in these classes are called by the main function (found at the bottom of the file), which runs the game with a while loop. Here's some more detail about those sections: ### Model Classes -These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of a model class: +These contain the definition of the models of each type of object in the game, and their interactions with other objects or events. Each class defines things such as an object's postion, height, width, and distance traveled per loop. Here's an example of the chaser model class, named PainTrain: ``` class PainTrain(object): @@ -26,7 +26,32 @@ class PainTrain(object): self.x += self.constdx ``` -### View Classes +The classes you will find here are +- Player +- PainTrain +- Ground +- Platform + +### Viewer Classes +These classes project the information contained in the model classes onto your screen. Each contains an initial function to define the class, and a draw function that says what the object should look like. For consistency's sake, here's the example of the PainTrain's Viewer class: + +``` +class PainTrainView(object): + def __init__(self, model): + self.model = model + + def draw(self, surface): + model = self.model + # this takes (x,y,width,height) + pygame.draw.rect(surface,BLACK,(model.x,model.y,model.width,model.height)) +``` + +The classes you will find here are +- PlayerView +- PainTrainView +- GroundView +- ObstacleView + ### Controller Classes ### Main Function From 5dbfc2366ec597f6eec7eae4439514f57215e479 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 15:11:10 -0400 Subject: [PATCH 13/15] finished How Does It Work --- README.md | 45 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 35265bf..7cdb640 100644 --- a/README.md +++ b/README.md @@ -53,8 +53,51 @@ The classes you will find here are - ObstacleView ### Controller Classes +This is actually one class called the Controller class. It has an initial function to define it, and a handle_event function. This class interacts with the main function, which passes it an event. The handle_event function then determines which event it is (i.e., which key has been pressed), and says what to do in that case. Here's the class: + +``` +class Controller(object): + def __init__(self,models): + self.models = models + self.player = models[0] # make sure this aligns with controlled_models in main + + def handle_event(self): + # time passed isn't actually time based... based on while loop efficiency + player = self.player + models = self.models + keys = pygame.key.get_pressed() # checking pressed keys + for model in models: + if keys[pygame.K_LEFT]: + if player.go_back(): + model.x -= model.shiftdx + else: + model.x -= model.dx + if keys[pygame.K_RIGHT]: + if player.shift_world(): + model.x += model.shiftdx + else: + model.x += model.dx + + if keys[pygame.K_UP] and player.dy == 0: + player.dy = player.jumpdy +``` + +The class you will find here is +- Controller + ### Main Function +Now this function is the thing that pulls the whole game together, and thus, it is probably the most confusing. Here's what it does: + +- Initializes pygame and the screen +- defines objects and their views and puts them in appropriate arrays +- defines controller +- Runs main while loop, which updates object positions, draws the corresponding views, and checks for events. This stops running if the player dies or closes the window. +- If the player dies, it runs another while loop which displays the losing screen. + +And that's it! -## How do I play? +## So how do I play? ## Other questions +####It's running to slow, what can I do to fix it? +####Why do I always lose? ## License/Attribution From a159e049c14fb2c1c2cea02896a0fd785c8a2261 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 15:48:37 -0400 Subject: [PATCH 14/15] updated how you play --- README.md | 19 +++++++++++++++++-- playfile.py | 10 +++++----- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7cdb640..b4e28a1 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,22 @@ Now this function is the thing that pulls the whole game together, and thus, it And that's it! ## So how do I play? +Assuming you're working in Linux, it's really as simple as installing Pygame, downloading playfile.py, and calling it in Python using your terminal. This is what this will look like in your command window: + +First, +``` +pip install pygame +``` +Next, +``` +python playfile.py +``` +And look out, because now you're playing! + +If you run into some issues installing Pygame or even Python, [this is a nice guide to refer to](https://www.pygame.org/wiki/GettingStarted) + ## Other questions -####It's running to slow, what can I do to fix it? -####Why do I always lose? + +#### It's running too slow, what can I do to fix it? +#### Why do I always lose? ## License/Attribution diff --git a/playfile.py b/playfile.py index 5d8d5b5..e5c3c84 100644 --- a/playfile.py +++ b/playfile.py @@ -14,9 +14,6 @@ import pygame import math -# Images -gameover = pygame.image.load('gameover1.bmp') - # Colors BLACK = (0,0,0) WHITE = (255,255,255) @@ -155,6 +152,9 @@ def main(): pygame.init() screen = pygame.display.set_mode((640,480)) + # Images + gameover = pygame.image.load('gameover1.bmp').convert() + # models # level models: ground = Ground(width=1500) #x=0? @@ -181,7 +181,7 @@ def main(): views.append(ObstacleView(platform4)) views.append(ObstacleView(platform5)) - # TODO: Add controller + # controller controller = Controller(controlled_models) running = True counter = 0 @@ -191,7 +191,7 @@ def main(): while running == True: # Pretty awful way to slow player down. - counter += 1 # adjust this if it's running too slow. Sorry. + counter += 1 # adjust this if it's running too slow. A little jank, sorry. if counter%5 == 0: controller.handle_event() From 4f971b0da27dc76a75d8b8eb7af069a086ab0f95 Mon Sep 17 00:00:00 2001 From: charlievweiss Date: Thu, 23 Mar 2017 16:32:11 -0400 Subject: [PATCH 15/15] Updated everything but the license --- README.md | 24 ++++++++++++++++++++++-- playfile.py | 18 +++--------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index b4e28a1..12e762a 100644 --- a/README.md +++ b/README.md @@ -109,10 +109,30 @@ python playfile.py ``` And look out, because now you're playing! -If you run into some issues installing Pygame or even Python, [this is a nice guide to refer to](https://www.pygame.org/wiki/GettingStarted) +If you run into some issues installing Pygame or even Python, [this is a nice guide to refer to](https://www.pygame.org/wiki/GettingStarted). ## Other questions #### It's running too slow, what can I do to fix it? +Try lowering the mod on the counter in the first if statement of the main while loop. For example, try turning this: + +``` +while running == True: + counter += 1 + if counter%5 == 0: # adjust this + controller.handle_event() +``` +into this: +``` +while running == True: + counter += 1 + if counter%2 == 0: # adjust this + controller.handle_event() +``` +Beyond that, this really isn't the most efficient thing in the world right now... sorry about that. + #### Why do I always lose? -## License/Attribution +Life isn't fair, buddy. Neither is this. + +## License +Hmm. \ No newline at end of file diff --git a/playfile.py b/playfile.py index e5c3c84..ec7e101 100644 --- a/playfile.py +++ b/playfile.py @@ -1,15 +1,4 @@ -"""main.py - -objects: screen, player, follower - -TODO: -- train constant speed -- player slows down -- lose on collision -- flat level - -DONE: -- simple objects for player sprite""" +"""Pain Train the Video Game, by Charlie Weiss and Diego Garcia""" import pygame import math @@ -190,9 +179,8 @@ def main(): delta_speed = .00005 # good one is .00005 while running == True: - # Pretty awful way to slow player down. - counter += 1 # adjust this if it's running too slow. A little jank, sorry. - if counter%5 == 0: + counter += 1 + if counter%5 == 0: # adjust this if it's running too slow. A little jank, sorry. controller.handle_event() for event in pygame.event.get():