Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
3 changes: 3 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -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)
1 change: 0 additions & 1 deletion connect_game.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
155 changes: 121 additions & 34 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
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
from game_renderer import GameRenderer


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))

Expand Down Expand Up @@ -48,52 +54,133 @@ 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)
screen.blit(TextSurf, TextRect)


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()
9 changes: 7 additions & 2 deletions game_data.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down
49 changes: 33 additions & 16 deletions game_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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)
)

Expand Down
Binary file added images/Violet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/Yellow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/darkBlue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/gray.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/green.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/neonBlue.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/red.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.