diff --git a/assets.py b/assets.py index 1c9c9f2..1d679ad 100644 --- a/assets.py +++ b/assets.py @@ -5,6 +5,10 @@ event_sound = join("sounds", "event.ogg") disc_drop_1 = join("sounds", "disc_drop_1.wav") disc_drop_2 = join("sounds", "disc_drop_2.wav") -red_coin = load(join("images", "redball90px.png")) -yellow_coin = load(join("images", "yellowball90px.png")) +red_coin = load(join("images", "red.png")) +yellow_coin = load(join("images", "Yellow.png")) +blue_coin = load(join("images", "darkBlue.png")) +gray_coin = load(join("images", "gray.png")) +green_coin = load(join("images", "green.png")) +violet_coin = load(join("images", "Violet.png")) black_coin = load(join("images", "blackball91px.png")) diff --git a/config.py b/config.py index debfff6..15b7a96 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,9 @@ # Colors yellow = (255, 255, 0) red = (255, 0, 0) +green = (0, 255, 0) blue = (0, 0, 255) white = (255, 255, 255) black = (0, 0, 0) +violet = (255, 0, 255) +gray = (211, 211, 211) diff --git a/connect_game.py b/connect_game.py index c5fe4a9..832bee7 100644 --- a/connect_game.py +++ b/connect_game.py @@ -102,7 +102,6 @@ def update(self): if self.game_data.game_over: print(os.getpid()) pygame.time.wait(1000) - os.system("game.py") def draw(self): """ diff --git a/game.py b/game.py index 5f16d96..684d82a 100644 --- a/game.py +++ b/game.py @@ -3,7 +3,7 @@ import pygame from pygame.locals import KEYDOWN -from config import black, blue, white +from config import black, blue, gray, green, red, violet, white, yellow from connect_game import ConnectGame from events import MouseClickEvent, MouseHoverEvent, bus from game_data import GameData @@ -11,11 +11,17 @@ def quit(): + ''' + To exit game + ''' sys.exit() def start(): - data = GameData() + ''' + Main Game Loop + ''' + data = GameData(c1, c2 ,bg) screen = pygame.display.set_mode(data.size) game = ConnectGame(data, GameRenderer(screen, data)) @@ -48,14 +54,77 @@ def start(): game.update() game.draw() + restart() + + +def restart(): + ''' + Use to show the menu after game is finished + ''' + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + sys.exit() + button("RESTART", 275, 200, 150, 50, white, start) + button("RESTART", 277, 202, 146, 46, black, start) + button("CHANGE COLORS", 230, 350, 250, 50, white, menu) + button("CHANGE COLORS", 232, 352, 246, 46, black, menu) + button("QUIT", 300, 500, 100, 50, white, quit) + button("QUIT", 302, 502, 96, 46, black, quit) + pygame.display.update() + + + +def color_change(color, player_no): + ''' + Used to change colors of player coins and background + ''' + global c1, c2 ,bg + + if player_no == 1: + c1 = color + elif player_no == 2: + c2 = color + else : + bg = color + +def button(msg, x, y, w, h, color, action=None, player_no=0): + ''' + Used to make buttons which fires when clicked and performs the designated action + ''' + mouse = pygame.mouse.get_pos() + click = pygame.mouse.get_pressed() + + if x + w > mouse[0] > x and y + h > mouse[1] > y: + pygame.draw.rect(screen, color, (x, y, w, h)) + + if click[0] == 1 and action != None: + if action == color_change: + action(color, player_no) + else: + action() + else: + pygame.draw.rect(screen, color, (x, y, w, h)) + + smallText = pygame.font.SysFont("monospace", 30) + textSurf, textRect = text_objects(msg, smallText, white) + textRect.center = ((x + (w / 2)), (y + (h / 2))) + screen.blit(textSurf, textRect) def text_objects(text, font, color): + ''' + Used to make text for objects + ''' textSurface = font.render(text, True, color) return textSurface, textSurface.get_rect() def message_display(text, color, p, q, v): + ''' + Used to make the message display on screen + ''' largeText = pygame.font.SysFont("monospace", v) TextSurf, TextRect = text_objects(text, largeText, color) TextRect.center = (p, q) @@ -63,37 +132,55 @@ def message_display(text, color, p, q, v): pygame.init() -screen = pygame.display.set_mode(GameData().size) -pygame.display.set_caption("Connect Four | Mayank Singh") -message_display("CONNECT FOUR!!", white, 350, 150, 75) -message_display("HAVE FUN!", (23, 196, 243), 350, 300, 75) - -running = True -while running: - for event in pygame.event.get(): - if event.type == pygame.QUIT: - running = False +c1 = red +c2 = yellow +bg = blue - def button(msg, x, y, w, h, ic, ac, action=None): - mouse = pygame.mouse.get_pos() - click = pygame.mouse.get_pressed() - - if x + w > mouse[0] > x and y + h > mouse[1] > y: - pygame.draw.rect(screen, ac, (x, y, w, h)) - - if click[0] == 1 and action != None: - action() - else: - pygame.draw.rect(screen, ic, (x, y, w, h)) - - smallText = pygame.font.SysFont("monospace", 30) - textSurf, textRect = text_objects(msg, smallText, white) - textRect.center = ((x + (w / 2)), (y + (h / 2))) - screen.blit(textSurf, textRect) - - button("PLAY!", 150, 450, 100, 50, white, white, start) - button("PLAY", 152, 452, 96, 46, black, black, start) - button("QUIT", 450, 450, 100, 50, white, white, quit) - button("QUIT", 452, 452, 96, 46, black, black, quit) - pygame.display.update() +screen = pygame.display.set_mode(GameData().size) +pygame.display.set_caption("Connect Four | Mayank Singh") +logo = pygame.image.load("images/logo/connect4.png") + +def menu(): + ''' + Menu Loop + ''' + running = True + while running: + for event in pygame.event.get(): + if event.type == pygame.QUIT: + running = False + pygame.Surface.fill(screen,black) + message_display("PLAYER 1", c1, 210, 250, 45) + message_display("PLAYER 2", c2, 210, 400, 45) + message_display("BOARD COLOR", bg, 233, 550, 40) + + button("PLAY!", 500, 300, 100, 50, white, start) + button("PLAY!", 502, 302, 96, 46, black, start) + button("QUIT", 500, 450, 100, 50, white, quit) + button("QUIT", 502, 452, 96, 46, black, quit) + + button("", 100, 280, 30, 30, red, color_change, 1) + button("", 150, 280, 30, 30, yellow, color_change, 1) + button("", 200, 280, 30, 30, green, color_change, 1) + button("", 250, 280, 30, 30, blue, color_change, 1) + button("", 300, 280, 30, 30, violet, color_change, 1) + button("", 350, 280, 30, 30, gray, color_change, 1) + + button("", 100, 430, 30, 30, red, color_change, 2) + button("", 150, 430, 30, 30, yellow, color_change, 2) + button("", 200, 430, 30, 30, green, color_change, 2) + button("", 250, 430, 30, 30, blue, color_change, 2) + button("", 300, 430, 30, 30, violet, color_change, 2) + button("", 350, 430, 30, 30, gray, color_change, 2) + + button("", 100, 580, 30, 30, red, color_change, None) + button("", 150, 580, 30, 30, yellow, color_change, None) + button("", 200, 580, 30, 30, green, color_change, None) + button("", 250, 580, 30, 30, blue, color_change, None) + button("", 300, 580, 30, 30, violet, color_change, None) + button("", 350, 580, 30, 30, gray, color_change, None) + + screen.blit(logo,(50,50)) + pygame.display.update() +menu() \ No newline at end of file diff --git a/game_data.py b/game_data.py index a7ae2fc..2e6a88d 100644 --- a/game_data.py +++ b/game_data.py @@ -1,5 +1,8 @@ from typing import Tuple +from assets import (black_coin, disc_drop_1, disc_drop_2, event_sound, + red_coin, yellow_coin) +from config import black, blue, green, red, violet, white, yellow from game_board import GameBoard @@ -19,14 +22,16 @@ class GameData: last_move_col: [int] game_board: GameBoard - def __init__(self): + def __init__(self, color_player1=red, color_player2=yellow ,back_color=blue): self.game_over = False self.turn = 0 self.last_move_row = [] self.last_move_col = [] self.game_board = GameBoard() self.action = None - + self.c1 = color_player1 + self.c2 = color_player2 + self.bg = back_color self.sq_size: int = 100 self.width: int = 7 * self.sq_size self.height: int = 7 * self.sq_size diff --git a/game_renderer.py b/game_renderer.py index 465063b..592b0b2 100644 --- a/game_renderer.py +++ b/game_renderer.py @@ -7,9 +7,10 @@ from pygame.ftfont import Font from pygame.gfxdraw import aacircle, filled_circle -from assets import (black_coin, disc_drop_1, disc_drop_2, event_sound, - red_coin, yellow_coin) -from config import black, blue, red, white, yellow +from assets import (black_coin, blue_coin, disc_drop_1, disc_drop_2, + event_sound, gray_coin, green_coin, red_coin, violet_coin, + yellow_coin) +from config import black, blue, gray, green, red, violet, white, yellow from events import GameOver, MouseHoverEvent, PieceDropEvent, bus from game_data import GameData @@ -29,6 +30,22 @@ def on_piece_drop(event: PieceDropEvent): mixer.music.play(0) +def coin_color(color): + k = red_coin + if color == green: + k = green_coin + elif color == blue: + k = blue_coin + elif color == violet: + k = violet_coin + elif color == yellow: + k = yellow_coin + elif color == gray: + k = gray_coin + + return k + + class GameRenderer: """ Renders the current game state to the screen and the speakers. @@ -70,21 +87,21 @@ def on_mouse_move(self, event: MouseHoverEvent): int(self.game_data.sq_size) - self.game_data.sq_size + 5, ) - def draw_red_coin(self, x, y): + def draw_coin_p1(self, x, y): """ - Draws a red coin. + Draws the coin of player 1. :param x: The x position to draw the coin. :param y: The y position to draw the coin. """ - self.screen.blit(red_coin, (x, y)) + self.screen.blit(coin_color(self.game_data.c1), (x, y)) - def draw_yellow_coin(self, x, y): + def draw_coin_p2(self, x, y): """ - Draws a yellow coin. + Draws the coin of player 2. :param x: The x position to draw the coin. :param y: The y position to draw the coin. """ - self.screen.blit(yellow_coin, (x, y)) + self.screen.blit(coin_color(self.game_data.c2), (x, y)) def draw_black_coin(self, x, y): """ @@ -104,9 +121,9 @@ def draw_coin(self, game_data, x, y): :param y: The y position for the coin to be drawn. """ if game_data.turn == 0: - self.screen.blit(red_coin, (x, y)) + self.screen.blit(coin_color(game_data.c1), (x, y)) else: - self.screen.blit(yellow_coin, (x, y)) + self.screen.blit(coin_color(game_data.c2), (x, y)) def draw(self, game_data: GameData): """ @@ -154,9 +171,9 @@ def on_game_over(self, event: GameOver): color = None if event.winner == 1: - color = red + color = self.game_data.c1 if event.winner == 2: - color = yellow + color = self.game_data.c2 if not event.was_tie: self.label = self.myfont.render(f"PLAYER {event.winner} WINS!", 1, color) @@ -184,7 +201,7 @@ def draw_board(self, board): for r in range(board.rows): pygame.draw.rect( self.screen, - blue, + (self.game_data.bg), (c * sq_size, (r + 1) * sq_size, sq_size, sq_size), ) aacircle( @@ -205,12 +222,12 @@ def draw_board(self, board): for c in range(board.cols): for r in range(board.rows): if board.board[r][c] == 1: - self.draw_red_coin( + self.draw_coin_p1( int(c * sq_size) + 5, height - int(r * sq_size + sq_size - 5) ) elif board.board[r][c] == 2: - self.draw_yellow_coin( + self.draw_coin_p2( int(c * sq_size) + 5, height - int(r * sq_size + sq_size - 5) ) diff --git a/images/Violet.png b/images/Violet.png new file mode 100644 index 0000000..c58f7f4 Binary files /dev/null and b/images/Violet.png differ diff --git a/images/Yellow.png b/images/Yellow.png new file mode 100644 index 0000000..23c36ed Binary files /dev/null and b/images/Yellow.png differ diff --git a/images/darkBlue.png b/images/darkBlue.png new file mode 100644 index 0000000..373a927 Binary files /dev/null and b/images/darkBlue.png differ diff --git a/images/gray.png b/images/gray.png new file mode 100644 index 0000000..b76d3fd Binary files /dev/null and b/images/gray.png differ diff --git a/images/green.png b/images/green.png new file mode 100644 index 0000000..cdfe9c7 Binary files /dev/null and b/images/green.png differ diff --git a/images/neonBlue.png b/images/neonBlue.png new file mode 100644 index 0000000..3565713 Binary files /dev/null and b/images/neonBlue.png differ diff --git a/images/red.png b/images/red.png new file mode 100644 index 0000000..c89cc0e Binary files /dev/null and b/images/red.png differ