84 lines
2.3 KiB
Python
84 lines
2.3 KiB
Python
import math
|
|
import random
|
|
import time
|
|
|
|
class Kart:
|
|
|
|
def __init__(self, name, color, acceleration, vMax, handling):
|
|
self.vMax = vMax
|
|
self.name = name
|
|
self.color = color
|
|
self.speed = 0
|
|
self.rotation = 0
|
|
self.handling = handling
|
|
self.position = [0, 0]
|
|
self.acceleration = acceleration
|
|
self.lastRam = 0
|
|
|
|
def __str__(self):
|
|
return "Das Kart heißt:" + self.name
|
|
|
|
def acc(self, brk): #brk is true or false
|
|
if brk == False:
|
|
if self.speed < self.vMax:
|
|
self.speed += self.acceleration
|
|
elif brk:
|
|
if self.speed > -(self.vMax / 2):
|
|
self.speed -= self.acceleration * 0.5
|
|
else:
|
|
if self.speed != 0:
|
|
self.speed -= self.speed / 50
|
|
|
|
def turn(self, dir): #dir = 1 or -1
|
|
self.rotation += dir * self.handling
|
|
|
|
def pos(self):
|
|
self.position[0] += math.cos(math.radians(self.rotation)) * self.speed
|
|
self.position[1] += math.sin(math.radians(self.rotation)) * self.speed
|
|
|
|
def ram(self, other):
|
|
|
|
current = time.time()
|
|
|
|
if current - self.lastRam < 1:
|
|
return
|
|
|
|
self.lastRam = current
|
|
|
|
dx = self.position[0] - other.position[0]
|
|
dy = self.position[1] - other.position[1]
|
|
|
|
distance = math.sqrt(dx**2 + dy**2)
|
|
|
|
if distance <= 200 and distance > 0:
|
|
|
|
# Richtung vom anderen Kart zum eigenen Kart
|
|
direction_x = dx / distance
|
|
direction_y = dy / distance
|
|
|
|
# Beide Karts bekommen eine schnelle Bewegung
|
|
# aufeinander zu bzw. vom Zusammenstoß weg.
|
|
ramSpeed = 12
|
|
|
|
self.position[0] += direction_x * ramSpeed
|
|
self.position[1] += direction_y * ramSpeed
|
|
|
|
other.position[0] -= direction_x * ramSpeed
|
|
other.position[1] -= direction_y * ramSpeed
|
|
|
|
# Zufällige Drehung wie vorher
|
|
randrot = random.randint(-20, 20)
|
|
|
|
self.rotation += randrot
|
|
other.rotation -= randrot
|
|
|
|
|
|
class Item:
|
|
def __init__(self, type):
|
|
self.type = type
|
|
self.color = (255, 255, 255)
|
|
|
|
def typ(self):
|
|
if self.type == "pool":
|
|
self.color = (0, 0, 200)
|