From cdcbeef773df471159a46329f4381c818f868b00 Mon Sep 17 00:00:00 2001 From: dfgarciag Date: Wed, 2 Nov 2016 14:25:55 -0400 Subject: [PATCH] Added some type of jumping --- main.py | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/main.py b/main.py index 2ea25be..33e7ad1 100644 --- a/main.py +++ b/main.py @@ -26,13 +26,14 @@ """Model classes""" class Player(object): - def __init__(self,x=0,y=0,width=50,height=50,dx=1,shiftdx=0): + def __init__(self,x=0,y=0,width=50,height=50,dx=1,dy=0,shiftdx=0): # places player centered above the coordinate given self.x = x self.y = y-height self.width = width self.height = height self.dx = dx + self.dy = dy self.shiftdx = shiftdx def train_wreck(self, train): @@ -44,6 +45,9 @@ def shift_world(self): def go_back(self): return self.x < 130 + def hit_ground(self,ground): + return (self.y + self.height) > ground.y + 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 @@ -124,8 +128,8 @@ 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: - keys = pygame.key.get_pressed() # checking pressed keys if keys[pygame.K_LEFT]: if player.go_back(): model.x -= model.shiftdx @@ -137,6 +141,13 @@ def handle_event(self): else: model.x += model.dx + if keys[pygame.K_UP] and player.dy == 0: + player.dy = -2 + + + + + def main(): pygame.init() screen = pygame.display.set_mode((640,480)) @@ -172,10 +183,14 @@ def main(): running = True counter = 0 + + # variable to make speed lower delta_speed = .00005 # good one is .00005 while running == True: + print player.y, player.dy + # Pretty awful way to slow player down. counter += 1 # adjust this if it's running to slow. Sorry. if counter%5 == 0: @@ -190,8 +205,17 @@ def main(): player.dx = 0 running = False + if player.hit_ground(ground): + player.dy = 0 + player.y = ground.y - player.height + + # keep train moving train.step() + player.y += player.dy + if player.dy != 0: + player.dy += 0.01 + # decrease speed of player (and all things relative to it) for model in controlled_models: @@ -222,4 +246,4 @@ def main(): pygame.quit() if __name__ == '__main__': - main() \ No newline at end of file + main()