design ohne Zeitfunktion
This commit is contained in:
parent
29d5914047
commit
eb6d95bfdc
|
Before Width: | Height: | Size: 430 KiB After Width: | Height: | Size: 430 KiB |
|
Before Width: | Height: | Size: 37 KiB After Width: | Height: | Size: 37 KiB |
@ -7,16 +7,39 @@ import os
|
||||
# Country–Capital Memory Game
|
||||
# -------------------------------
|
||||
|
||||
CARD_BACK_COLOR = (100, 100, 200)
|
||||
CARD_FRONT_COLOR = (230, 230, 250)
|
||||
MATCH_COLOR = (120, 200, 120)
|
||||
TEXT_COLOR = (0, 0, 0)
|
||||
BG_COLOR = (50, 50, 80)
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
|
||||
CARD_FRONT_COLOR = (245, 246, 248)
|
||||
MATCH_COLOR = (160, 220, 160)
|
||||
TEXT_COLOR = (10, 30, 40)
|
||||
BG_COLOR = (10, 42, 83) # dunkles Blaugrün wie deine Startseite
|
||||
BUTTON_FILL = (18, 122, 138)
|
||||
BUTTON_BORDER = (255, 255, 255)
|
||||
FPS = 30
|
||||
# Design-Bild (für Kartenrückseite + optionalen Hintergrund)
|
||||
DESIGN_FILE = "GlobalHintergrund.png"
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT = 900, 600
|
||||
|
||||
# --- Absoluter Pfad zu deinem Logo ---
|
||||
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||
BACKGROUND_FILE = os.path.join(BASE_DIR, "GlobalHintergrund.png") # dein Kartenbild
|
||||
|
||||
# -------------------------------
|
||||
# Kartenrückseite = komplettes Bild
|
||||
# -------------------------------
|
||||
def build_card_back(image_path, size):
|
||||
"""Skaliert dein Logo-Bild so, dass es die Karte komplett bedeckt."""
|
||||
w, h = size
|
||||
surf = pygame.Surface((w, h), pygame.SRCALPHA).convert_alpha()
|
||||
|
||||
if os.path.exists(image_path):
|
||||
try:
|
||||
img = pygame.image.load(image_path).convert_alpha()
|
||||
img = pygame.transform.smoothscale(img, (w, h)) # volle Fläche
|
||||
surf.blit(img, (0, 0))
|
||||
except pygame.error as e:
|
||||
print(f"⚠️ Fehler beim Laden von {image_path}: {e}")
|
||||
surf.fill((127, 127, 200)) # Notfallfarbe
|
||||
else:
|
||||
print(f"⚠️ Bild nicht gefunden: {image_path}")
|
||||
surf.fill((127, 127, 200))
|
||||
return surf
|
||||
|
||||
|
||||
class MemoryGame:
|
||||
@ -36,21 +59,21 @@ class MemoryGame:
|
||||
self.awaiting_confirmation = False
|
||||
self.confirmation_result = None
|
||||
self.correct_answer_expected = None
|
||||
self.state = "continent" # continent → americas → difficulty → pairs → game
|
||||
self.state = "continent"
|
||||
self.buttons = []
|
||||
self.selected_continents = []
|
||||
self.level = None
|
||||
self.pair_count = 6 # Default
|
||||
self.pair_count = 6
|
||||
self.bg_image = None
|
||||
self.card_back = None
|
||||
|
||||
# -------------------------------
|
||||
# Card Loading
|
||||
# Kartendateien laden
|
||||
# -------------------------------
|
||||
def load_cards(self, filename):
|
||||
"""Loads pairs from a text file."""
|
||||
if not os.path.exists(filename):
|
||||
print(f"⚠️ File not found: {filename}")
|
||||
return []
|
||||
|
||||
pairs = []
|
||||
with open(filename, "r", encoding="utf-8") as f:
|
||||
for line in f:
|
||||
@ -60,7 +83,6 @@ class MemoryGame:
|
||||
return pairs
|
||||
|
||||
def prepare_deck(self):
|
||||
"""Loads all relevant continent + difficulty files."""
|
||||
self.deck = []
|
||||
for continent in self.selected_continents:
|
||||
base = continent
|
||||
@ -79,7 +101,6 @@ class MemoryGame:
|
||||
sys.exit()
|
||||
|
||||
random.shuffle(self.deck)
|
||||
# Limit to selected pair count
|
||||
self.deck = self.deck[:self.pair_count]
|
||||
|
||||
def setup_game(self):
|
||||
@ -102,8 +123,11 @@ class MemoryGame:
|
||||
margin = 10
|
||||
card_width = (SCREEN_WIDTH - (cols + 1) * margin) // cols
|
||||
card_height = (SCREEN_HEIGHT - (rows + 1) * margin - 100) // rows
|
||||
y_offset = 80
|
||||
|
||||
# Kartenrückseite = dein Logo vollflächig
|
||||
self.card_back = build_card_back(BACKGROUND_FILE, (card_width, card_height))
|
||||
|
||||
y_offset = 80
|
||||
for i, _ in enumerate(self.cards):
|
||||
col = i % cols
|
||||
row = i // cols
|
||||
@ -113,67 +137,53 @@ class MemoryGame:
|
||||
self.card_rects.append(rect)
|
||||
|
||||
# -------------------------------
|
||||
# Drawing Menus
|
||||
# Menü zeichnen
|
||||
# -------------------------------
|
||||
def draw_menu(self, screen, title, options):
|
||||
screen.fill(BG_COLOR)
|
||||
title_text = self.font.render(title, True, (255, 255, 255))
|
||||
screen.blit(title_text, (SCREEN_WIDTH // 2 - title_text.get_width() // 2, 100))
|
||||
self.buttons = []
|
||||
|
||||
for i, option in enumerate(options):
|
||||
rect = pygame.Rect(SCREEN_WIDTH // 2 - 150, 200 + i * 70, 300, 50)
|
||||
pygame.draw.rect(screen, (100, 100, 250), rect)
|
||||
pygame.draw.rect(screen, (255, 255, 255), rect, 2)
|
||||
pygame.draw.rect(screen, BUTTON_FILL, rect, border_radius=10)
|
||||
pygame.draw.rect(screen, BUTTON_BORDER, rect, 2, border_radius=10)
|
||||
text = self.font.render(option, True, (255, 255, 255))
|
||||
screen.blit(text, (rect.centerx - text.get_width() // 2, rect.centery - text.get_height() // 2))
|
||||
self.buttons.append((rect, option))
|
||||
pygame.display.flip()
|
||||
|
||||
# -------------------------------
|
||||
# Spiel zeichnen
|
||||
# -------------------------------
|
||||
def draw_game(self, screen):
|
||||
screen.fill(BG_COLOR)
|
||||
|
||||
title = self.font.render(f"Player {self.current_player + 1}'s turn", True, (255, 255, 255))
|
||||
screen.blit(title, (20, 20))
|
||||
score_text = self.font.render(f"Scores: P1={self.scores[0]} P2={self.scores[1]}", True, (200, 200, 200))
|
||||
score_text = self.font.render(f"Scores: P1={self.scores[0]} P2={self.scores[1]}", True, (220, 230, 235))
|
||||
screen.blit(score_text, (20, 50))
|
||||
|
||||
for i, rect in enumerate(self.card_rects):
|
||||
if self.matched[i]:
|
||||
color = MATCH_COLOR
|
||||
elif self.revealed[i]:
|
||||
color = CARD_FRONT_COLOR
|
||||
else:
|
||||
color = CARD_BACK_COLOR
|
||||
pygame.draw.rect(screen, color, rect)
|
||||
pygame.draw.rect(screen, MATCH_COLOR, rect)
|
||||
pygame.draw.rect(screen, (0, 0, 0), rect, 2)
|
||||
if self.revealed[i] or self.matched[i]:
|
||||
text = self.font.render(self.cards[i], True, TEXT_COLOR)
|
||||
text_rect = text.get_rect(center=rect.center)
|
||||
screen.blit(text, text_rect)
|
||||
screen.blit(text, text.get_rect(center=rect.center))
|
||||
elif self.revealed[i]:
|
||||
pygame.draw.rect(screen, CARD_FRONT_COLOR, rect)
|
||||
pygame.draw.rect(screen, (0, 0, 0), rect, 2)
|
||||
text = self.font.render(self.cards[i], True, TEXT_COLOR)
|
||||
screen.blit(text, text.get_rect(center=rect.center))
|
||||
else:
|
||||
# Dein Logo als Rückseite
|
||||
screen.blit(self.card_back, rect.topleft)
|
||||
pygame.draw.rect(screen, (0, 0, 0), rect, 2)
|
||||
|
||||
if self.awaiting_confirmation:
|
||||
self.draw_confirmation_box(screen)
|
||||
pygame.display.flip()
|
||||
|
||||
def draw_confirmation_box(self, screen): #y-axis Box
|
||||
box_rect = pygame.Rect(SCREEN_WIDTH // 2 - 150, SCREEN_HEIGHT // 2 - 320, 300, 160)
|
||||
pygame.draw.rect(screen, (250, 250, 250), box_rect)
|
||||
pygame.draw.rect(screen, (0, 0, 0), box_rect, 3)
|
||||
text = self.font.render("Is that correct?", True, (0, 0, 0))
|
||||
screen.blit(text, (box_rect.centerx - text.get_width() // 2, box_rect.y + 20))
|
||||
|
||||
yes_rect = pygame.Rect(box_rect.x + 50, box_rect.y + 90, 80, 40)
|
||||
no_rect = pygame.Rect(box_rect.x + 170, box_rect.y + 90, 80, 40)
|
||||
pygame.draw.rect(screen, (0, 200, 0), yes_rect)
|
||||
pygame.draw.rect(screen, (200, 0, 0), no_rect)
|
||||
yes_text = self.font.render("Yes", True, (255, 255, 255))
|
||||
no_text = self.font.render("No", True, (255, 255, 255))
|
||||
screen.blit(yes_text, (yes_rect.centerx - yes_text.get_width() // 2, yes_rect.centery - yes_text.get_height() // 2))
|
||||
screen.blit(no_text, (no_rect.centerx - no_text.get_width() // 2, no_rect.centery - no_text.get_height() // 2))
|
||||
self.yes_rect, self.no_rect = yes_rect, no_rect
|
||||
|
||||
# -------------------------------
|
||||
# Handling Clicks
|
||||
# Klicks & Logik
|
||||
# -------------------------------
|
||||
def handle_click(self, pos):
|
||||
if self.state in ["continent", "americas", "difficulty", "pairs"]:
|
||||
@ -188,7 +198,6 @@ class MemoryGame:
|
||||
else:
|
||||
self.selected_continents = [option]
|
||||
self.state = "difficulty"
|
||||
|
||||
elif self.state == "americas":
|
||||
if option == "North-America":
|
||||
self.selected_continents = ["North-America"]
|
||||
@ -197,83 +206,51 @@ class MemoryGame:
|
||||
elif option == "Americas":
|
||||
self.selected_continents = ["North-America", "South-America"]
|
||||
self.state = "difficulty"
|
||||
|
||||
elif self.state == "difficulty":
|
||||
self.level = option
|
||||
self.state = "pairs"
|
||||
|
||||
elif self.state == "pairs":
|
||||
self.pair_count = int(option)
|
||||
self.prepare_deck()
|
||||
self.setup_game()
|
||||
self.state = "game"
|
||||
return
|
||||
|
||||
elif self.state == "game":
|
||||
if self.awaiting_confirmation:
|
||||
if self.yes_rect.collidepoint(pos):
|
||||
self.confirmation_result = "yes"
|
||||
elif self.no_rect.collidepoint(pos):
|
||||
self.confirmation_result = "no"
|
||||
return
|
||||
|
||||
for i, rect in enumerate(self.card_rects):
|
||||
if rect.collidepoint(pos) and not self.revealed[i] and not self.matched[i]:
|
||||
self.revealed[i] = True
|
||||
self.selected.append(i)
|
||||
return
|
||||
|
||||
# -------------------------------
|
||||
# Game Logic Most important
|
||||
# -------------------------------
|
||||
def check_selected(self):
|
||||
if self.state != "game":
|
||||
return
|
||||
|
||||
# Wenn zwei Karten ausgewählt wurden → prüfen
|
||||
if len(self.selected) == 2 and not self.awaiting_confirmation:
|
||||
if len(self.selected) == 2:
|
||||
a, b = self.selected
|
||||
is_match = self.pair_map.get(self.cards[a]) == self.cards[b]
|
||||
self.correct_answer_expected = "yes" if is_match else "no"
|
||||
self.awaiting_confirmation = True
|
||||
|
||||
# Wenn der Spieler im "Is that correct?"-Dialog geantwortet hat
|
||||
elif self.awaiting_confirmation and self.confirmation_result:
|
||||
a, b = self.selected
|
||||
expected = self.correct_answer_expected
|
||||
player_correct = self.confirmation_result == expected
|
||||
|
||||
if player_correct:
|
||||
# Spieler hat korrekt geantwortet
|
||||
if expected == "yes":
|
||||
# Richtiges Paar bestätigt
|
||||
if self.pair_map.get(self.cards[a]) == self.cards[b]:
|
||||
self.matched[a] = self.matched[b] = True
|
||||
self.scores[self.current_player] += 1
|
||||
self.found_pairs += 1
|
||||
else:
|
||||
# "No" richtig bestätigt → kein Paar
|
||||
pygame.time.wait(600)
|
||||
self.revealed[a] = self.revealed[b] = False
|
||||
else:
|
||||
# Spieler hat sich vertan → Karten umdrehen & Punkt abziehen
|
||||
self.scores[self.current_player] -= 1
|
||||
self.revealed[a] = self.revealed[b] = False
|
||||
|
||||
# Reset für nächsten Zug
|
||||
self.awaiting_confirmation = False
|
||||
self.confirmation_result = None
|
||||
self.selected = []
|
||||
self.current_player = 1 - self.current_player
|
||||
self.selected = []
|
||||
|
||||
# -------------------------------
|
||||
# Main Game Loop
|
||||
# Hauptschleife
|
||||
# -------------------------------
|
||||
def run(self):
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
|
||||
pygame.display.set_caption("Country–Capital Memory Game")
|
||||
pygame.display.set_caption("Global Match – Memory")
|
||||
clock = pygame.time.Clock()
|
||||
self.font = pygame.font.SysFont(None, 32)
|
||||
|
||||
print("Erwarteter Bildpfad:", BACKGROUND_FILE)
|
||||
if not os.path.exists(BACKGROUND_FILE):
|
||||
print("⚠️ Logo nicht gefunden! Lege GlobalMatch.png in denselben Ordner.")
|
||||
|
||||
while self.running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
@ -295,7 +272,7 @@ class MemoryGame:
|
||||
if self.found_pairs == self.total_pairs:
|
||||
self.display_winner(screen)
|
||||
pygame.display.flip()
|
||||
pygame.time.wait(4000)
|
||||
pygame.time.wait(2000)
|
||||
self.running = False
|
||||
|
||||
clock.tick(FPS)
|
||||
@ -303,9 +280,6 @@ class MemoryGame:
|
||||
pygame.quit()
|
||||
sys.exit()
|
||||
|
||||
# -------------------------------
|
||||
# Winner Display
|
||||
# -------------------------------
|
||||
def display_winner(self, screen):
|
||||
if self.scores[0] > self.scores[1]:
|
||||
text = "🏆 Player 1 Wins!"
|
||||
@ -317,29 +291,7 @@ class MemoryGame:
|
||||
rect = win_text.get_rect(center=(SCREEN_WIDTH // 2, SCREEN_HEIGHT // 2))
|
||||
screen.blit(win_text, rect)
|
||||
|
||||
def run_memory(players: int = 1):
|
||||
pygame.init()
|
||||
screen = pygame.display.set_mode((900, 600))
|
||||
pygame.display.set_caption(f"Global Match – {players} Spieler")
|
||||
clock = pygame.time.Clock()
|
||||
|
||||
running = True
|
||||
while running:
|
||||
for event in pygame.event.get():
|
||||
if event.type == pygame.QUIT:
|
||||
running = False
|
||||
|
||||
screen.fill((10, 42, 53))
|
||||
# ... dein Memory-Rendering / Logic ...
|
||||
pygame.display.flip()
|
||||
clock.tick(60)
|
||||
|
||||
pygame.quit()
|
||||
|
||||
# -------------------------------
|
||||
# Run the Game
|
||||
# -------------------------------
|
||||
if __name__ == "__main__":
|
||||
game = MemoryGame()
|
||||
game.run()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user