-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgravity.py
More file actions
102 lines (75 loc) · 2.65 KB
/
gravity.py
File metadata and controls
102 lines (75 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
Created on Tue Mar 4 18:28:41 2014
@author: anneubuntu
"""
import pygame, sys
from pygame.locals import *
class Jumpman(object):
"""
Basic jumpman object that stores a tuple position and a tuple velocity
"""
def __init__(self, position, velocity):
#position and velocity are tuples of 2 x and y components
self.position = position
self.velocity = velocity
def setPosition(self, position):
self.position = position
def getPosition(self):
return self.position
def setVelocity(self,velocity):
self.velocity = velocity
def getVelocity(self):
return self.velocity
def updatePosition(self, dt):
x = self.position[0] + self.velocity[0]*dt
y = self.position[1] + self.velocity[1]*dt
self.position = (x,y)
def jump(self):
vx = self.velocity[0]
vy = -100
self.velocity = (vx,vy)
def gravity(jumpman, dt):
"""
updates the velocity of the jumpman after time dt has passed
"""
acceleration = 50
speed = jumpman.getVelocity()[1] #jumpman velocity should be a tuple with x-comp and y-comp
yPos = jumpman.getPosition()[1] #position should be tuple of x-coor and y-coor of top right corner
dy = speed*dt + 0.5*acceleration*dt**2
jumpman.setVelocity((jumpman.getVelocity()[0], speed+(acceleration*dt)))
pygame.init()
FPS = 30 # frames per second setting
fpsClock = pygame.time.Clock()
# set up the window
WINDOWHEIGHT = 600
WINDOWWIDTH = 600
DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT), 0, 32)
pygame.display.set_caption('Gravity')
WHITE = (255, 255, 255)
BLACK = (0,0,0)
MAN_HEIGHT = 100.0
MAN_WIDTH = 50.0
man = Jumpman((0.0,100.0),(40.0,0.0))
pygame.draw.rect(DISPLAYSURF, WHITE, (man.getPosition()[0], man.getPosition()[1], MAN_WIDTH,MAN_HEIGHT))
while True: # the main game loop
DISPLAYSURF.fill(BLACK)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == KEYDOWN:
if event.key == K_SPACE:
man.jump()
dt = 1.0/FPS
#accounts for collision with botton of window
man.updatePosition(dt)
if(man.getPosition()[1] < (WINDOWHEIGHT-MAN_HEIGHT)):
gravity(man,dt)
else:
man.setPosition((man.getPosition()[0],(WINDOWHEIGHT-MAN_HEIGHT)))
man.setVelocity((0.0,0.0))
#draws a new jumpman rectangel
pygame.draw.rect(DISPLAYSURF, WHITE, (man.getPosition()[0], man.getPosition()[1], MAN_WIDTH, MAN_HEIGHT))
pygame.display.update()
fpsClock.tick(FPS)