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)
112 changes: 89 additions & 23 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,22 @@
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

c1 = red
c2 = yellow


def quit():
sys.exit()


def start():
data = GameData()
data = GameData(c1, c2)
screen = pygame.display.set_mode(data.size)
game = ConnectGame(data, GameRenderer(screen, data))

Expand Down Expand Up @@ -50,6 +53,84 @@ def start():
game.draw()


flag = 1


def color_change():
global screen, flag
screen.fill(black)

running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
quit()

message_display("CONNECT FOUR!!", white, 350, 150, 75)
message_display("PLAYER 1", c1, 215, 300, 35)
message_display("PLAYER 2", c2, 215, 500, 35)

button("", 100, 325, 30, 30, red, coin_change, 1)
button("", 150, 325, 30, 30, yellow, coin_change, 1)
button("", 200, 325, 30, 30, green, coin_change, 1)
button("", 250, 325, 30, 30, blue, coin_change, 1)
button("", 300, 325, 30, 30, violet, coin_change, 1)
button("", 350, 325, 30, 30, gray, coin_change, 1)

button("", 100, 525, 30, 30, red, coin_change, 2)
button("", 150, 525, 30, 30, yellow, coin_change, 2)
button("", 200, 525, 30, 30, green, coin_change, 2)
button("", 250, 525, 30, 30, blue, coin_change, 2)
button("", 300, 525, 30, 30, violet, coin_change, 2)
button("", 350, 525, 30, 30, gray, coin_change, 2)

button("SAVE", 510, 600, 130, 50, white, save)
button("SAVE", 512, 602, 125, 46, black, save)
pygame.display.update()

if flag == 0:
flag = 1
screen.fill(black)
message_display("CONNECT FOUR!!", white, 350, 150, 75)
message_display("HAVE FUN!", (23, 196, 243), 350, 300, 75)
running = False


def save():
global flag
flag = 0


def coin_change(color, player_no):
global c1, c2

if player_no == 1:
c1 = color
else:
c2 = color


def button(msg, x, y, w, h, color, action=None, player_no=0):
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 == coin_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):
textSurface = font.render(text, True, color)
return textSurface, textSurface.get_rect()
Expand All @@ -75,25 +156,10 @@ def message_display(text, color, p, q, v):
if event.type == pygame.QUIT:
running = False

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)
button("PLAY!", 150, 450, 100, 50, white, start)
button("PLAY", 152, 452, 96, 46, black, start)
button("QUIT", 450, 450, 100, 50, white, quit)
button("QUIT", 452, 452, 96, 46, black, quit)
button("COLOR CHANGE", 210, 600, 230, 50, white, color_change)
button("COLOR CHANGE", 212, 602, 225, 46, black, color_change)
pygame.display.update()
8 changes: 6 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,15 @@ class GameData:
last_move_col: [int]
game_board: GameBoard

def __init__(self):
def __init__(self, color_player1=red, color_player2=yellow):
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.sq_size: int = 100
self.width: int = 7 * self.sq_size
self.height: int = 7 * self.sq_size
Expand Down
43 changes: 30 additions & 13 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.
: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.
: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 @@ -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.