Post by gaslouk on Jan 8, 2024 5:09:11 GMT
Hi
Here is a game UNO I need Help Please.
Thanks
import pygame
import random
import sys
# Ορισμός χρωμάτων
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
class Player:
def __init__(self, name):
self.name = name
self.hand = []
self.is_turn = False
def draw_card(self, card):
self.hand.append(card)
def play_card(self, card, pile_card):
if card in self.hand and self.can_play_card(card, pile_card):
self.hand.remove(card)
return True
else:
return False
def can_play_card(self, selected_card, pile_card):
if pile_card:
# Ελέγξτε τους κανόνες για το ποιες κάρτες μπορούν να παιχτούν πάνω σε ποιες
if (
selected_card.color == pile_card.color
or selected_card.number == pile_card.number
or selected_card.color == YELLOW
):
return True
else:
return False
else:
# Αν δεν υπάρχει ήδη κάρτα στη στοίβα, οποιαδήποτε κάρτα μπορεί να παιχτεί
return True
def show_hand(self):
return self.hand
def check_uno(self):
if len(self.hand) == 1:
print(f"{self.name} called UNO!")
class Card(pygame.sprite.Sprite):
def __init__(self, x, y, color, number):
super().__init__()
self.image = pygame.Surface((50, 80))
self.image.fill(color)
font = pygame.font.Font(None, 36)
text_content = "⇐⇑⇒" if number == 10 else "Παράκαμψη" if number == 11 else "Παρε 2" if number == 12 else "Παρε 4" if number == 13 else "Αλλαγή Χρώματος" if number == 14 else str(number)
font = pygame.font.Font(pygame.font.get_default_font(), 14)
text = font.render(text_content, True, (0, 0, 0))
self.image.blit(text, (20, 30)) # Χρησιμοποίησε το κείμενο αντί για τον αριθμό
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.color = color
self.number = number
def __str__(self):
return f"{self.color} {self.number}"
class UnoGame:
def __init__(self, num_players):
self.players = [Player(f"Player {i + 1}") for i in range(num_players)]
self.current_player_index = 0
self.pile_card = None
def next_turn(self):
self.current_player_index = (self.current_player_index + 1) % len(self.players)
self.players[self.current_player_index].is_turn = True
def draw_cards(self, player, num_cards):
for _ in range(num_cards):
card = (
deck.pop()
) # Υποθέτουμε ότι η μεταβλητή deck είναι προηγουμένως ορισμένη
player.draw_card(card)
def handle_uno_rules(self, card):
if card is not None:
# Εφαρμόστε εδώ τυχόν ειδικούς κανόνες Uno
if card.number == 10:
# Reverse card - Αντιστροφή σειράς παικτών
self.players.reverse()
elif card.number == 11:
# Skip card - Παράκαμψη επόμενου παίκτη
self.next_turn()
elif card.number == 12:
# Draw Two card - Ο επόμενος παίκτης τραβάει δύο κάρτες και χάνει την σειρά του
next_player = self.players[
(self.current_player_index + 1) % len(self.players)
]
self.draw_cards(next_player, 2)
print(f"{next_player.name} drew two cards.")
self.next_turn()
elif card.number == 13:
# Draw Four card - Ο επόμενος παίκτης τραβάει τέσσερις κάρτες Και χάνει την σειρά του.
next_player = self.players[
(self.current_player_index + 1) % len(self.players)
]
self.draw_cards(next_player, 4)
print(f"{next_player.name} drew four cards.")
self.next_turn()
elif card.number == 14:
# Balader - Ο παίκτης επιλέγει νέο χρώμα
next_player = self.players[
(self.current_player_index + 1) % len(self.players)
]
selected_color = self.choose_color(next_player)
self.pile_card.color = selected_color
self.next_turn()
while selected_color not in valid_colors:
selected_color = input(
f"{player.name}, choose a color (Red, Green, Blue, Yellow): "
).upper()
# Συνάρτηση που εμφανίζει τα ονόματα και τα χέρια των παικτών
def show_hands(players):
for player in players:
print(f"{player.name}'s hand: {player.hand}")
def show_screen():
global deck # Declare deck as a global variable
global players
players = []
screen = pygame.display.set_mode((width, height))
# intro_screen.fill(PLAYER_COLOR)
font = pygame.font.Font(None, 36)
text = font.render("Welcome to Uno!", True, (0, 0, 0))
screen.blit(text, (width // 2 - text.get_width() // 2, 50))
num_players = 0
num_players = int(input("Enter the number of players (2 to 4): "))
while num_players < 2 or num_players > 4:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if pygame.K_2 <= event.key <= pygame.K_4:
num_players = int(chr(event.key))
if 2 <= num_players <= 4:
break
# intro_screen.fill(PLAYER_COLOR)
text = font.render("Welcome to Uno!", True, (0, 0, 0))
screen.blit(text, (width // 2 - text.get_width() // 2, 50))
pygame.display.flip()
pygame.time.Clock().tick(30) # Ελέγχουμε τα events κάθε 30 ms
for i in range(1, num_players + 1):
player_name = input(f"Enter the name for Player {i}: ")
players.append(Player(player_name))
dealer_index = random.randint(0, num_players - 1)
dealer = players[dealer_index]
print(f"{dealer.name} will deal the cards.")
# Κώδικας που δημιουργεί λίστες με τα χρώματα και τους αριθμούς των καρτών
colors = ["red", "green", "blue", "yellow"]
numbers = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
specials_with_color = ["Draw2", "Reverse", "Stop"] # Ειδικές κάρτες χωρίς χρώμα
specials_without_color = ["Draw4", "Wild"] # Ειδικές κάρτες με χρώμα
# Δημιουργία λίστας με όλες τις κάρτες
deck = []
# Προσθήκη όλων των καρτών με χρώματα στη λίστα
for color in colors:
for number in numbers:
card = Card(0, 0, color, number)
deck.append(card)
# Προσθήκη ειδικών καρτών χωρίς χρώμα
for special in specials_without_color:
for _ in range(4): # Προσθήκη 4 φορές κάθε ειδική κάρτα
card = Card(0, 0, "black", special)
deck.append(card)
# Προσθήκη ειδικών καρτών με χρώμα
for special in specials_with_color:
for color in colors:
card = Card(0, 0, color, special)
deck.append(card)
valid_colors = [RED, GREEN, BLUE, YELLOW]
selected_color = None
# Ανακατεύουμε τη λίστα των καρτών
random.shuffle(deck)
pass
pygame.time.wait(2000) # Wait for 2 seconds to display the intro screen
return players, dealer
def next_turn(self):
self.players[self.current_player_index].is_turn = False
if self.players:
self.current_player_index = (self.current_player_index + 1) % len(self.players)
else:
# Εδώ μπορείτε να προσθέσετε κώδικα για την περίπτωση που η λίστα είναι άδεια
if self.players:
self.players[self.current_player_index].is_turn = True
# Αρχικοποίηση Pygame
pygame.init()
# Δημιουργία παραθύρου
width, height = 800, 600
screen = pygame.display.set_mode((width + 40, height))
image = pygame.Surface((width, height))
pygame.display.set_mode((width, height), pygame.RESIZABLE)
pygame.display.set_caption("Uno Game")
# Αρχικοποίηση άλλων μεταβλητών
selected_card = None # Προσθήκη αυτής της γραμμής
current_player_index = 0
pile_card = None
WHITE = (255, 255, 255)
PLAYER_COLOR = (200, 200, 200)
BLACK = (0, 0, 0,)
players, dealer = show_screen()
# Αρχικοποίηση μεταβλητής για τον διανομέα
dealer.is_turn = True
# Εμφανίστε τον παίκτη που θα μοιράσει τις κάρτες
print(f"{dealer.name} will now deal the cards.")
# Καθαρίστε την οθόνη
# screen.fill(WHITE)
pygame.display.flip()
# Κάθε παίκτης εμφανίζει το χέρι του
for i, player in enumerate(players):
print(f"{player.name}'s hand: {player.show_hand()}")
# Αρχικοποίηση μεταβλητων για τις κάρτες
card_stack_sprites = pygame.sprite.Group()
open_card_sprites = pygame.sprite.Group()
pile_sprites = pygame.sprite.Group()
card_sprites = pygame.sprite.Group()
# Προσθήκη όλων των καρτών στο sprite group
card_sprites.add(deck)
# Initial coordinates for player cards
initial_x = 50
initial_y = height - 440
# Τοποθετούμε τις κάρτες των παικτών κάτω από τη στοίβα
num_cards_per_player = 7
for i, player in enumerate(players):
for j in range(num_cards_per_player):
color = random.choice([RED, GREEN, BLUE, YELLOW])
card = Card(
initial_x + j * 55, initial_y + i * 85, color, random.randint(1, 14)
)
player.draw_card(card)
card_sprites.add(card)
# Προσθήκη αριθμημένων καρτών (1-9) για κάθε χρώμα
for color in [RED, GREEN, BLUE, YELLOW]:
for number in range(1, 10):
card = Card(0, 0, color, number)
deck.extend([card] * 2) # Προσθήκη της κάρτας δύο φορές
# Προσθήκη της αριθμημένης κάρτας 0 για κάθε χρώμα
for color in [RED, GREEN, BLUE, YELLOW]:
card = Card(0, 0, color, 0)
deck.append(card)
# Set player turns
players[current_player_index].is_turn = True
# Τοποθετούμε τις κάρτες των παικτών κάτω από τη στοίβα
for i, player in enumerate(players):
for j, card in enumerate(player.hand):
card_sprites.add(card)
# Εμφάνιση της στοίβας
for i, card in enumerate(deck):
card.rect.x = 10 * 10
card.rect.y = height // 10
pile_sprites.add(card)
# Δημιουργία ανοικτής κάρτας
open_card_position = (10 * 20, height // 10)
open_card = Card(
open_card_position[0],
open_card_position[1],
random.choice(color),
random.randint(1, 14),
)
open_card_sprites.add(open_card)
# Εμφάνιση καταστάσεων μεταβλητών
font = pygame.font.Font(None, 30)
text = font.render(
f"Current Player: {players[current_player_index].name}", True, (0, 0, 0)
)
screen.blit(text, (20, height - 60))
if pile_card:
font = pygame.font.Font(None, 30)
text = font.render(
f"Pile Card: {pile_card.color} {pile_card.number}", True, (0, 0, 0)
)
screen.blit(text, (20, height - 30))
# Νέες γραμμές κειμένου
if pile_card:
font = pygame.font.Font(None, 30)
text = font.render(
f"Pile Cards: {', '.join([str(card.number) for card in pile_card])}",
True,
(0, 0, 0),
)
else:
font = pygame.font.Font(None, 30)
text = font.render("Pile Cards: ", True, (0, 0, 0))
screen.blit(text, (20, height - 90))
font = pygame.font.Font(None, 30)
text = font.render(
f"Player Discards: {', '.join([str(card.number) for card in open_card_sprites])}",
True,
(0, 0, 0),
)
screen.blit(text, (20, height - 120))
# Επαναφορά της selected_card σε None κάθε φορά που αρχίζει τον γύρο
selected_card = None
# Κύριος βρόχος παιχνιδιού
running = True
player_info_y = height - 30
players[current_player_index]
# game = UnoGame(num_players)
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = event.pos
clicked_sprites = [
sprite for sprite in card_sprites if sprite.rect.collidepoint(x, y)
]
if clicked_sprites and players[current_player_index].is_turn:
selected_card = clicked_sprites[0]
if pile_card and players[current_player_index].can_play_card(
selected_card, pile_card
):
if players[current_player_index].play_card(selected_card):
print(
f"{players[current_player_index].name} played {selected_card}"
)
players[current_player_index].is_turn = False
open_card_sprites.empty()
open_card_sprites.add(selected_card)
if deck:
new_card = deck.pop()
players[current_player_index].draw_card(new_card)
card_sprites.add(new_card)
if players[current_player_index].play_card(
selected_card
):
print(
f"{players[current_player_index].name} played {selected_card}"
)
else:
print("Deck is empty.")
pile_card = selected_card
check_uno(players[current_player_index])
if deck:
new_card = deck.pop()
players[current_player_index].draw_card(new_card)
card_sprites.add(new_card)
else:
print("Deck is empty.")
# Καλέστε τη μέθοδο για τους κανόνες Uno
game.handle_uno_rules(game.pile_card)
current_player_index = (current_player_index + 1) % len(
players
)
players[current_player_index].is_turn = True
handle_uno_rules(selected_card)
game.next_turn()
# Ενημέρωση οθόνης
screen.fill(PLAYER_COLOR)
pile_sprites.draw(screen)
open_card_sprites.draw(screen)
card_sprites.draw(screen)
# Εμφάνιση καταστάσεων μεταβλητών
font = pygame.font.Font(None, 30)
text = font.render(
f"Current Player: {players[current_player_index].name}", True, (0, 0, 0)
)
screen.blit(text, (20, height - 60))
pygame.display.flip()
if pile_card:
text = font.render(
f"Pile Card: {pile_card.color} {pile_card.number}", True, (0, 0, 0)
)
screen.blit(text, (20, height - 30))
text = font.render(
f"Pile Cards: {', '.join([str(card.number) for card in pile_card])}",
True,
(0, 0, 0),
)
screen.blit(text, (20, height - 120))
# Εμφάνιση όνοματος παίκτη που έχει σειρά
current_player_name_text = font.render(
f"Current Player: {players[current_player_index].name}", True, (0, 0, 0)
)
screen.blit(current_player_name_text, (20, height - 60))
# Κώδικας για δημιουργία και εμφάνιση των καρτών
card_sprites = (
pygame.sprite.Group()
)
# Εδώ δημιουργούμε το Group ξανά για να αδειάσει το περιεχόμενό του
for i, player in enumerate(players):
for j, card in enumerate(player.hand):
card_x = 20 + j * 55
card_y = player_info_y - i * 85 - 220
card.rect.x = card_x
card.rect.y = card_y
card_sprites.add(card)
# Εμφάνιση του ονόματος του κατόχου δίπλα στην κάρτα
font = pygame.font.Font(None, 18)
owner_text = font.render(player.name, True, (255, 255, 255))
screen.blit(owner_text, (card_x + 10, card_y + 80))
pygame.display.flip()
# Τερματισμός Pygame
pygame.quit()
sys.exit()
Thanks