Untitled
unknown
plain_text
10 months ago
2.0 kB
3
Indexable
import pygame
import sys
import random
# Initialisiere Pygame
pygame.init()
# Bildschirmgröße definieren
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Virtuelle Stadt - Los Angeles Modell")
# Farben
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
BLUE = (0, 102, 255)
GREEN = (34, 139, 34)
# Framerate
clock = pygame.time.Clock()
FPS = 60
# Straßen
roads = [
((100, 0), (100, 600)), # Vertikale Straße
((0, 300), (800, 300)) # Horizontale Straße
]
# Gebäude
buildings = [
pygame.Rect(150, 50, 100, 100), # Gebäude 1
pygame.Rect(300, 400, 120, 120) # Gebäude 2
]
# Autos (Bewegung)
cars = [{"pos": [50, 280], "dir": (2, 0)}, # Richtung x
{"pos": [90, 50], "dir": (0, 2)}] # Richtung y
def draw_roads():
for road in roads:
pygame.draw.line(screen, GRAY, road[0], road[1], 10)
def draw_buildings():
for building in buildings:
pygame.draw.rect(screen, BLUE, building)
def draw_cars():
for car in cars:
pygame.draw.circle(screen, GREEN, (int(car["pos"][0]), int(car["pos"][1])), 8)
def update_cars():
for car in cars:
car["pos"][0] += car["dir"][0]
car["pos"][1] += car["dir"][1]
# Grenzen und Richtungswechsel
if car["pos"][0] > WIDTH or car["pos"][0] < 0:
car["dir"] = (-car["dir"][0], car["dir"][1])
if car["pos"][1] > HEIGHT or car["pos"][1] < 0:
car["dir"] = (car["dir"][0], -car["dir"][1])
# Haupt-Loop
running = True
while running:
screen.fill(WHITE)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# Zeichne Stadt
draw_roads()
draw_buildings()
draw_cars()
# Updates
update_cars()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
sys.exit()
Editor is loading...
Leave a Comment