Dateien nach "/" hochladen
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
|||||||
|
import random
|
||||||
|
|
||||||
|
sixes = 0
|
||||||
|
amount = 10000000
|
||||||
|
for i in range(amount):
|
||||||
|
dice = random.randint(1, 6)
|
||||||
|
if dice == 6:
|
||||||
|
sixes += 1
|
||||||
|
print(sixes / i)
|
||||||
|
|
||||||
+25
@@ -0,0 +1,25 @@
|
|||||||
|
import random
|
||||||
|
done = False
|
||||||
|
biggestNumber = 100
|
||||||
|
|
||||||
|
number = random.randint(1, biggestNumber)
|
||||||
|
guess = int(input("Rate eine Zahl\n"))
|
||||||
|
tries = 0
|
||||||
|
|
||||||
|
|
||||||
|
def tryNumber(guess):
|
||||||
|
global done
|
||||||
|
global tries
|
||||||
|
tries += 1
|
||||||
|
|
||||||
|
if guess < number:
|
||||||
|
guess = int(input("Die Zahl ist größer\n"))
|
||||||
|
elif guess > number:
|
||||||
|
guess = int(input("Die Zahl ist kleiner\n"))
|
||||||
|
elif guess == number:
|
||||||
|
print("Die Zahl ist richtig!\n Du hast ", tries, " Versuche gebraucht.")
|
||||||
|
done = True
|
||||||
|
if done == False:
|
||||||
|
tryNumber(guess)
|
||||||
|
|
||||||
|
tryNumber(guess)
|
||||||
+22
@@ -0,0 +1,22 @@
|
|||||||
|
grades = []
|
||||||
|
done = False
|
||||||
|
|
||||||
|
print("Gib deine Punkte ein. Wenn du fertig bist gebe f ein")
|
||||||
|
|
||||||
|
while done == False:
|
||||||
|
try:
|
||||||
|
grades.append(int(input("")))
|
||||||
|
except ValueError:
|
||||||
|
print("Noteneingabe beendet")
|
||||||
|
done = True
|
||||||
|
|
||||||
|
schnitt = int(input("Welchen Notenschnitt willst du erreichen?\n"))
|
||||||
|
|
||||||
|
average = sum(grades) / len(grades)
|
||||||
|
|
||||||
|
needed = (schnitt - average) * len(grades)
|
||||||
|
|
||||||
|
if needed > 0:
|
||||||
|
print("Du brauchst noch ", needed, " Punkte")
|
||||||
|
if needed <= 0:
|
||||||
|
print("Du hast bereits genug Punkte")
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
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)
|
||||||
@@ -0,0 +1,282 @@
|
|||||||
|
import pygame
|
||||||
|
import random
|
||||||
|
import time
|
||||||
|
from kart import Kart, Item
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Einstellungen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
WIDTH = 1000
|
||||||
|
HEIGHT = 700
|
||||||
|
FPS = 60
|
||||||
|
|
||||||
|
BACKGROUND = (40, 40, 40)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Hilfsfunktion zum Zeichnen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
def draw_kart(screen, kart):
|
||||||
|
# Kart als Rechteck
|
||||||
|
width = 40
|
||||||
|
height = 25
|
||||||
|
|
||||||
|
surface = pygame.Surface((width, height), pygame.SRCALPHA)
|
||||||
|
pygame.draw.rect(surface, kart.color, (0, 0, width, height))
|
||||||
|
|
||||||
|
# Richtung des Karts
|
||||||
|
rotated = pygame.transform.rotate(surface, -kart.rotation)
|
||||||
|
|
||||||
|
rect = rotated.get_rect(
|
||||||
|
center=(int(kart.position[0]), int(kart.position[1]))
|
||||||
|
)
|
||||||
|
|
||||||
|
screen.blit(rotated, rect)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Pool zeichnen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
def draw_pool(screen, pool):
|
||||||
|
pygame.draw.ellipse(
|
||||||
|
screen,
|
||||||
|
pool.color,
|
||||||
|
(
|
||||||
|
int(pool.position[0] - pool.size),
|
||||||
|
int(pool.position[1] - pool.size / 2),
|
||||||
|
int(pool.size * 2),
|
||||||
|
int(pool.size)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Main
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
def main():
|
||||||
|
pygame.init()
|
||||||
|
|
||||||
|
screen = pygame.display.set_mode((WIDTH, HEIGHT))
|
||||||
|
pygame.display.set_caption("Kart Game")
|
||||||
|
|
||||||
|
clock = pygame.time.Clock()
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Karts erzeugen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
kart1 = Kart(
|
||||||
|
"Player 1",
|
||||||
|
(255, 50, 50),
|
||||||
|
acceleration=0.2,
|
||||||
|
vMax=5,
|
||||||
|
handling=4
|
||||||
|
)
|
||||||
|
|
||||||
|
kart2 = Kart(
|
||||||
|
"Player 2",
|
||||||
|
(50, 100, 255),
|
||||||
|
acceleration=0.2,
|
||||||
|
vMax=5,
|
||||||
|
handling=4
|
||||||
|
)
|
||||||
|
|
||||||
|
# Startpositionen
|
||||||
|
kart1.position = [250, 350]
|
||||||
|
kart1.rotation = 0
|
||||||
|
|
||||||
|
kart2.position = [750, 350]
|
||||||
|
kart2.rotation = 180
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Pool
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
pool = None
|
||||||
|
|
||||||
|
# Zeitpunkt, wann der nächste Pool erscheint
|
||||||
|
nextPoolTime = time.time() + random.uniform(3, 8)
|
||||||
|
|
||||||
|
running = True
|
||||||
|
|
||||||
|
while running:
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Events
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
for event in pygame.event.get():
|
||||||
|
if event.type == pygame.QUIT:
|
||||||
|
running = False
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Pool erzeugen / entfernen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
currentTime = time.time()
|
||||||
|
|
||||||
|
# Wenn momentan kein Pool existiert
|
||||||
|
# und die Wartezeit vorbei ist
|
||||||
|
if pool is None and currentTime >= nextPoolTime:
|
||||||
|
|
||||||
|
pool = Item("pool")
|
||||||
|
pool.typ()
|
||||||
|
|
||||||
|
# Zufällige Position
|
||||||
|
pool.position = [
|
||||||
|
random.randint(80, WIDTH - 80),
|
||||||
|
random.randint(80, HEIGHT - 80)
|
||||||
|
]
|
||||||
|
|
||||||
|
# Zufällige Größe
|
||||||
|
pool.size = random.randint(40, 80)
|
||||||
|
|
||||||
|
# Pool bleibt unterschiedlich lange sichtbar
|
||||||
|
poolEndTime = currentTime + random.uniform(3, 7)
|
||||||
|
|
||||||
|
# Pool verschwinden lassen
|
||||||
|
if pool is not None and currentTime >= poolEndTime:
|
||||||
|
|
||||||
|
pool = None
|
||||||
|
|
||||||
|
# Unregelmäßige Zeit bis zum nächsten Pool
|
||||||
|
nextPoolTime = currentTime + random.uniform(3, 10)
|
||||||
|
|
||||||
|
keys = pygame.key.get_pressed()
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Kart 1
|
||||||
|
# WASD + E
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
# W = Gas
|
||||||
|
if keys[pygame.K_w]:
|
||||||
|
kart1.acc(False)
|
||||||
|
|
||||||
|
# S = Rückwärts / Bremsen
|
||||||
|
elif keys[pygame.K_s]:
|
||||||
|
kart1.acc(True)
|
||||||
|
|
||||||
|
# Wenn kein Gas/Bremse -> langsam abbremsen
|
||||||
|
else:
|
||||||
|
kart1.speed *= 0.98
|
||||||
|
|
||||||
|
# A = links
|
||||||
|
if keys[pygame.K_a]:
|
||||||
|
kart1.turn(-1)
|
||||||
|
|
||||||
|
# D = rechts
|
||||||
|
if keys[pygame.K_d]:
|
||||||
|
kart1.turn(1)
|
||||||
|
|
||||||
|
# E = Rammen
|
||||||
|
if keys[pygame.K_e]:
|
||||||
|
kart1.ram(kart2)
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Kart 2
|
||||||
|
# IJKL + O
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
# I = Gas
|
||||||
|
if keys[pygame.K_i]:
|
||||||
|
kart2.acc(False)
|
||||||
|
|
||||||
|
# K = Rückwärts / Bremsen
|
||||||
|
elif keys[pygame.K_k]:
|
||||||
|
kart2.acc(True)
|
||||||
|
|
||||||
|
else:
|
||||||
|
kart2.speed *= 0.98
|
||||||
|
|
||||||
|
# J = links
|
||||||
|
if keys[pygame.K_j]:
|
||||||
|
kart2.turn(-1)
|
||||||
|
|
||||||
|
# L = rechts
|
||||||
|
if keys[pygame.K_l]:
|
||||||
|
kart2.turn(1)
|
||||||
|
|
||||||
|
# O = Rammen
|
||||||
|
if keys[pygame.K_o]:
|
||||||
|
kart2.ram(kart1)
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Position aktualisieren
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
kart1.pos()
|
||||||
|
kart2.pos()
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Bildschirm verlassen?
|
||||||
|
# -> auf andere Seite wechseln
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
# Abstand zum Bildschirmrand
|
||||||
|
border = 20
|
||||||
|
|
||||||
|
for kart in [kart1, kart2]:
|
||||||
|
|
||||||
|
# Linker Rand
|
||||||
|
if kart.position[0] < border:
|
||||||
|
kart.position[0] = border
|
||||||
|
|
||||||
|
# Rechter Rand
|
||||||
|
if kart.position[0] > WIDTH - border:
|
||||||
|
kart.position[0] = WIDTH - border
|
||||||
|
|
||||||
|
# Oberer Rand
|
||||||
|
if kart.position[1] < border:
|
||||||
|
kart.position[1] = border
|
||||||
|
|
||||||
|
# Unterer Rand
|
||||||
|
if kart.position[1] > HEIGHT - border:
|
||||||
|
kart.position[1] = HEIGHT - border
|
||||||
|
|
||||||
|
# -------------------------
|
||||||
|
# Zeichnen
|
||||||
|
# -------------------------
|
||||||
|
|
||||||
|
screen.fill(BACKGROUND)
|
||||||
|
|
||||||
|
# Pool zeichnen
|
||||||
|
if pool is not None:
|
||||||
|
draw_pool(screen, pool)
|
||||||
|
|
||||||
|
draw_kart(screen, kart1)
|
||||||
|
draw_kart(screen, kart2)
|
||||||
|
|
||||||
|
# Namen anzeigen
|
||||||
|
font = pygame.font.Font(None, 28)
|
||||||
|
|
||||||
|
text1 = font.render(
|
||||||
|
f"{kart1.name}: {kart1.speed:.1f}",
|
||||||
|
True,
|
||||||
|
kart1.color
|
||||||
|
)
|
||||||
|
|
||||||
|
text2 = font.render(
|
||||||
|
f"{kart2.name}: {kart2.speed:.1f}",
|
||||||
|
True,
|
||||||
|
kart2.color
|
||||||
|
)
|
||||||
|
|
||||||
|
screen.blit(text1, (20, 20))
|
||||||
|
screen.blit(text2, (20, 50))
|
||||||
|
|
||||||
|
pygame.display.flip()
|
||||||
|
|
||||||
|
# FPS
|
||||||
|
clock.tick(FPS)
|
||||||
|
|
||||||
|
pygame.quit()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main()
|
||||||
Reference in New Issue
Block a user