代码拉取完成,页面将自动刷新
import math
import pygame
import Controller as con
vector = pygame.Vector2
# 定义角色类
class Agent(pygame.sprite.Sprite):
def __init__(self, mass,inertia,pos:vector, vel:vector,radius=10,color=(255,0,0)):
super().__init__()
self.fMass = mass
self.fInertia = inertia
self.fInertiaInverse = 1/self.fInertia
self.vPostion = pos
self.vVelocity = vel
self.fSpeed = vel.length()
self.vForces = vector(0, 0)
self.vMoveForce = vector(0,0)
self.radius = radius
self.color = color
self.image = pygame.Surface((self.radius * 2, self.radius * 2))
self.image.set_colorkey((0, 0, 0)) #将图像的黑色部分设置为透明。
pygame.draw.circle(self.image, self.color, (self.radius, self.radius), self.radius)
self.rect = self.image.get_rect()
self.rect.x = self.vPostion.x - self.radius
self.rect.y = self.vPostion.y - self.radius
# 更新速度和位置
def update(self, force:vector):
self.vForces = force + self.vMoveForce
self.vVelocity += self.vForces / self.fMass
self.rigid_resist()
self.vPostion += self.vVelocity
self.rect.x = self.vPostion.x - self.radius
self.rect.y = self.vPostion.y - self.radius
def rigid_calc_resist(self, mode: int):
if (mode == -1):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * math.sqrt(abs(self.vVelocity.x))
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x + self.fInertiaInverse * math.sqrt(abs(self.vVelocity.x))
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * math.sqrt(abs(self.vVelocity.y))
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y + self.fInertiaInverse * math.sqrt(abs(self.vVelocity.y))
if (mode == 0):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y
if (mode == 1):
if (self.vVelocity.x > 0): # 阻力计算
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 2
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x + self.fInertiaInverse * self.vVelocity.x ** 2
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 2
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y + self.fInertiaInverse * self.vVelocity.y ** 2
if (mode == 2): # 如果速度小于1,反而会很难停下来
if (self.vVelocity.x > 0): # 注意符号
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 3
if (self.vVelocity.x < 0):
self.vVelocity.x = self.vVelocity.x - self.fInertiaInverse * self.vVelocity.x ** 3
if (self.vVelocity.y > 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 3
if (self.vVelocity.y < 0):
self.vVelocity.y = self.vVelocity.y - self.fInertiaInverse * self.vVelocity.y ** 3
def rigid_resist(self):
if self.fSpeed > 30:
self.rigid_calc_resist(2)
elif self.fSpeed >= 5:
self.rigid_calc_resist(1)
elif self.fSpeed >= 0.5:
self.rigid_calc_resist(0)
else:
self.rigid_calc_resist(-1)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。