forked from Q2-Wl/FinnBestmann_Kart
406 lines
8.3 KiB
Python
406 lines
8.3 KiB
Python
import pygame
|
|
import math
|
|
import random
|
|
|
|
# ---------------------------------------------------------
|
|
# PYGAME INITIALISIERUNG
|
|
# ---------------------------------------------------------
|
|
|
|
pygame.init()
|
|
|
|
BREITE = 1200
|
|
HOEHE = 700
|
|
|
|
screen = pygame.display.set_mode((BREITE, HOEHE))
|
|
pygame.display.set_caption("Kart-Rennsimulation")
|
|
|
|
clock = pygame.time.Clock()
|
|
|
|
# Farben
|
|
WEISS = (255, 255, 255)
|
|
SCHWARZ = (20, 20, 20)
|
|
GRAU = (100, 100, 100)
|
|
DUNKELGRAU = (45, 45, 45)
|
|
GRUEN = (50, 150, 70)
|
|
ROT = (220, 50, 50)
|
|
BLAU = (50, 100, 230)
|
|
GELB = (240, 220, 50)
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# KART-KLASSE
|
|
# ---------------------------------------------------------
|
|
|
|
class Kart:
|
|
def __init__(self, name, farbe, v_max, geschw, handling, position, rammen):
|
|
self.name = name
|
|
self.farbe = farbe
|
|
self.v_max = v_max
|
|
self.geschw = geschw
|
|
self.handling = handling
|
|
self.driften_anzahl = 0
|
|
self.position = position
|
|
self.rammen_wert = rammen
|
|
# Pygame-Werte
|
|
self.x = 0
|
|
self.y = 0
|
|
self.winkel = 0
|
|
|
|
self.beschleunigungsstaerke = 10
|
|
self.bremsstaerke = 20
|
|
|
|
# -----------------------------------------------------
|
|
# BESCHLEUNIGEN
|
|
# -----------------------------------------------------
|
|
|
|
def beschleunigung(self):
|
|
self.geschw += 10
|
|
|
|
if self.geschw > self.v_max:
|
|
self.geschw = self.v_max
|
|
|
|
# -----------------------------------------------------
|
|
# BREMSEN
|
|
# -----------------------------------------------------
|
|
|
|
def bremsen(self):
|
|
self.geschw -= 20
|
|
|
|
if self.geschw < 0:
|
|
self.geschw = 0
|
|
|
|
# -----------------------------------------------------
|
|
# DRIFTEN
|
|
# -----------------------------------------------------
|
|
|
|
def driften(self):
|
|
self.driften_anzahl += 1
|
|
|
|
self.geschw -= 5
|
|
|
|
if self.geschw < 0:
|
|
self.geschw = 0
|
|
|
|
# -----------------------------------------------------
|
|
# RAMMEN
|
|
# -----------------------------------------------------
|
|
|
|
def rammen(self):
|
|
self.rammen_wert += 1
|
|
|
|
# -----------------------------------------------------
|
|
# KART ZEICHNEN
|
|
# -----------------------------------------------------
|
|
|
|
def zeichnen(self, surface):
|
|
|
|
breite = 45
|
|
hoehe = 28
|
|
|
|
# Kart als Rechteck erstellen
|
|
kart_surface = pygame.Surface((breite, hoehe), pygame.SRCALPHA)
|
|
|
|
# Farbe bestimmen
|
|
if self.farbe == "rot":
|
|
farbe = ROT
|
|
elif self.farbe == "blau":
|
|
farbe = BLAU
|
|
else:
|
|
farbe = WEISS
|
|
|
|
pygame.draw.rect(
|
|
kart_surface,
|
|
farbe,
|
|
(0, 0, breite, hoehe),
|
|
border_radius=8
|
|
)
|
|
|
|
# Front des Karts
|
|
pygame.draw.rect(
|
|
kart_surface,
|
|
SCHWARZ,
|
|
(breite - 8, 5, 5, hoehe - 10)
|
|
)
|
|
|
|
# Fenster
|
|
pygame.draw.rect(
|
|
kart_surface,
|
|
(80, 180, 220),
|
|
(15, 5, 14, 18),
|
|
border_radius=3
|
|
)
|
|
|
|
# Kart drehen
|
|
gedreht = pygame.transform.rotate(
|
|
kart_surface,
|
|
self.winkel
|
|
)
|
|
|
|
rechteck = gedreht.get_rect(
|
|
center=(self.x, self.y)
|
|
)
|
|
|
|
surface.blit(gedreht, rechteck)
|
|
|
|
# -----------------------------------------------------
|
|
# BEWEGUNG
|
|
# -----------------------------------------------------
|
|
|
|
def bewegen(self, links, rechts):
|
|
|
|
# Handling bestimmt, wie schnell das Kart lenkt
|
|
lenkgeschwindigkeit = self.handling * 0.08
|
|
|
|
if self.geschw > 0:
|
|
|
|
if links:
|
|
self.winkel += lenkgeschwindigkeit
|
|
|
|
if rechts:
|
|
self.winkel -= lenkgeschwindigkeit
|
|
|
|
# Geschwindigkeit in Pixel umrechnen
|
|
bewegung = self.geschw * 0.08
|
|
|
|
rad = math.radians(self.winkel)
|
|
|
|
self.x += math.cos(rad) * bewegung
|
|
self.y -= math.sin(rad) * bewegung
|
|
|
|
# Begrenzung des Spielfeldes
|
|
self.x = max(80, min(BREITE - 80, self.x))
|
|
self.y = max(80, min(HOEHE - 80, self.y))
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# KARTS ERSTELLEN
|
|
# ---------------------------------------------------------
|
|
|
|
kart1 = Kart(
|
|
"Magic",
|
|
"rot",
|
|
500,
|
|
100,
|
|
8,
|
|
1,
|
|
1
|
|
)
|
|
|
|
kart2 = Kart(
|
|
"Luigi",
|
|
"blau",
|
|
400,
|
|
120,
|
|
6,
|
|
2,
|
|
1
|
|
)
|
|
|
|
# Startpositionen
|
|
kart1.x = 300
|
|
kart1.y = 350
|
|
kart1.winkel = 0
|
|
|
|
kart2.x = 300
|
|
kart2.y = 430
|
|
kart2.winkel = 0
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# SCHRIFTEN
|
|
# ---------------------------------------------------------
|
|
|
|
font_gross = pygame.font.SysFont("Arial", 32, bold=True)
|
|
font_normal = pygame.font.SysFont("Arial", 22)
|
|
font_klein = pygame.font.SysFont("Arial", 18)
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# RENNSTRECKE
|
|
# ---------------------------------------------------------
|
|
|
|
def strecke_zeichnen():
|
|
|
|
# Gras
|
|
screen.fill(GRUEN)
|
|
|
|
# Äußerer Straßenbereich
|
|
pygame.draw.rect(
|
|
screen,
|
|
DUNKELGRAU,
|
|
(80, 80, BREITE - 160, HOEHE - 160),
|
|
border_radius=100
|
|
)
|
|
|
|
# Innerer Bereich
|
|
pygame.draw.rect(
|
|
screen,
|
|
GRUEN,
|
|
(200, 180, BREITE - 400, HOEHE - 360),
|
|
border_radius=70
|
|
)
|
|
|
|
# Mittellinie
|
|
pygame.draw.rect(
|
|
screen,
|
|
(230, 230, 230),
|
|
(BREITE // 2 - 5, 80, 10, 100)
|
|
)
|
|
|
|
pygame.draw.rect(
|
|
screen,
|
|
(230, 230, 230),
|
|
(BREITE // 2 - 5, HOEHE - 180, 10, 100)
|
|
)
|
|
|
|
# Start-/Ziellinie
|
|
start_x = BREITE // 2 - 5
|
|
|
|
for y in range(180, 520, 30):
|
|
|
|
if (y // 30) % 2 == 0:
|
|
farbe = WEISS
|
|
else:
|
|
farbe = SCHWARZ
|
|
|
|
pygame.draw.rect(
|
|
screen,
|
|
farbe,
|
|
(start_x, y, 10, 30)
|
|
)
|
|
|
|
|
|
# ---------------------------------------------------------
|
|
# INFO-BEREICH
|
|
# ---------------------------------------------------------
|
|
|
|
def informationen_zeichnen():
|
|
|
|
# Hintergrund
|
|
pygame.draw.rect(
|
|
screen,
|
|
(25, 25, 25),
|
|
(0, 0, BREITE, 60)
|
|
)
|
|
|
|
# Kart 1
|
|
text1 = font_normal.render(
|
|
f"{kart1.name}: {int(kart1.geschw)} km/h",
|
|
True,
|
|
ROT
|
|
)
|
|
|
|
screen.blit(text1, (20, 18))
|
|
|
|
# Kart 2
|
|
text2 = font_normal.render(
|
|
f"{kart2.name}: {int(kart2.geschw)} km/h",
|
|
True,
|
|
BLAU
|
|
)
|
|
|
|
screen.blit(text2, (270, 18))
|
|
|
|
# Drift
|
|
text3 = font_klein.render(
|
|
f"Drifts: {kart1.driften_anzahl}",
|
|
True,
|
|
WEISS
|
|
)
|
|
|
|
screen.blit(text3, (520, 21))
|
|
|
|
text4 = font_klein.render(
|
|
f"Drifts: {kart2.driften_anzahl}",
|
|
True,
|
|
WEISS
|
|
)
|
|
|
|
screen.blit(text4, (650, 21))
|
|
|
|
|
|
|
|
def steuerung_zeichnen():
|
|
|
|
texte = [
|
|
"MAGIC: W = Gas | S = Bremse | A/D = Lenken | Leertaste = Drift",
|
|
"LUIGI: Pfeile = Fahren | R = Drift"
|
|
]
|
|
|
|
y = HOEHE - 65
|
|
|
|
for text in texte:
|
|
|
|
bild = font_klein.render(
|
|
text,
|
|
True,
|
|
WEISS
|
|
)
|
|
|
|
screen.blit(
|
|
bild,
|
|
(20, y)
|
|
)
|
|
|
|
y += 22
|
|
|
|
|
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
clock.tick(60)
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
if event.type == pygame.QUIT:
|
|
running = False
|
|
|
|
keys = pygame.key.get_pressed()
|
|
|
|
|
|
if keys[pygame.K_w]:
|
|
kart1.beschleunigung()
|
|
|
|
if keys[pygame.K_s]:
|
|
kart1.bremsen()
|
|
|
|
if keys[pygame.K_SPACE]:
|
|
kart1.driften()
|
|
|
|
kart1.bewegen(
|
|
keys[pygame.K_a],
|
|
keys[pygame.K_d]
|
|
)
|
|
|
|
|
|
if keys[pygame.K_UP]:
|
|
kart2.beschleunigung()
|
|
|
|
if keys[pygame.K_DOWN]:
|
|
kart2.bremsen()
|
|
|
|
if keys[pygame.K_r]:
|
|
kart2.driften()
|
|
|
|
kart2.bewegen(
|
|
keys[pygame.K_LEFT],
|
|
keys[pygame.K_RIGHT]
|
|
)
|
|
|
|
|
|
strecke_zeichnen()
|
|
|
|
kart1.zeichnen(screen)
|
|
kart2.zeichnen(screen)
|
|
|
|
informationen_zeichnen()
|
|
steuerung_zeichnen()
|
|
|
|
pygame.display.flip()
|
|
|
|
|
|
pygame.quit()
|