Need help creating a project for the game UNO (on PyGame) #857
Unanswered
popkorni4
asked this question in
Code with Codespaces
Replies: 1 comment
-
sry for ugle cod. here is the text file |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'm doing a project on the uno game and I got a few errors in the code (offline game, not finished online yet), I tried to fix it with neural networks, but nothing worked, the reverse doesn't work in my code. Also, sometimes, when the 3rd bot starts walking, I have a problem. Here is all my code. Thank you in advance:
`import pygame
import random
from collections import deque
#Screen Settings
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
#Colors
ORANGE = (255, 165, 0)
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GRAY = (200, 200, 200)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
YELLOW = (255, 255, 0)
DARK_PURPLE = (128, 0, 128)
Initialization PyGame
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("UNO Game")
Map class
class Card:
def init(self, color, value):
self.color = color
self.value = value
self.chosen_color = None # A field for storing the selected color
def draw(self, x, y):
if self.color == 'black' and self.chosen_color: # Displaying the selected color for black cards
pygame.draw.rect(screen, self.get_color(self.chosen_color), (x, y, 70, 100))
elif self.color == 'black':
pygame.draw.rect(screen, DARK_PURPLE, (x, y, 70, 100)) # Dark purple color for black cards before color selection
else:
pygame.draw.rect(screen, self.get_color(), (x, y, 70, 100))
font = pygame.font.SysFont(None, 36)
text = font.render(str(self.value), True, BLACK)
screen.blit(text, (x + 25, y + 40))
The card shirt
def draw_card_back(x, y):
pygame.draw.rect(screen, (255, 0, 0), (x, y, 70, 100)) # red half
pygame.draw.rect(screen, (0, 0, 0), (x + 10, y + 10, 50, 80)) # black half
def draw_card_deck(x, y):
pygame.draw.rect(screen, (255, 0, 0), (x, y, 70, 100)) # red half
pygame.draw.rect(screen, (0, 0, 0), (x + 10, y + 10, 50, 80)) # black half
pygame.draw.circle(screen, ORANGE, (x + 35, y + 50), 25) #orange circle
def draw_color_selectors():
colors = {'red': RED, 'blue': BLUE, 'green': GREEN, 'yellow': YELLOW}
positions = [
(SCREEN_WIDTH // 2 - 60, SCREEN_HEIGHT // 2 - 60),
(SCREEN_WIDTH // 2 - 60, SCREEN_HEIGHT // 2 + 30),
(SCREEN_WIDTH // 2 + 30, SCREEN_HEIGHT // 2 + 30),
(SCREEN_WIDTH // 2 + 30, SCREEN_HEIGHT // 2 - 60)
]
for i, (pos_x, pos_y) in enumerate(positions):
color_name = list(colors.keys())[i]
pygame.draw.polygon(screen, colors[color_name], [
(pos_x, pos_y),
(pos_x + 30, pos_y + 30),
(pos_x - 30, pos_y + 30)
])
Creating a deck
def create_deck():
colors = ['red', 'blue', 'green', 'yellow']
values = [str(i) for i in range(10)] + ['skip', 'reverse', '+2']
deck = []
for color in colors:
for value in values:
deck.append(Card(color, value))
if value != '0':
deck.append(Card(color, value))
for _ in range(4):
deck.append(Card('black', '+4')) # Adding special cards
deck.append(Card('black', 'wild'))
return deck
Checking if the card is special
def is_special_card(card):
return card.value in ['skip', 'reverse', '+2', '+4', 'wild']
Main menu
def main_menu():
font = pygame.font.SysFont(None, 72)
button_font = pygame.font.SysFont(None, 48)
while True:
screen.fill(WHITE)
title = font.render("UNO Game", True, BLACK)
screen.blit(title, (SCREEN_WIDTH // 2 - title.get_width() // 2, 100))
# Button "Play with friends"
multiplayer_button = pygame.Rect(SCREEN_WIDTH // 2 - 150, 300, 300, 50)
pygame.draw.rect(screen, GRAY, multiplayer_button)
multiplayer_text = button_font.render("Play with friends", True, BLACK)
screen.blit(multiplayer_text, (SCREEN_WIDTH // 2 - multiplayer_text.get_width() // 2, 310))
# button "Play alone"
singleplayer_button = pygame.Rect(SCREEN_WIDTH // 2 - 150, 400, 300, 50)
pygame.draw.rect(screen, GRAY, singleplayer_button)
singleplayer_text = button_font.render("Играть одному", True, BLACK)
screen.blit(singleplayer_text, (SCREEN_WIDTH // 2 - singleplayer_text.get_width() // 2, 410))
pygame.display.flip()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
return
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_pos = pygame.mouse.get_pos()
if multiplayer_button.collidepoint(mouse_pos):
start_multiplayer()
elif singleplayer_button.collidepoint(mouse_pos):
start_singleplayer()
Multiplayer mode
def start_multiplayer():
print("Multiplayer is not implemented in this example.")
Player Class
class Player:
def init(self, name="Bot"):
self.name = name
self.hand = []
self.blocked = False # Player blocking flag
Single player mode with bots
def start_singleplayer():
global current_color # A global variable for tracking the current color
deck = create_deck()
random.shuffle(deck)
# The first card should not be special.
while is_special_card(deck[-1]):
random.shuffle(deck)
discard_pile = deque()
discard_pile.append(deck.pop()) # The first card on the table
player = Player("Player")
bots = [Player(f"Bot {i}") for i in range(3)]
# Card distribution
for _ in range(7):
player.draw_card(deck)
for bot in bots:
bot.draw_card(deck)
current_player = 0
players = [player] + bots
direction = 1 # Direction of travel (1 - clockwise, -1 - counterclockwise)
Processing of special cards
def process_special_card(card, players, current_player, direction, deck):
global current_color # A global variable for tracking the current color
if card.value == 'skip':
# Skip the next player
next_player = (current_player + direction) % len(players)
players[next_player].blocked = True # Blocking the next player
elif card.value == 'reverse':
if len(players) == 2: #If there are only two players, reverse works like skip
next_player = (current_player + direction) % len(players)
players[next_player].blocked = True
else:
direction *= -1 # Changing the direction of travel
print(f"Direction changed to {direction}") # Debugging output
# Visual notification of a change of direction
font = pygame.font.SysFont(None, 36)
reverse_text = font.render("Direction reversed!", True, BLACK)
screen.blit(reverse_text, (SCREEN_WIDTH // 2 - reverse_text.get_width() // 2, 50))
pygame.display.flip()
pygame.time.wait(1000) # Delay to demonstrate the notification
elif card.value == '+2':
next_player = (current_player + direction) % len(players)
for _ in range(2):
players[next_player].draw_card(deck) # The next player draws two cards
players[next_player].blocked = True # Blocking the next player
elif card.value == '+4':
next_player = (current_player + direction) % len(players)
for _ in range(4):
players[next_player].draw_card(deck) # The next player draws four cards
players[next_player].blocked = True # Blocking the next player
if card.chosen_color: # If the card is "+4", change the color to the selected one
current_color = card.chosen_color
elif card.value == 'wild':
if card.chosen_color: # If the card is "wild", change the color to the selected one.
current_color = card.chosen_color
if name == "main":
main_menu()`
Beta Was this translation helpful? Give feedback.
All reactions