/
Suplin3
/
PySimulation
Обзор
Документация
Войти
/
Suplin3
/
PySimulation
Код
Запросы
0
Задачи
Пакеты
0
Релизы
1
Аналитика
Безопасность
master
phisycs.py
115 строк
4 KB
Suplin3
Add requirements
26 сен 2024, 20:21
26 сен 2024, 20:21
fe16559
Код
Авторство
О чём код?
import pygame import math # constants g = 9.8 def degToRad(deg): return deg / 180 * math.pi # 1rad = 180/pi def radToDeg(rad): return rad * 180 / math.pi class Vector2: def __init__(self, x, y): self.x = x self.y = y def __str__(self): return str((self.x, self.y)) def __getitem__(self, key): if key == 0: return self.x elif key == 1: return self.y else: raise IndexError def __add__(self, other): if isinstance(other, Vector2): return Vector2(self.x + other.x, self.y + other.y) if isinstance(other, int) or isinstance(other, float): return Vector2(self.x + other, self.y + other) else: raise TypeError def __sub__(self, other): if isinstance(other, Vector2): return Vector2(self.x - other.x, self.y - other.y) if isinstance(other, int) or isinstance(other, float): return Vector2(self.x - other, self.y - other) else: raise TypeError def __mul__(self, other): if isinstance(other, Vector2): return Vector2(self.x * other.x, self.y * other.y) if isinstance(other, int) or isinstance(other, float): return Vector2(self.x * other, self.y * other) else: raise TypeError def __truediv__(self, other): if isinstance(other, Vector2): return Vector2(self.x / other.x, self.y / other.y) if isinstance(other, int) or isinstance(other, float): return Vector2(self.x / other, self.y / other) else: raise TypeError def dot(self, other): if isinstance(other, Vector2): return self.x * other.x + self.y * other.y else: raise TypeError def len(self): return (self.x ** 2 + self.y ** 2) ** 0.5 def norm(self): module = self.len() return Vector2(self.x / module, self.y / module) def radians(self, other): if isinstance(other, Vector2): return math.acos(self.norm().dot(other.norm())) else: raise TypeError class Ball: def __init__(self, pos: Vector2 = Vector2(10, 10), radius=1, color=(200, 200, 255)): self.pos = pos self.pos0 = pos self.color = color self.radius = radius self.speed = Vector2(0, 0) self.speed0 = self.speed self.accelerate = Vector2(0, 0) self.bouncing = 1 def draw(self, surface: pygame.Surface, coordAxes): pos1 = Vector2(coordAxes.gap + self.pos.x * coordAxes.oneStepPixels, surface.get_height() - (coordAxes.gap + self.pos.y * coordAxes.oneStepPixels)) pygame.draw.circle(surface, self.color, (pos1.x, pos1.y), self.radius * coordAxes.oneStepPixels) def calcPos(self, time): self.speed = self.speed0 + self.accelerate * time self.pos = self.pos0 + self.speed0 * time + self.accelerate * time ** 2 / 2 def throw(self, angle: float, force: float): self.speed0 = Vector2(force * math.cos(degToRad(angle)), force * math.sin(degToRad(angle))) self.accelerate = Vector2(0, -g) def getRect(self, coordAxes, surface): return pygame.Rect( (self.pos.x - self.radius) * coordAxes.oneStepPixels + coordAxes.gap, surface.get_height() - (self.pos.y + 3 * self.radius) * coordAxes.oneStepPixels + coordAxes.gap, 2 * self.radius * coordAxes.oneStepPixels, 2 * self.radius * coordAxes.oneStepPixels)