-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhysics.py
90 lines (71 loc) · 2.83 KB
/
Physics.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
import pygame
from graphics import load_image
from config import GROUND_HEIGHT
"""Physics.py"""
"""
This file is part of The Last Caturai.
The Last Caturai is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
The Last Caturai is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Foobar. If not, see <http://www.gnu.org/licenses/>.
"""
"""Class that implement the physics of particles, such
as characters, throwing weapons, etc."""
class Physics(pygame.sprite.Sprite):
def __init__(self, img_path, position):
super().__init__()
# measures the change on X and Y
self.__position_y__, self.__position_x__ = 0, 0
# Fetch the rectangle object that has the dimensions of the image
# Update the position of this object by setting the values of rect.x and rect.y
self.image = load_image(img_path, True)
self.rect = self.image.get_rect()
self.position = position
# speed component of the particle
self.x_speed_vector_ = 0
self.y_speed_vector_ = 0
self.on_ground = True
self.change_index = 0
self.direction='D'
self.index = 0
self.velocity = 3
self.world_shift = 0
# Set the value of the speed vector
def change_x_speed_vector(self, speed):
self.x_speed_vector_ = abs(speed)
# Set the angle of the movement in radians
def change_y_speed_vector(self, angle):
self.y_speed_vector_ = angle
def move_right(self):
self.direction='D'
self.x_speed_vector_ = self.velocity
self.change_index = 1
def move_left(self):
self.direction='I'
self.x_speed_vector_ = -self.velocity
self.change_index = 1
def stop_moving(self):
self.x_speed_vector_ = 0
self.y_speed_vector_ = 0
self.change_index = 0
def gravity(self):
if self.y_speed_vector_ < 0:
self.y_speed_vector_ += 1
else:
self.y_speed_vector_ += 1
if self.rect.y >= GROUND_HEIGHT:
self.y_speed_vector_ = 0
self.on_ground = True
self.rect.y = GROUND_HEIGHT
def update(self):
self.gravity()
self.rect.x += self.x_speed_vector_
self.rect.y += self.y_speed_vector_
self.index = ((self.rect.x + self.world_shift + self.change_index) // 30) % 2
self.position = (self.rect.x, self.rect.y)