-2

How do I implement a play again feature in this Connect 4 game?

import numpy as np
import pygame
import math


pygame.init()

#COLORS
GREEN = (0,200,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
RED = (200,0,0)
YELLOW = (255,255,0)
WHITE = (255,255,255)
PINK = (255,0,127)

bright_pink = (255,51,153)
bright_red = (255,0,0)
bright_green = (0,255,0)
bright_blue = (0,0,204)

ROW_COUNT = 6
COLUMN_COUNT = 7


pause = False
#DISPLAY
gameDisplay = pygame.display.set_mode((ROW_COUNT, COLUMN_COUNT))
pygame.display.set_caption("Connect 4") #Window Title
clock = pygame.time.Clock()


def text_objects(text, font):
    textSurface = font.render(text, True, BLACK)
    return textSurface, textSurface.get_rect()

def create_board():
    board = np.zeros((ROW_COUNT,COLUMN_COUNT))
    return board

def drop_piece(board, row, col, piece):
    board[row][col] = piece
    hitSound.play()

def is_valid_location(board, col):
    return board[ROW_COUNT-1][col] == 0

def get_next_open_row(board, col):
    for r in range(ROW_COUNT):
        if board[r][col] == 0:
            return r

def print_board(board):
    print(np.flip(board, 0))

def winning_move(board, piece):
    # Check horizontal locations for win
    for c in range(COLUMN_COUNT-3):
        for r in range(ROW_COUNT):
            if board[r][c] == piece and board[r][c+1] == piece and board[r][c+2] == piece and board[r][c+3] == piece:
                return True

    # Check vertical locations for win
    for c in range(COLUMN_COUNT):
        for r in range(ROW_COUNT-3):
            if board[r][c] == piece and board[r+1][c] == piece and board[r+2][c] == piece and board[r+3][c] == piece:
                return True

    # Check positively sloped diaganols
    for c in range(COLUMN_COUNT-3):
        for r in range(ROW_COUNT-3):
            if board[r][c] == piece and board[r+1][c+1] == piece and board[r+2][c+2] == piece and board[r+3][c+3] == piece:
                return True

    # Check negatively sloped diaganols
    for c in range(COLUMN_COUNT-3):
        for r in range(3, ROW_COUNT):
            if board[r][c] == piece and board[r-1][c+1] == piece and board[r-2][c+2] == piece and board[r-3][c+3] == piece:
                return True

def draw_board(board):
    for c in range(COLUMN_COUNT):
        for r in range(ROW_COUNT):

            pygame.draw.rect(screen, bright_blue, (c*SQUARESIZE, r*SQUARESIZE+SQUARESIZE, SQUARESIZE, SQUARESIZE))
            pygame.draw.circle(screen, BLACK, (int(c*SQUARESIZE+SQUARESIZE/2), int(r*SQUARESIZE+SQUARESIZE+SQUARESIZE/2)), RADIUS)


    for c in range(COLUMN_COUNT):
        for r in range(ROW_COUNT):
            if board[r][c] == 1:
                pygame.draw.circle(screen, RED, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)
            elif board[r][c] == 2:
                pygame.draw.circle(screen, YELLOW, (int(c*SQUARESIZE+SQUARESIZE/2), height-int(r*SQUARESIZE+SQUARESIZE/2)), RADIUS)

    pygame.display.update()


board = create_board()
print_board(board)
game_over = False



SQUARESIZE = 100

width = COLUMN_COUNT * SQUARESIZE
height = (ROW_COUNT+1) * SQUARESIZE

size = (width, height)

RADIUS = int(SQUARESIZE/2 - 5)

screen = pygame.display.set_mode(size)

draw_board(board)
pygame.display.update()


myfont2 = pygame.font.SysFont("Arial black", 75)


def gameover():

    gameDisplay.blit(background, (0, 0))
    gameDisplay.blit(gameoverimg, (0, 0))

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

        button("Play Again", 210, 350, 300, 80, GREEN, bright_green, )
        button("Quit", 260, 470, 200, 80, PINK, bright_pink, quit)

        pygame.display.update()
        clock.tick(15)




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(gameDisplay, ac, (x, y, w, h))
        if click[0] == 1 and action != None:
            action()
    else:
        pygame.draw.rect(gameDisplay, ic, (x, y, w, h))
    buttonText = pygame.font.SysFont("arial black", 50)
    textSurf, textRect = text_objects(msg, buttonText)
    textRect.center = ((x + (w / 2)), (y + (h / 2)))
    gameDisplay.blit(textSurf, textRect)


def quitgame():
    pygame.quit()
    quit()

def unpause():
    global pause
    pygame.mixer.music.unpause()
    pause = False

def paused():

    pygame.mixer.music.pause()

    gameDisplay.blit(background, (0, 0))
    gameDisplay.blit(pauseimg, (0, 0))

    while pause:
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                pygame.quit()
                quit()


        button("Continue",210, 350, 300, 80, GREEN, bright_green, unpause)
        button("Quit",260, 470, 200, 80, PINK, bright_pink, quit)

        pygame.display.update()
        clock.tick(15)


def game_intro():
    intro = True

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

        gameDisplay.blit(background, (0, 0))
        gameDisplay.blit(gmenu, (0, 60))

        button("START", 210, 350, 300, 80, GREEN, bright_green, game_loop)
        button("QUIT", 260, 470, 200, 80, PINK, bright_pink, quit)


        pygame.display.update()
        clock.tick(15)

def game_loop(turn=0):

    global pause
    game_over = False

    while not game_over:
        print_board(board)
        draw_board(board)

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

            if event.type == pygame.MOUSEMOTION:
                pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARESIZE))
                posx = event.pos[0]
                if turn == 0:
                    label2 = myfont2.render("RED TURN", 1, WHITE)
                    screen.blit(label2, (150, 10))
                    pygame.draw.circle(screen, RED, (posx, int(SQUARESIZE/2)), RADIUS)

                else:
                    label3 = myfont2.render("YELLOW TURN", 1, WHITE)
                    screen.blit(label3, (50, 10))
                    pygame.draw.circle(screen, YELLOW, (posx, int(SQUARESIZE/2)), RADIUS)


            if event.type == pygame.KEYDOWN:

                if event.key == pygame.K_p:
                    pause = True
                    pause_sound.play()
                    paused()

            # print_board(board)
            # draw_board(board)

            if event.type == pygame.MOUSEBUTTONDOWN:
                pygame.draw.rect(screen, BLACK, (0,0, width, SQUARESIZE))

                # Ask for Player 1 Input
                if turn == 0:
                    posx = event.pos[0]
                    col = int(math.floor(posx/SQUARESIZE))

                    if is_valid_location(board, col):
                        row = get_next_open_row(board, col)
                        drop_piece(board, row, col, 1)

                        if winning_move(board, 1):
                            label = myfont2.render("RED WINS", 1, RED)
                            screen.blit(label, (155,10))
                            # gameover()
                            game_over = True #Change to False so it will not auto-quit


                # # Ask for Player 2 Input
                else:
                    posx = event.pos[0]
                    col = int(math.floor(posx/SQUARESIZE))

                    if is_valid_location(board, col):
                        row = get_next_open_row(board, col)
                        drop_piece(board, row, col, 2)

                        if winning_move(board, 2):
                            label = myfont2.render("YELLOW WINS", 1, YELLOW)
                            screen.blit(label, (60,10))
                            game_over = True #Change to False so it will not auto-quit
                            # gameover()

                print_board(board)
                draw_board(board)


                turn += 1
                turn = turn % 2

                if game_over:
                    pygame.time.wait(3000)
                    gameover()
            pygame.display.update()
            clock.tick(60)


game_intro()
game_loop()
pygame.quit()
quit()
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • 1
    Hi Alex, you should try to be more specific when you ask questions in this forum, and always make sure to minimize the amount of code required to reproduce whatever problems you are having. – Jonas Byström Nov 19 '19 at 21:14

1 Answers1

0

Welcome to Stack Overflow. More specific questions, rather than "how do I do this", are preferred.

def gameover():

    gameDisplay.blit(background, (0, 0))
    gameDisplay.blit(gameoverimg, (0, 0))

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

        button("Play Again", 210, 350, 300, 80, GREEN, bright_green, )
        button("Quit", 260, 470, 200, 80, PINK, bright_pink, quit)

        pygame.display.update()
        clock.tick(15)

At the end of your button call for "Play Again", after your color definition, you need to call the correct function to "play again". It seems as though based on your code, you'd need to call game_loop or game_intro.

APhillips
  • 1,175
  • 9
  • 17