forked from sd17fall/InteractiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
124 lines (102 loc) · 4.9 KB
/
models.py
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import os, sys
import pygame
from config import *
class Obstacle:
"""A square obstacle, defined by its top left hand coordinate, length, and color.
Also takes in screen as an argument to draw the obstacle."""
def __init__(self, obs_x, obs_y, obs_len, screen, color):
"""Initialize the instance."""
self.obs_x = obs_x # top left hand x coordinate
self.obs_y = obs_y # top left hand y coordinate
self.obs_len = obs_len # side length
self.screen = screen # game screen
self.color = color # color of obstacle
def __repr__(self):
return 'Obstacle({}, {}, {}, {})'.format(self.obs_x, self.obs_y, self.obs_len, self.screen)
def draw(self):
"""Draw osbstacle based on top left hand coordinate and length."""
pygame.draw.rect(self.screen, colors[self.color], [self.obs_x, self.obs_y, self.obs_len, self.obs_len])
def move_forward(self):
"""Update horizontal location of obstacle."""
self.obs_x -= 20
def is_gone(self):
"""Check if obstacle is completely off screen."""
return self.obs_x < -self.obs_len
class Player:
"""A square player, defined by its top left hand coordinate and length.
Also takes in screen as an argument to draw the player."""
def __init__(self, play_x, play_y, play_len, screen):
"""Initialize the instance."""
self.play_x = play_x # top left hand x coordinate
self.play_y = play_y # top left hand y coordinate
self.play_len = play_len # side length
self.screen = screen # game screen
self.speed = 10 # right/left speed
self.jumpInProgress = False # initialize to False
self.v = 7.5 # "velocity" for jump
self.m = 2.5 # "mass" for jump
self.floor = play_y # location of player before jump, used for comparison
def draw(self):
"""Draw player based on top left hand coordinate and length."""
pygame.draw.rect(self.screen, colors['WHITE'], [self.play_x, self.play_y, self.play_len, self.play_len])
def move_right(self):
"""Update horizontal location of player after moving right."""
if self.play_x < 300:
self.play_x += self.speed
def move_left(self):
"""Update horizontal location of player after moving left."""
if self.play_x > 0:
self.play_x -= self.speed
def jump(self):
"""Set jumping status."""
self.jumpInProgress = True
def update(self):
"""Update height of player during jump."""
if self.jumpInProgress:
# change in height = "mass" times "velocity"
dy = self.m * self.v
# subtract height by change in height
self.play_y -= dy
# decrease velocity
self.v -= .75
# stop jumping if player has landed
if self.play_y >= self.floor:
# prevent player from falling through the floor
self.play_y = self.floor
# no longer jumping
self.jumpInProgress = False
# reset velocity
self.v = 7.5
def is_collide(self, obs_x, obs_y, obs_len):
"""Check collision of player with obstacle."""
# set coordinates for top left hand corner (0) and bottom right hand corner (1) of obstacle
obs_x0 = obs_x
obs_x1 = obs_x + obs_len
obs_y0 = obs_y
obs_y1 = obs_y + obs_len
# and of player
play_x0 = self.play_x
play_x1 = self.play_x + self.play_len
play_y0 = self.play_y
play_y1 = self.play_y + self.play_len
# check if player coordinates within obstacle coordinates
if (obs_x0 <= play_x0 <= obs_x1 or obs_x0 <= play_x1 <= obs_x1) and (obs_y0 <= play_y0 < obs_y1 or obs_y0 < play_y1 <= obs_y1):
return True
class StaminaBar:
"""A stamina bar, defined by its starting location and color.
Also takes in screen as an argument to draw the stamina bar."""
def __init__(self, screen, start, color):
self.screen = screen # game screen
self.start = start # starting location of stamina bar
self.color = color # color of stamina bar
self.bars = 100 # initialize number of health bars
def draw(self):
"""Draw stamina bar based on color, starting location, and number of health bars."""
pygame.draw.rect(self.screen, colors[self.color], [self.start, 20, self.bars, 10])
def decrease_bar(self, num_bars):
"""Decrease health bar by num_bars."""
self.bars -= num_bars
def increase_bar(self, speed = 1):
"""Increase health bar continuously if number of bars is lower than 100."""
if self.bars < 100:
self.bars += 1 * speed