.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
from typing import Self, override
|
||||
|
||||
import items
|
||||
import pygame
|
||||
from input import Input
|
||||
from items import Item
|
||||
from pygame.math import clamp
|
||||
|
||||
Vec2 = pygame.Vector2
|
||||
|
||||
class Kart:
|
||||
def __init__(self, name: str, pos: Vec2, vel: Vec2, dir: float, speed_max: float):
|
||||
self.name: str = name
|
||||
self.pos: Vec2 = pos
|
||||
self.vel: Vec2 = vel
|
||||
self.dir: float = dir
|
||||
self.speed_max: float = speed_max
|
||||
|
||||
@override
|
||||
def __str__(self) -> str:
|
||||
return f"Kart {{ name: {self.name}, pos: {self.pos}, vel: {self.vel}, dir: {self.dir}, speed_max: {self.speed_max}}}"
|
||||
|
||||
def tick(self, input: Input):
|
||||
input_x = input.horizontal()
|
||||
input_y = input.vertical()
|
||||
assert abs(input_x) < 1
|
||||
assert abs(input_y) < 1
|
||||
|
||||
acc_amount = input_y * 2
|
||||
self.vel += Vec2(acc_amount, 0).rotate(self.dir)
|
||||
steer = input_x * 2
|
||||
self.dir += steer
|
||||
|
||||
self.vel.clamp_magnitude_ip(self.speed_max)
|
||||
self.pos += self.vel
|
||||
|
||||
def ram(self, other: Self):
|
||||
"""
|
||||
swap the two kart's velocities (not physically accurate)
|
||||
"""
|
||||
temp = self.vel
|
||||
self.vel = other.vel
|
||||
other.vel = temp
|
||||
|
||||
def consume_item(self, item: Item):
|
||||
if item.kind == items.ITEM_BANANA:
|
||||
self.speed_max += 1
|
||||
elif item.kind == items.ITEM_BANANA:
|
||||
self.speed_max -= 1
|
||||
|
||||
self.speed_max = clamp(self.speed_max, 0, 100)
|
||||
Reference in New Issue
Block a user