diff --git a/README.md b/README.md index 8df4fe7..803e5f6 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,11 @@ More like a shorter version of chess, where one needs to stay focused, anticipat $ git clone https://github.com/NJACKWinterOfCode/Race-the-car.git $ cd Race-the-car ``` -* If you don't have cx_Freeze installed, get it by typing the following in command prompt(for Windows) +* If you don't have cx_Freeze installed, get it by typing the following in command prompt (for Windows) ```sh $ pip install cx_Freeze ``` -* If you don't have cx_Freeze installed, get it by typing the following in terminal(for Linux) +* If you don't have cx_Freeze installed, get it by typing the following in terminal (for Linux) ```sh $ sudo apt install cx-freeze ``` @@ -39,7 +39,7 @@ $ python Race_the_car.py ``` ## Objective -In this game two players need to reach the desired destination(i.e the finish flag of respective colors). The one reaching first will win the game. +In this game two players need to reach the desired destination (i.e the finish flag of respective colors). The one reaching first will win the game. ## Twist To make the game interesting each player is provided with 8 fences to make the opponent's path difficult. diff --git a/Race_the_car.py b/Race_the_car.py index a6cbc31..acfaadd 100644 --- a/Race_the_car.py +++ b/Race_the_car.py @@ -1,11 +1,11 @@ -import sys, pygame +import sys from pygame.locals import * from constants import * -player1Fence = [] # records the position of the fences by player1 -player2Fence = [] # records the position of the fences by player2 -TotalFence = [] # recors the co-ordinates of all fences +player_1_fence = [] # records the position of the fences by player1 +player_2_fence = [] # records the position of the fences by player2 +total_fence = [] # records the co-ordinates of all fences # Board space ID's PLAYER_ONE = 1 @@ -13,174 +13,170 @@ OPEN_SPACE = 3 # Board positions -BOARDWIDTH_CENTER = BOARDWIDTH - int(BOARDWIDTH/2) - 1 +BOARDWIDTH_CENTER = BOARDWIDTH - int(BOARDWIDTH / 2) - 1 ONE_STARTING_ROW = BOARDHEIGHT - 1 TWO_STARTING_ROW = 0 + def main(): global FPSCLOCK, DISPLAYSURF, BASICFONT, FENCE_SURF, FENCE_RECT, MOVE_SURF, MOVE_RECT, SOLVE_SURF, SOLVE_RECT pygame.init() FPSCLOCK = pygame.time.Clock() - mousex = 0 - mousey = 0 # co-ordinates of mouse events - playerChance = 1 # chance of the player by default=1 + # co-ordinates of mouse events + mouse_x = 0 + mouse_y = 0 + # chance of the player by default=1 + player_chance = 1 DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT)) pygame.display.set_caption("Race the Car") BASICFONT = pygame.font.SysFont("comicsansms", BASICFONTSIZE) - textSurfaceObj = BASICFONT.render("Race the car....", True, BLACK, WHITE) - textRectObj = textSurfaceObj.get_rect() - textRectObj.center = (450, 10) - mainBoard = getStartingBoard() + text_surface_obj = BASICFONT.render("Race the car....", True, BLACK, WHITE) + text_rect_obj = text_surface_obj.get_rect() + text_rect_obj.center = (450, 10) + main_board = get_starting_board() # Store the option button add their rectangle in Options - FENCE_SURF, FENCE_RECT = makeText( - "FENCE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 450 - ) - MOVE_SURF, MOVE_RECT = makeText( - "MOVE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 410 - ) - checkForQuit() - fenceClicked = False - (spotx, spoty) = (None, None) - moveClicked = False - slideTo = None # the direction to which any slide should slide - showStartScreen() + FENCE_SURF, FENCE_RECT = make_text("FENCE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 450) + MOVE_SURF, MOVE_RECT = make_text("MOVE", BUTTONTEXTCOLOR, BUTTONCOLOR, WINDOWWIDTH - 120, WINDOWHEIGHT - 410) + check_for_quit() + fence_clicked = False + (spot_x, spot_y) = (None, None) + move_clicked = False + # the direction to which any slide should slide + slide_to = None + show_start_screen() + while True: - msg = "Player turn-> " + str(playerChance) - mouseClicked = False - drawBoard(mainBoard, msg) - hasWon(mainBoard) - DISPLAYSURF.blit(textSurfaceObj, textRectObj) + msg = "Player turn-> " + str(player_chance) + mouse_clicked = False + draw_board(main_board, msg) + has_won(main_board) + DISPLAYSURF.blit(text_surface_obj, text_rect_obj) for event in pygame.event.get(): if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE): terminate() elif event.type == MOUSEMOTION: - mousex, mousey = event.pos + mouse_x, mouse_y = event.pos elif event.type == MOUSEBUTTONUP: - mousex, mousey = event.pos - mouseClicked = True - spotx, spoty = getSpotClicked(mainBoard, mousex, mousey) - if (spotx, spoty) == (None, None): + mouse_x, mouse_y = event.pos + mouse_clicked = True + spot_x, spot_y = get_spot_clicked(main_board, mouse_x, mouse_y) + if (spot_x, spot_y) == (None, None): # check for the button pressed - if FENCE_RECT.collidepoint((mousex, mousey)) and mouseClicked: + if FENCE_RECT.collidepoint((mouse_x, mouse_y)) and mouse_clicked: # Fence Button pressed - fenceClicked = True - moveClicked = False - mouseClicked = False - if MOVE_RECT.collidepoint((mousex, mousey)): + fence_clicked = True + move_clicked = False + mouse_clicked = False + if MOVE_RECT.collidepoint((mouse_x, mouse_y)): # MOVe button pressed - fenceClicked = False - moveClicked = True - mouseClicked = False + fence_clicked = False + move_clicked = True + mouse_clicked = False else: - playerx, playery = getPlayerPosition(mainBoard, playerChance) + player_x, player_y = get_player_position(main_board, player_chance) - if playerChance == 1: - onumber = 2 + if player_chance == 1: + o_number = 2 else: - onumber = 1 - opp_playerx, opp_playery = getPlayerPosition(mainBoard, onumber) - - if spotx != opp_playerx or spoty != opp_playery: - if spotx == playerx + 1 and spoty == playery: - slideTo = LEFT - elif spotx == playerx - 1 and spoty == playery: - slideTo = RIGHT - elif spotx == playerx and spoty == playery - 1: - slideTo = DOWN - elif spotx == playerx and spoty == playery + 1: - slideTo = UP + o_number = 1 + opp_player_x, opp_player_y = get_player_position(main_board, o_number) + + if spot_x != opp_player_x or spot_y != opp_player_y: + if spot_x == player_x + 1 and spot_y == player_y: + slide_to = LEFT + elif spot_x == player_x - 1 and spot_y == player_y: + slide_to = RIGHT + elif spot_x == player_x and spot_y == player_y - 1: + slide_to = DOWN + elif spot_x == player_x and spot_y == player_y + 1: + slide_to = UP ''' The below logic uses the opposing player's position and the current player's position to figure out which direction the car should jump ''' - if opp_playerx == playerx + 1 and opp_playery == playery: - if spotx == playerx + 2 and spoty == playery: - slideTo = JLEFTLEFT - if spotx == playerx + 1 and spoty == playery - 1: - slideTo = JLEFTDOWN - if spotx == playerx + 1 and spoty == playery + 1: - slideTo = JLEFTUP - - elif opp_playerx == playerx - 1 and opp_playery == playery: - if spotx == playerx - 2 and spoty == playery: - slideTo = JRIGHTRIGHT - if spotx == playerx - 1 and spoty == playery - 1: - slideTo = JRIGHTDOWN - if spotx == playerx - 1 and spoty == playery + 1: - slideTo = JRIGHTUP - - elif opp_playerx == playerx and opp_playery == playery - 1: - if spotx == playerx and spoty == playery - 2: - slideTo = JDOWNDOWN - if spoty == playery - 1 and spotx == playerx - 1: - slideTo = JDOWNRIGHT - if spoty == playery - 1 and spotx == playerx + 1: - slideTo = JDOWNLEFT - - elif opp_playerx == playerx and opp_playery == playery + 1: - if spotx == playerx and spoty == playery + 2: - slideTo = JUPUP - if spoty == playery + 1 and spotx == playerx - 1: - slideTo = JUPRIGHT - if spoty == playery + 1 and spotx == playerx + 1: - slideTo = JUPLEFT - - # print slideTo - if fenceClicked: - if fenceRemainingCount(playerChance) > 0: - drawBoard( - mainBoard, - "Player : " - + str(playerChance) - + " , Remaining fences : " - + str(fenceRemainingCount(playerChance)), - ) - drawFenceHighlight(spotx, spoty, HIGHLIGHTCOLOR, mousex, mousey) + if opp_player_x == player_x + 1 and opp_player_y == player_y: + if spot_x == player_x + 2 and spot_y == player_y: + slide_to = JLEFTLEFT + if spot_x == player_x + 1 and spot_y == player_y - 1: + slide_to = JLEFTDOWN + if spot_x == player_x + 1 and spot_y == player_y + 1: + slide_to = JLEFTUP + + elif opp_player_x == player_x - 1 and opp_player_y == player_y: + if spot_x == player_x - 2 and spot_y == player_y: + slide_to = JRIGHTRIGHT + if spot_x == player_x - 1 and spot_y == player_y - 1: + slide_to = JRIGHTDOWN + if spot_x == player_x - 1 and spot_y == player_y + 1: + slide_to = JRIGHTUP + + elif opp_player_x == player_x and opp_player_y == player_y - 1: + if spot_x == player_x and spot_y == player_y - 2: + slide_to = JDOWNDOWN + if spot_y == player_y - 1 and spot_x == player_x - 1: + slide_to = JDOWNRIGHT + if spot_y == player_y - 1 and spot_x == player_x + 1: + slide_to = JDOWNLEFT + + elif opp_player_x == player_x and opp_player_y == player_y + 1: + if spot_x == player_x and spot_y == player_y + 2: + slide_to = JUPUP + if spot_y == player_y + 1 and spot_x == player_x - 1: + slide_to = JUPRIGHT + if spot_y == player_y + 1 and spot_x == player_x + 1: + slide_to = JUPLEFT + + # print slide_to + if fence_clicked: + if fence_remaining_count(player_chance) > 0: + draw_board(main_board, "Player : {} , Remaining fences : {}" + .format(str(player_chance), str(fence_remaining_count(player_chance)))) + + draw_fence_highlight(spot_x, spot_y, HIGHLIGHTCOLOR, mouse_x, mouse_y) pygame.draw.rect(DISPLAYSURF, BLACK, FENCE_RECT, 2) - if mouseClicked: - if fencePutting(spotx, spoty, playerChance, mousex, mousey): - if validateFence( - mainBoard, playerChance, playerx, playery - ) and validateFence(mainBoard, onumber, opp_playerx, opp_playery): - playerChance = 2 if playerChance == 1 else 1 - fenceClicked = False + if mouse_clicked: + if fence_putting(spot_x, spot_y, player_chance, mouse_x, mouse_y): + if validate_fence(main_board, player_chance, player_x, player_y) and\ + validate_fence(main_board, o_number, opp_player_x, opp_player_y): + player_chance = 2 if player_chance == 1 else 1 + fence_clicked = False else: - hasWon(mainBoard, onumber) + has_won(main_board, o_number) else: - drawBoard( - mainBoard, + draw_board( + main_board, "Player : " - + str(playerChance) + + str(player_chance) + " , No more fences available please make a move", ) # tile pressed - elif moveClicked: - drawBoard(mainBoard, "Player " + str(playerChance) + " to move the car") + elif move_clicked: + draw_board(main_board, "Player " + str(player_chance) + " to move the car") pygame.draw.rect(DISPLAYSURF, BLACK, MOVE_RECT, 2) if ( - slideTo - and not mouseClicked - and validateMove(mainBoard, spotx, spoty, playerChance, slideTo) + slide_to + and not mouse_clicked + and validate_move(main_board, spot_x, spot_y, player_chance, slide_to) ): - drawHighlightTile(spotx, spoty, BLACK) + draw_highlight_tile(spot_x, spot_y, BLACK) if ( - slideTo - and mouseClicked - and validateMove(mainBoard, spotx, spoty, playerChance, slideTo) + slide_to + and mouse_clicked + and validate_move(main_board, spot_x, spot_y, player_chance, slide_to) ): - moveAnimation(mainBoard, slideTo, playerChance) - makeMove(mainBoard, slideTo, playerChance) - if playerChance == 1: - playerChance = 2 - elif playerChance == 2: - playerChance = 1 - moveClicked = False + move_animation(main_board, slide_to, player_chance) + make_move(main_board, slide_to, player_chance) + if player_chance == 1: + player_chance = 2 + elif player_chance == 2: + player_chance = 1 + move_clicked = False pygame.display.update() - slideTo = None + slide_to = None FPSCLOCK.tick(FPS) @@ -189,7 +185,7 @@ def terminate(): sys.exit() -def checkForQuit(): +def check_for_quit(): for event in pygame.event.get(QUIT): terminate() for event in pygame.event.get(KEYUP): @@ -198,13 +194,13 @@ def checkForQuit(): pygame.event.post(event) -def getLeftTopOfTile(tileX, tileY): - left = XMARGIN + (tileX * TILESIZE) + (tileX - 1) - top = YMARGIN + (tileY * TILESIZE) + (tileY - 1) - return (left, top) +def get_left_top_of_tile(tile_x, tile_y): + left = XMARGIN + (tile_x * TILESIZE) + (tile_x - 1) + top = YMARGIN + (tile_y * TILESIZE) + (tile_y - 1) + return left, top -def getStartingBoard(state=OPEN_SPACE): +def get_starting_board(state=OPEN_SPACE): # Returns the board data structure with tiles in it board = [] @@ -218,503 +214,418 @@ def getStartingBoard(state=OPEN_SPACE): return board -def hasWon(board, playerwon=-1): +def has_won(board, player_won=-1): # checks for winning player - color1 = LIGHTBGCOLOR - color2 = BGCOLOR - if board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][0] == 1 or playerwon == 1: + color_1 = LIGHTBGCOLOR + color_2 = BGCOLOR + if board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][0] == 1 or player_won == 1: # player 1 has won show it and exit for i in range(13): - color1, color2 = color2, color1 - drawBoard(board, "Player 1 has won", color1) + color_1, color_2 = color_2, color_1 + draw_board(board, "Player 1 has won", color_1) pygame.display.update() pygame.time.wait(500) terminate() - elif ( - board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][BOARDHEIGHT - 1] == 2 - or playerwon == 2 - ): + elif board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][BOARDHEIGHT - 1] == 2 or player_won == 2: # player 2 has won show it and exit - for i in range(13): - color1, color2 = color2, color1 - drawBoard(board, "Player 2 has won", color1) + color_1, color_2 = color_2, color_1 + draw_board(board, "Player 2 has won", color_1) pygame.display.update() pygame.time.wait(500) terminate() -def drawTile(tileX, tileY, number, adjx=0, adjy=0): +def draw_tile(tile_x, tile_y, number, adj_x=0, adj_y=0): # draw a tile at tileX ad tileY # pixels over determined by adjx and adjy - left, top = getLeftTopOfTile(tileX, tileY) - pygame.draw.rect( - DISPLAYSURF, TILECOLOR, (left + adjx, top + adjy, TILESIZE, TILESIZE) - ) - if tileX == 3 and tileY == 0: - textSurf = BASICFONT.render(str(number), True, TEXTCOLOR) - textRect = textSurf.get_rect() - adjy = 11 - adjx = 15 - textRect.center = left + adjx, top + adjy - DISPLAYSURF.blit(flagImg1, textRect) - if tileX == 3 and tileY == BOARDHEIGHT - 1: - textSurf = BASICFONT.render(str(number), True, TEXTCOLOR) - textRect = textSurf.get_rect() - adjy = 35 - adjx = 20 - textRect.center = left + adjx, top + adjy - DISPLAYSURF.blit(flagImg, textRect) + left, top = get_left_top_of_tile(tile_x, tile_y) + pygame.draw.rect(DISPLAYSURF, TILECOLOR, (left + adj_x, top + adj_y, TILESIZE, TILESIZE)) + if tile_x == 3 and tile_y == 0: + text_surf = BASICFONT.render(str(number), True, TEXTCOLOR) + text_rect = text_surf.get_rect() + adj_y = 11 + adj_x = 15 + text_rect.center = left + adj_x, top + adj_y + DISPLAYSURF.blit(flagImg1, text_rect) + if tile_x == 3 and tile_y == BOARDHEIGHT - 1: + text_surf = BASICFONT.render(str(number), True, TEXTCOLOR) + text_rect = text_surf.get_rect() + adj_y = 35 + adj_x = 20 + text_rect.center = left + adj_x, top + adj_y + DISPLAYSURF.blit(flagImg, text_rect) if number == 2 or number == 1: - - textSurf = BASICFONT.render(str(number), True, TEXTCOLOR) - textRect = textSurf.get_rect() - textRect.center = left + adjx, top + adjy + text_surf = BASICFONT.render(str(number), True, TEXTCOLOR) + text_rect = text_surf.get_rect() + text_rect.center = left + adj_x, top + adj_y if number == 2: - adjx = 11 - adjy = 11 - textRect.center = left + adjx, top + adjy - DISPLAYSURF.blit(carImg1, textRect) + adj_x = 11 + adj_y = 11 + text_rect.center = left + adj_x, top + adj_y + DISPLAYSURF.blit(car_img_1, text_rect) else: - adjx = 4 - adjy = 6 - textRect.center = left + adjx, top + adjy - DISPLAYSURF.blit(carImg, textRect) - - for position in player1Fence: - drawFenceHighlight( - position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9 - ) - for position in player2Fence: - drawFenceHighlight( - position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9 - ) - - -def getPlayerPosition(board, number): + adj_x = 4 + adj_y = 6 + text_rect.center = left + adj_x, top + adj_y + DISPLAYSURF.blit(car_img, text_rect) + + for position in player_1_fence: + draw_fence_highlight(position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9) + for position in player_2_fence: + draw_fence_highlight(position[0][0], position[0][1], BLACK, position[1][0], position[1][1], 9) + + +def get_player_position(board, number): # return the x,y co=ordinates of the blank box for x in range(BOARDWIDTH): for y in range(BOARDHEIGHT): if board[x][y] == number: - return (x, y) + return x, y -def makeMove(board, move, number): +def make_move(board, move, number): # just makes a move not checks if it's valid or not - - playerx, playery = getPlayerPosition(board, number) + player_x, player_y = get_player_position(board, number) if move == UP: - board[playerx][playery], board[playerx][playery + 1] = ( - board[playerx][playery + 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x][player_y + 1] = (board[player_x][player_y + 1], + board[player_x][player_y]) elif move == DOWN: - board[playerx][playery], board[playerx][playery - 1] = ( - board[playerx][playery - 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x][player_y - 1] = (board[player_x][player_y - 1], + board[player_x][player_y]) elif move == RIGHT: - board[playerx][playery], board[playerx - 1][playery] = ( - board[playerx - 1][playery], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x - 1][player_y] = (board[player_x - 1][player_y], + board[player_x][player_y]) elif move == LEFT: - board[playerx][playery], board[playerx + 1][playery] = ( - board[playerx + 1][playery], - board[playerx][playery], - ) - + board[player_x][player_y], board[player_x + 1][player_y] = (board[player_x + 1][player_y], + board[player_x][player_y]) elif move == JUPUP: - board[playerx][playery], board[playerx][playery + 2] = ( - board[playerx][playery + 2], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x][player_y + 2] = (board[player_x][player_y + 2], + board[player_x][player_y]) elif move == JUPLEFT: - board[playerx][playery], board[playerx + 1][playery + 1] = ( - board[playerx + 1][playery + 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x + 1][player_y + 1] = (board[player_x + 1][player_y + 1], + board[player_x][player_y]) elif move == JUPRIGHT: - board[playerx][playery], board[playerx - 1][playery + 1] = ( - board[playerx - 1][playery + 1], - board[playerx][playery], - ) - + board[player_x][player_y], board[player_x - 1][player_y + 1] = (board[player_x - 1][player_y + 1], + board[player_x][player_y]) elif move == JDOWNDOWN: - board[playerx][playery], board[playerx][playery - 2] = ( - board[playerx][playery - 2], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x][player_y - 2] = (board[player_x][player_y - 2], + board[player_x][player_y]) elif move == JDOWNLEFT: - board[playerx][playery], board[playerx + 1][playery - 1] = ( - board[playerx + 1][playery - 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x + 1][player_y - 1] = (board[player_x + 1][player_y - 1], + board[player_x][player_y]) elif move == JDOWNRIGHT: - board[playerx][playery], board[playerx - 1][playery - 1] = ( - board[playerx - 1][playery - 1], - board[playerx][playery], - ) - + board[player_x][player_y], board[player_x - 1][player_y - 1] = (board[player_x - 1][player_y - 1], + board[player_x][player_y]) elif move == JRIGHTRIGHT: - board[playerx][playery], board[playerx - 2][playery] = ( - board[playerx - 2][playery], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x - 2][player_y] = (board[player_x - 2][player_y], + board[player_x][player_y]) elif move == JRIGHTUP: - board[playerx][playery], board[playerx - 1][playery + 1] = ( - board[playerx - 1][playery + 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x - 1][player_y + 1] = (board[player_x - 1][player_y + 1], + board[player_x][player_y]) elif move == JRIGHTDOWN: - board[playerx][playery], board[playerx - 1][playery - 1] = ( - board[playerx - 1][playery - 1], - board[playerx][playery], - ) - + board[player_x][player_y], board[player_x - 1][player_y - 1] = (board[player_x - 1][player_y - 1], + board[player_x][player_y]) elif move == JLEFTLEFT: - board[playerx][playery], board[playerx + 2][playery] = ( - board[playerx + 2][playery], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x + 2][player_y] = (board[player_x + 2][player_y], + board[player_x][player_y]) elif move == JLEFTUP: - board[playerx][playery], board[playerx + 1][playery + 1] = ( - board[playerx + 1][playery + 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x + 1][player_y + 1] = (board[player_x + 1][player_y + 1], + board[player_x][player_y]) elif move == JLEFTDOWN: - board[playerx][playery], board[playerx + 1][playery - 1] = ( - board[playerx + 1][playery - 1], - board[playerx][playery], - ) + board[player_x][player_y], board[player_x + 1][player_y - 1] = (board[player_x + 1][player_y - 1], + board[player_x][player_y]) + pygame.display.update() FPSCLOCK.tick(FPS) -def drawBoard(board, message, color1=BGCOLOR): +def draw_board(board, message, color1=BGCOLOR): DISPLAYSURF.fill(color1) if message: - textSurf, textRect = makeText(message, MESSAGECOLOR, color1, 5, 5) - DISPLAYSURF.blit(textSurf, textRect) - for tilex in range(len(board)): - for tiley in range(len(board[0])): - if board[tilex][tiley]: - drawTile(tilex, tiley, board[tilex][tiley]) - left, top = getLeftTopOfTile(0, 0) + text_surf, text_rect = make_text(message, MESSAGECOLOR, color1, 5, 5) + DISPLAYSURF.blit(text_surf, text_rect) + for tile_x in range(len(board)): + for tile_y in range(len(board[0])): + if board[tile_x][tile_y]: + draw_tile(tile_x, tile_y, board[tile_x][tile_y]) + left, top = get_left_top_of_tile(0, 0) width = BOARDWIDTH * TILESIZE height = BOARDHEIGHT * TILESIZE - pygame.draw.rect( - DISPLAYSURF, BORDERCOLOR, (left - 5, top - 5, width + 11, height + 11), 6 - ) + pygame.draw.rect(DISPLAYSURF, BORDERCOLOR, (left - 5, top - 5, width + 11, height + 11), 6) DISPLAYSURF.blit(FENCE_SURF, FENCE_RECT) DISPLAYSURF.blit(MOVE_SURF, MOVE_RECT) - # DISPLAYSURF.blit(SOLVE_SURF,SOLVE_RECT) -def getSpotClicked(board, x, y): +def get_spot_clicked(board, x, y): # from x,and y pixel co-ordinates get x and y box co-odnate - for tileX in range(len(board)): - for tileY in range(len(board[0])): - left, top = getLeftTopOfTile(tileX, tileY) - tileRect = pygame.Rect(left, top, TILESIZE, TILESIZE) - if tileRect.collidepoint(x, y): - return (tileX, tileY) - return (None, None) - - -def drawFenceHighlight(tileX, tileY, highLightColor, mousex, mousey, fence_thickness=4): - left, top = getLeftTopOfTile(tileX, tileY) - Ox, Oy = getLeftTopOfTile(0, 0) - if mousex > mousey and mousex + mousey < (top + left + TILESIZE): - if left <= Ox + 6 * TILESIZE and top != Oy: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left, top), - (left + 2 * TILESIZE, top), - fence_thickness, - ) - elif left > Ox + 6 * TILESIZE and top != Oy: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (Ox + 5 * TILESIZE + 5, top), - (Ox + 7 * TILESIZE + 2, top), - fence_thickness, - ) - - elif mousex > mousey and mousex + mousey > (top + left + TILESIZE): - if top <= Oy + 6 * TILESIZE and left < Ox + 6 * TILESIZE: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left + TILESIZE, top), - (left + TILESIZE, top + 2 * TILESIZE), - fence_thickness, - ) - elif top > Oy + 6 * TILESIZE and left < Ox + 6 * TILESIZE: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left + TILESIZE, Oy + 5 * TILESIZE + 1), - (left + TILESIZE, Oy + 7 * TILESIZE + 1), - fence_thickness, - ) - - elif mousex < mousey and mousex + mousey < (top + left + TILESIZE): - if top <= Oy + 6 * TILESIZE and left != Ox: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left, top), - (left, top + 2 * TILESIZE), - fence_thickness, - ) - elif top > Oy + 6 * TILESIZE and left != Ox: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left, Oy + 5 * TILESIZE + 1), - (left, Oy + 7 * TILESIZE + 1), - fence_thickness, - ) - - elif mousex < mousey and mousex + mousey > (top + left + TILESIZE): - if left <= Ox + 6 * TILESIZE and top < Oy + 6 * TILESIZE: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (left, top + TILESIZE), - (left + 2 * TILESIZE, top + TILESIZE), - fence_thickness, - ) - elif left > Ox + 6 * TILESIZE and top < Oy + 6 * TILESIZE: - pygame.draw.line( - DISPLAYSURF, - highLightColor, - (Ox + 5 * TILESIZE, top + TILESIZE), - (Ox + 7 * TILESIZE + 2, top + TILESIZE), - fence_thickness, - ) - - -def makeText(text, color, bgcolor, top, left): + for tile_x in range(len(board)): + for tile_y in range(len(board[0])): + left, top = get_left_top_of_tile(tile_x, tile_y) + tile_rect = pygame.Rect(left, top, TILESIZE, TILESIZE) + if tile_rect.collidepoint(x, y): + return tile_x, tile_y + return None, None + + +def draw_fence_highlight(tile_x, tile_y, highlight_color, mouse_x, mouse_y, fence_thickness=4): + left, top = get_left_top_of_tile(tile_x, tile_y) + o_x, o_y = get_left_top_of_tile(0, 0) + + if mouse_x > mouse_y and mouse_x + mouse_y < (top + left + TILESIZE): + if left <= o_x + 6 * TILESIZE and top != o_y: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left, top), + (left + 2 * TILESIZE, top), + fence_thickness) + elif left > o_x + 6 * TILESIZE and top != o_y: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (o_x + 5 * TILESIZE + 5, top), + (o_x + 7 * TILESIZE + 2, top), + fence_thickness) + + elif mouse_x > mouse_y and mouse_x + mouse_y > (top + left + TILESIZE): + if top <= o_y + 6 * TILESIZE and left < o_x + 6 * TILESIZE: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left + TILESIZE, top), + (left + TILESIZE, top + 2 * TILESIZE), + fence_thickness) + elif top > o_y + 6 * TILESIZE and left < o_x + 6 * TILESIZE: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left + TILESIZE, o_y + 5 * TILESIZE + 1), + (left + TILESIZE, o_y + 7 * TILESIZE + 1), + fence_thickness) + + elif mouse_x < mouse_y and mouse_x + mouse_y < (top + left + TILESIZE): + if top <= o_y + 6 * TILESIZE and left != o_x: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left, top), + (left, top + 2 * TILESIZE), + fence_thickness) + elif top > o_y + 6 * TILESIZE and left != o_x: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left, o_y + 5 * TILESIZE + 1), + (left, o_y + 7 * TILESIZE + 1), + fence_thickness) + + elif mouse_x < mouse_y and mouse_x + mouse_y > (top + left + TILESIZE): + if left <= o_x + 6 * TILESIZE and top < o_y + 6 * TILESIZE: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (left, top + TILESIZE), + (left + 2 * TILESIZE, top + TILESIZE), + fence_thickness) + elif left > o_x + 6 * TILESIZE and top < o_y + 6 * TILESIZE: + pygame.draw.line(DISPLAYSURF, + highlight_color, + (o_x + 5 * TILESIZE, top + TILESIZE), + (o_x + 7 * TILESIZE + 2, top + TILESIZE), + fence_thickness) + + +def make_text(text, color, bg_color, top, left): # create the surface and rect object for some text - textSurf = BASICFONT.render(text, True, color, bgcolor) - textRect = textSurf.get_rect() - textRect.topleft = (top, left) - return (textSurf, textRect) + text_surf = BASICFONT.render(text, True, color, bg_color) + text_rect = text_surf.get_rect() + text_rect.topleft = (top, left) + return text_surf, text_rect -def moveAnimation(board, direction, number, message="", jx=0, jy=0): - playerx, playery = getPlayerPosition(board, number) - drawBoard(board, message) - baseSurf = DISPLAYSURF.copy() +def move_animation(board, direction, number, message="", jx=0, jy=0): + player_x, player_y = get_player_position(board, number) + draw_board(board, message) + base_surf = DISPLAYSURF.copy() # Drawing a base surface for temporary movement - if playerx == 3 and playery == 0: - textSurf = BASICFONT.render(str(number), True, TEXTCOLOR) - textRect = textSurf.get_rect() - adjy = 11 - adjx = 15 - moveLeft, moveTop = getLeftTopOfTile(playerx, playery) - textRect.center = moveLeft + adjx, moveTop + adjy - pygame.draw.rect( - baseSurf, TILECOLOR, (moveLeft, moveTop, TILESIZE - 2, TILESIZE - 4) - ) - baseSurf.blit(flagImg1, textRect) - elif playerx == 3 and playery == BOARDHEIGHT - 1: - textSurf = BASICFONT.render(str(number), True, TEXTCOLOR) - textRect = textSurf.get_rect() - adjy = 35 - adjx = 20 - moveLeft, moveTop = getLeftTopOfTile(playerx, playery) - textRect.center = moveLeft + adjx, moveTop + adjy - pygame.draw.rect( - baseSurf, TILECOLOR, (moveLeft, moveTop, TILESIZE - 2, TILESIZE - 3) - ) - baseSurf.blit(flagImg, textRect) + if player_x == 3 and player_y == 0: + text_surf = BASICFONT.render(str(number), True, TEXTCOLOR) + text_rect = text_surf.get_rect() + adj_y = 11 + adj_x = 15 + move_left, move_top = get_left_top_of_tile(player_x, player_y) + text_rect.center = move_left + adj_x, move_top + adj_y + pygame.draw.rect(base_surf, TILECOLOR, (move_left, move_top, TILESIZE - 2, TILESIZE - 4)) + base_surf.blit(flagImg1, text_rect) + elif player_x == 3 and player_y == BOARDHEIGHT - 1: + text_surf = BASICFONT.render(str(number), True, TEXTCOLOR) + text_rect = text_surf.get_rect() + adj_y = 35 + adj_x = 20 + move_left, move_top = get_left_top_of_tile(player_x, player_y) + text_rect.center = move_left + adj_x, move_top + adj_y + pygame.draw.rect(base_surf, TILECOLOR, (move_left, move_top, TILESIZE - 2, TILESIZE - 3)) + base_surf.blit(flagImg, text_rect) else: - moveLeft, moveTop = getLeftTopOfTile(playerx, playery) - pygame.draw.rect( - baseSurf, TILECOLOR, (moveLeft + 4, moveTop + 4, TILESIZE - 7, TILESIZE - 7) - ) + move_left, move_top = get_left_top_of_tile(player_x, player_y) + pygame.draw.rect(base_surf, TILECOLOR, (move_left + 4, move_top + 4, TILESIZE - 7, TILESIZE - 7)) - # checking for jump conditions + # checking for jump conditions if direction == JUPUP: - moveAnimation(board, UP, number) - moveAnimation(board, UP, number, "", 0, 1) + move_animation(board, UP, number) + move_animation(board, UP, number, "", 0, 1) return elif direction == JUPRIGHT: - moveAnimation(board, UP, number) - moveAnimation(board, RIGHT, number, "", 0, 1) + move_animation(board, UP, number) + move_animation(board, RIGHT, number, "", 0, 1) return elif direction == JUPLEFT: - moveAnimation(board, UP, number) - moveAnimation(board, LEFT, number, "", 0, 1) + move_animation(board, UP, number) + move_animation(board, LEFT, number, "", 0, 1) return elif direction == JDOWNDOWN: - moveAnimation(board, DOWN, number) - moveAnimation(board, DOWN, number, "", 0, -1) + move_animation(board, DOWN, number) + move_animation(board, DOWN, number, "", 0, -1) return elif direction == JDOWNLEFT: - moveAnimation(board, DOWN, number) - moveAnimation(board, LEFT, number, "", 0, -1) + move_animation(board, DOWN, number) + move_animation(board, LEFT, number, "", 0, -1) return elif direction == JDOWNRIGHT: - moveAnimation(board, DOWN, number) - moveAnimation(board, RIGHT, number, "", 0, -1) + move_animation(board, DOWN, number) + move_animation(board, RIGHT, number, "", 0, -1) return elif direction == JRIGHTRIGHT: - moveAnimation(board, RIGHT, number) - moveAnimation(board, RIGHT, number, "", -1, 0) + move_animation(board, RIGHT, number) + move_animation(board, RIGHT, number, "", -1, 0) return elif direction == JRIGHTUP: - moveAnimation(board, RIGHT, number) - moveAnimation(board, UP, number, "", -1, 0) + move_animation(board, RIGHT, number) + move_animation(board, UP, number, "", -1, 0) return elif direction == JRIGHTDOWN: - moveAnimation(board, RIGHT, number) - moveAnimation(board, DOWN, number, "", -1, 0) + move_animation(board, RIGHT, number) + move_animation(board, DOWN, number, "", -1, 0) return elif direction == JLEFTLEFT: - moveAnimation(board, LEFT, number) - moveAnimation(board, LEFT, number, "", 1, 0) + move_animation(board, LEFT, number) + move_animation(board, LEFT, number, "", 1, 0) return elif direction == JLEFTUP: - moveAnimation(board, LEFT, number) - moveAnimation(board, UP, number, "", 1, 0) + move_animation(board, LEFT, number) + move_animation(board, UP, number, "", 1, 0) return elif direction == JLEFTDOWN: - moveAnimation(board, LEFT, number) - moveAnimation(board, DOWN, number, "", 1, 0) + move_animation(board, LEFT, number) + move_animation(board, DOWN, number, "", 1, 0) return - drawBoard(board) - # for double animation if required - moveLeft, moveTop = getLeftTopOfTile(playerx + jx, playery + jy) + # for double animation if required + move_left, move_top = get_left_top_of_tile(player_x + jx, player_y + jy) for i in range(0, TILESIZE, ANIMATIONSPEED): - checkForQuit() - DISPLAYSURF.blit(baseSurf, (0, 0)) + check_for_quit() + DISPLAYSURF.blit(base_surf, (0, 0)) if number == 1: if direction == UP: - # drawTile(movex,movey,board[movex][movey],0,-i) - DISPLAYSURF.blit(carImg, (moveLeft, moveTop + i)) + # drawTile(move_x,move_y,board[move_x][move_y],0,-i) + DISPLAYSURF.blit(car_img, (move_left, move_top + i)) elif direction == DOWN: - # drawTile(movex,movey,board[movex][movey],0,i) - DISPLAYSURF.blit(carImg, (moveLeft, moveTop - i)) + # drawTile(move_x,move_y,board[move_x][move_y],0,i) + DISPLAYSURF.blit(car_img, (move_left, move_top - i)) elif direction == RIGHT: - # drawTile(movex,movey,board[movex][movey],i,0) - DISPLAYSURF.blit(carImg, (moveLeft - i, moveTop)) + # drawTile(move_x,move_y,board[move_x][move_y],i,0) + DISPLAYSURF.blit(car_img, (move_left - i, move_top)) elif direction == LEFT: - # drawTile(movex,movey,board[movex][movey],-i,0) - DISPLAYSURF.blit(carImg, (moveLeft + i, moveTop)) + # drawTile(move_x,move_y,board[move_x][move_y],-i,0) + DISPLAYSURF.blit(car_img, (move_left + i, move_top)) else: if direction == UP: - # drawTile(movex,movey,board[movex][movey],0,-i) - DISPLAYSURF.blit(carImg1, (moveLeft, moveTop + i)) + # drawTile(move_x,move_y,board[move_x][move_y],0,-i) + DISPLAYSURF.blit(car_img_1, (move_left, move_top + i)) elif direction == DOWN: - # drawTile(movex,movey,board[movex][movey],0,i) - DISPLAYSURF.blit(carImg1, (moveLeft, moveTop - i)) + # drawTile(move_x,move_y,board[move_x][move_y],0,i) + DISPLAYSURF.blit(car_img_1, (move_left, move_top - i)) elif direction == RIGHT: - # drawTile(movex,movey,board[movex][movey],i,0) - DISPLAYSURF.blit(carImg1, (moveLeft - i, moveTop)) + # drawTile(move_x,move_y,board[move_x][move_y],i,0) + DISPLAYSURF.blit(car_img_1, (move_left - i, move_top)) elif direction == LEFT: - # drawTile(movex,movey,board[movex][movey],-i,0) - DISPLAYSURF.blit(carImg1, (moveLeft + i, moveTop)) + # drawTile(move_x,move_y,board[move_x][move_y],-i,0) + DISPLAYSURF.blit(car_img_1, (move_left + i, move_top)) pygame.display.update() FPSCLOCK.tick(FPS) -def fencePutting(tileX, tileY, playerChance, mousex, mousey): - # puts the fence on the board and record it's positon in the array - left, top = getLeftTopOfTile(tileX, tileY) - Ox, Oy = getLeftTopOfTile(0, 0) +def fence_putting(tile_x, tile_y, player_chance, mouse_x, mouse_y): + # puts the fence on the board and record it's position in the array + left, top = get_left_top_of_tile(tile_x, tile_y) + o_x, o_y = get_left_top_of_tile(0, 0) flag = 0 # fence not on the border of the board then only append else return false - if mousex > mousey and mousex + mousey < (top + left + TILESIZE): - if ( - left <= Ox + 6 * TILESIZE - and top != Oy - and fenceLine(tileX, tileY, tileX + 2, tileY) - ): - TotalFence.append(((tileX, tileY), (tileX + 2, tileY))) + if mouse_x > mouse_y and mouse_x + mouse_y < (top + left + TILESIZE): + if left <= o_x + 6 * TILESIZE and\ + top != o_y and\ + fence_line(tile_x, tile_y, tile_x + 2, tile_y): + total_fence.append(((tile_x, tile_y), (tile_x + 2, tile_y))) flag = 1 - elif ( - left > Ox + 6 * TILESIZE and top != Oy and fenceLine(5, tileY, 5 + 2, tileY) - ): - TotalFence.append(((5, tileY), (7, tileY))) + elif left > o_x + 6 * TILESIZE and\ + top != o_y and\ + fence_line(5, tile_y, 5 + 2, tile_y): + total_fence.append(((5, tile_y), (7, tile_y))) flag = 1 - - elif mousex > mousey and mousex + mousey > (top + left + TILESIZE): - if ( - top <= Oy + 6 * TILESIZE - and left < Ox + 6 * TILESIZE - and fenceLine(tileX + 1, tileY, tileX + 1, tileY + 2) - ): - TotalFence.append(((tileX + 1, tileY), (tileX + 1, tileY + 2))) + elif mouse_x > mouse_y and mouse_x + mouse_y > (top + left + TILESIZE): + if top <= o_y + 6 * TILESIZE and\ + left < o_x + 6 * TILESIZE and\ + fence_line(tile_x + 1, tile_y, tile_x + 1, tile_y + 2): + total_fence.append(((tile_x + 1, tile_y), (tile_x + 1, tile_y + 2))) flag = 1 - elif ( - top > Oy + 6 * TILESIZE - and left < Ox + 6 * TILESIZE - and fenceLine(tileX + 1, 5, tileX + 1, 5 + 2) - ): - TotalFence.append(((tileX + 1, 5), (tileX + 1, 7))) + elif top > o_y + 6 * TILESIZE and\ + left < o_x + 6 * TILESIZE and\ + fence_line(tile_x + 1, 5, tile_x + 1, 5 + 2): + total_fence.append(((tile_x + 1, 5), (tile_x + 1, 7))) flag = 1 - elif mousex < mousey and mousex + mousey < (top + left + TILESIZE): - if ( - top <= Oy + 6 * TILESIZE - and left != Ox - and fenceLine(tileX, tileY, tileX, tileY + 2) - ): - TotalFence.append(((tileX, tileY), (tileX, tileY + 2))) + elif mouse_x < mouse_y and mouse_x + mouse_y < (top + left + TILESIZE): + if top <= o_y + 6 * TILESIZE and\ + left != o_x and\ + fence_line(tile_x, tile_y, tile_x, tile_y + 2): + total_fence.append(((tile_x, tile_y), (tile_x, tile_y + 2))) flag = 1 - elif ( - top > Oy + 6 * TILESIZE and left != Ox and fenceLine(tileX, 5, tileX, 5 + 2) - ): - TotalFence.append(((tileX, 5), (tileX, 5 + 2))) + elif top > o_y + 6 * TILESIZE and\ + left != o_x and\ + fence_line(tile_x, 5, tile_x, 5 + 2): + total_fence.append(((tile_x, 5), (tile_x, 5 + 2))) flag = 1 - elif mousex < mousey and mousex + mousey > (top + left + TILESIZE): - if ( - left <= Ox + 6 * TILESIZE - and top < Oy + 6 * TILESIZE - and fenceLine(tileX, tileY + 1, tileX + 2, tileY + 1) - ): - TotalFence.append(((tileX, tileY + 1), (tileX + 2, tileY + 1))) + elif mouse_x < mouse_y and mouse_x + mouse_y > (top + left + TILESIZE): + if left <= o_x + 6 * TILESIZE and\ + top < o_y + 6 * TILESIZE and\ + fence_line(tile_x, tile_y + 1, tile_x + 2, tile_y + 1): + total_fence.append(((tile_x, tile_y + 1), (tile_x + 2, tile_y + 1))) flag = 1 - elif ( - left > Ox + 6 * TILESIZE - and top < Oy + 6 * TILESIZE - and fenceLine(5, tileY + 1, 5 + 2, tileY + 1) - ): - TotalFence.append(((5, tileY + 1), (7, tileY + 1))) + elif left > o_x + 6 * TILESIZE and\ + top < o_y + 6 * TILESIZE and\ + fence_line(5, tile_y + 1, 5 + 2, tile_y + 1): + total_fence.append(((5, tile_y + 1), (7, tile_y + 1))) flag = 1 - if playerChance == 1 and flag == 1: - player1Fence.append(((tileX, tileY), (mousex, mousey))) + + if player_chance == 1 and flag == 1: + player_1_fence.append(((tile_x, tile_y), (mouse_x, mouse_y))) return True - elif playerChance == 2 and flag == 1: - player2Fence.append(((tileX, tileY), (mousex, mousey))) + elif player_chance == 2 and flag == 1: + player_2_fence.append(((tile_x, tile_y), (mouse_x, mouse_y))) return True + return False -def drawHighlightTile(tileX, tileY, highLightColor, tile_thickness=4): +def draw_highlight_tile(tile_x, tile_y, highlight_color, tile_thickness=4): # highlight the box where car may move based on mouse hover - left, top = getLeftTopOfTile(tileX, tileY) - pygame.draw.rect( - DISPLAYSURF, - highLightColor, - (left - 4, top - 4, TILESIZE + 5, TILESIZE + 5), - tile_thickness, - ) + left, top = get_left_top_of_tile(tile_x, tile_y) + pygame.draw.rect(DISPLAYSURF, + highlight_color, + (left - 4, top - 4, TILESIZE + 5, TILESIZE + 5), + tile_thickness) -def gameWonAnimation(): +def game_won_animation(): color1 = LIGHTBGCOLOR color2 = BGCOLOR for i in range(13): @@ -722,273 +633,197 @@ def gameWonAnimation(): DISPLAYSURF.fill(color1) -def validateMove(board, tileX, tileY, playerChance, direction, playerx=-1, playery=-1): - if playerx == -1: # no parameter recieved, initialize to actual parameter - playerx, playery = getPlayerPosition(board, playerChance) - if playerChance == 1: - onumber = 2 +def validate_move(board, tile_x, tile_y, player_chance, direction, player_x=-1, player_y=-1): + if player_x == -1: # no parameter received, initialize to actual parameter + player_x, player_y = get_player_position(board, player_chance) + if player_chance == 1: + o_number = 2 else: - onumber = 1 - opp_playerx, opp_playery = getPlayerPosition(board, onumber) - for position in TotalFence: + o_number = 1 + + for position in total_fence: if position[0][0] == position[1][0]: # equation x-position[0][0] if position[0][1] > position[1][1]: - maxy = position[0][1] - miny = position[1][1] + max_y = position[0][1] + min_y = position[1][1] else: - miny = position[0][1] - maxy = position[1][1] + min_y = position[0][1] + max_y = position[1][1] - if tileY < maxy and tileY >= miny and playery < maxy and playery >= miny: - if playerx < position[0][0]: - if (playerx - position[0][0]) * (tileX - position[0][0] + 0.1) < 0: + if max_y > tile_y >= min_y and max_y > player_y >= min_y: + if player_x < position[0][0]: + if (player_x - position[0][0]) * (tile_x - position[0][0] + 0.1) < 0: return False # Invalid move - elif playerx == position[0][0]: - if (playerx - position[0][0] + 0.1) * (tileX - position[0][0]) < 0: + elif player_x == position[0][0]: + if (player_x - position[0][0] + 0.1) * (tile_x - position[0][0]) < 0: return False else: - if (playerx - position[0][0] - 0.1) * ( - tileX - position[0][0] + 0.1 - ) < 0: + if (player_x - position[0][0] - 0.1) * (tile_x - position[0][0] + 0.1) < 0: return False # Invalid move if position[0][1] == position[1][1]: # equation y-position[1][1] if position[0][0] > position[1][0]: - maxx = position[0][0] - minx = position[1][0] + max_x = position[0][0] + min_x = position[1][0] else: - minx = position[0][0] - maxx = position[1][0] - if tileX < maxx and tileX >= minx and playerx < maxx and playerx >= minx: - if playery < position[0][1]: - - if (playery - position[0][1]) * (tileY - position[0][1] + 0.1) < 0: + min_x = position[0][0] + max_x = position[1][0] + if max_x > tile_x >= min_x and max_x > player_x >= min_x: + if player_y < position[0][1]: + if (player_y - position[0][1]) * (tile_y - position[0][1] + 0.1) < 0: return False - elif playery == position[0][1]: - if (playery - position[0][1] + 0.1) * (tileY - position[0][1]) < 0: + elif player_y == position[0][1]: + if (player_y - position[0][1] + 0.1) * (tile_y - position[0][1]) < 0: return False else: - if (playery - position[0][1] - 0.1) * ( - tileY - position[0][1] + 0.1 - ) < 0: + if (player_y - position[0][1] - 0.1) * (tile_y - position[0][1] + 0.1) < 0: return False - # In case of jump fence should not be crossed + + # In case of jump fence should not be crossed if direction == JUPRIGHT: - return validateMove( - board, playerx, playery + 1, playerChance, UP - ) and validateMove(board, tileX, tileY, onumber, RIGHT) + return validate_move(board, player_x, player_y + 1, player_chance, UP) and \ + validate_move(board, tile_x, tile_y, o_number, RIGHT) if direction == JUPLEFT: - return validateMove( - board, playerx, playery + 1, playerChance, UP - ) and validateMove(board, tileX, tileY, onumber, LEFT) + return validate_move(board, player_x, player_y + 1, player_chance, UP) and \ + validate_move(board, tile_x, tile_y, o_number, LEFT) if direction == JDOWNLEFT: - return validateMove( - board, playerx, playery - 1, playerChance, DOWN - ) and validateMove(board, tileX, tileY, onumber, LEFT) + return validate_move(board, player_x, player_y - 1, player_chance, DOWN) and \ + validate_move(board, tile_x, tile_y, o_number, LEFT) if direction == JDOWNRIGHT: - return validateMove( - board, playerx, playery - 1, playerChance, DOWN - ) and validateMove(board, tileX, tileY, onumber, RIGHT) + return validate_move(board, player_x, player_y - 1, player_chance, DOWN) and \ + validate_move(board, tile_x, tile_y, o_number, RIGHT) + if direction == JRIGHTUP: - return validateMove( - board, playerx - 1, playery, playerChance, RIGHT - ) and validateMove(board, tileX, tileY, onumber, UP) + return validate_move(board, player_x - 1, player_y, player_chance, RIGHT) and \ + validate_move(board, tile_x, tile_y, o_number, UP) + if direction == JRIGHTDOWN: - return validateMove( - board, playerx - 1, playery, playerChance, RIGHT - ) and validateMove(board, tileX, tileY, onumber, DOWN) + return validate_move(board, player_x - 1, player_y, player_chance, RIGHT) and \ + validate_move(board, tile_x, tile_y, o_number, DOWN) + if direction == JLEFTUP: - return validateMove( - board, playerx + 1, playery, playerChance, RIGHT - ) and validateMove(board, tileX, tileY, onumber, UP) + return validate_move(board, player_x + 1, player_y, player_chance, RIGHT) and \ + validate_move(board, tile_x, tile_y, o_number, UP) + if direction == JLEFTDOWN: - return validateMove( - board, playerx + 1, playery, playerChance, RIGHT - ) and validateMove(board, tileX, tileY, onumber, DOWN) + return validate_move(board, player_x + 1, player_y, player_chance, RIGHT) and \ + validate_move(board, tile_x, tile_y, o_number, DOWN) return True -def fenceRemainingCount(playerChance): +def fence_remaining_count(player_chance): + """ + returns the number of fence remaining for the respective player """ - returns the number of fence remainning for the respective player - """ - if playerChance == 1: - return FENCELIMIT - len(player1Fence) + if player_chance == 1: + return FENCELIMIT - len(player_1_fence) else: - return FENCELIMIT - len(player2Fence) + return FENCELIMIT - len(player_2_fence) -def validateFence(board, playerChance, startx, starty): +def validate_fence(board, player_chance, start_x, start_y): """ - checks whether the fence is valid or not - """ - - if playerChance == 1: - finalx = BOARDWIDTH - int(BOARDWIDTH / 2) - 1 - finaly = 0 + checks whether the fence is valid or not + """ + if player_chance == 1: + final_x = BOARDWIDTH - int(BOARDWIDTH / 2) - 1 + final_y = 0 else: - finalx = BOARDWIDTH - int(BOARDWIDTH / 2) - 1 - finaly = BOARDHEIGHT - 1 + final_x = BOARDWIDTH - int(BOARDWIDTH / 2) - 1 + final_y = BOARDHEIGHT - 1 - duplicateBoard = getStartingBoard(0) - duplicateBoard[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][0] = 0 - duplicateBoard[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][ + duplicate_board = get_starting_board(0) + duplicate_board[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][0] = 0 + duplicate_board[(BOARDWIDTH - int(BOARDWIDTH / 2) - 1)][ BOARDHEIGHT - 1 - ] = 0 # getting board with all zeroes + ] = 0 # getting board with all zeroes - start = (startx, starty) - final = (finalx, finaly) + start = (start_x, start_y) + final = (final_x, final_y) s = [start] while s: current = s.pop() if current == final: return True - while s: - s.pop() # to end the loop - elif duplicateBoard[current[0]][current[1]] == 0: - duplicateBoard[current[0]][current[1]] = 1 - if ( - current[0] - 1 >= 0 - and validateMove( - board, - current[0] - 1, - current[1], - playerChance, - RIGHT, - current[0], - current[1], - ) - and duplicateBoard[current[0] - 1][current[1]] == 0 - ): + elif duplicate_board[current[0]][current[1]] == 0: + duplicate_board[current[0]][current[1]] = 1 + if current[0] - 1 >= 0 and\ + validate_move(board, current[0] - 1, current[1], player_chance, RIGHT, current[0], current[1]) and\ + duplicate_board[current[0] - 1][current[1]] == 0: s = s + [(current[0] - 1, current[1])] - if ( - current[0] + 1 < BOARDWIDTH - and validateMove( - board, - current[0] + 1, - current[1], - playerChance, - LEFT, - current[0], - current[1], - ) - and duplicateBoard[current[0] + 1][current[1]] == 0 - ): + if current[0] + 1 < BOARDWIDTH and\ + validate_move(board, current[0] + 1, current[1], player_chance, LEFT, current[0], current[1]) and\ + duplicate_board[current[0] + 1][current[1]] == 0: s = s + [(current[0] + 1, current[1])] - if ( - current[1] - 1 >= 0 - and validateMove( - board, - current[0], - current[1] - 1, - playerChance, - DOWN, - current[0], - current[1], - ) - and duplicateBoard[current[0]][current[1] - 1] == 0 - ): + if current[1] - 1 >= 0 and\ + validate_move(board, current[0], current[1] - 1, player_chance, DOWN, current[0], current[1]) and\ + duplicate_board[current[0]][current[1] - 1] == 0: s = s + [(current[0], current[1] - 1)] - if ( - current[1] + 1 < BOARDHEIGHT - and validateMove( - board, - current[0], - current[1] + 1, - playerChance, - UP, - current[0], - current[1], - ) - and duplicateBoard[current[0]][current[1] + 1] == 0 - ): + if current[1] + 1 < BOARDHEIGHT and\ + validate_move(board, current[0], current[1] + 1, player_chance, UP, current[0], current[1]) and\ + duplicate_board[current[0]][current[1] + 1] == 0: s = s + [(current[0], current[1] + 1)] return False -def fenceLine(xi, yi, xf, yf): +def fence_line(x_i, y_i, x_f, y_f): # function to check if fence has been put on the valid space or not - for fence in TotalFence: - if (fence[0][0] + fence[1][0]) == (xi + xf) and (fence[0][1] + fence[1][1]) == ( - yi + yf - ): # is not the same fence + for fence in total_fence: + # is not the same fence + if (fence[0][0] + fence[1][0]) == (x_i + x_f) and (fence[0][1] + fence[1][1]) == (y_i + y_f): return False - if ( - (fence[0][0] == fence[1][0]) - and xi == xf - and xi == fence[0][0] - and ( - (yi == fence[0][1] - 1 and yf == fence[1][1] - 1) - or (yi == fence[0][1] + 1 and yf == fence[1][1] + 1) - ) - ): - # print (xi,xf,yi,yf," ",fence[0][0],fence[0][1],fence[1][0],fence[1][1]) + if fence[0][0] == fence[1][0] and x_i == x_f and x_i == fence[0][0] and\ + ((y_i == fence[0][1] - 1 and y_f == fence[1][1] - 1) or + (y_i == fence[0][1] + 1 and y_f == fence[1][1] + 1)): return False - if ( - (fence[0][1] == fence[1][1]) - and yi == yf - and yi == fence[0][1] - and ( - (xi == fence[0][0] - 1 and xf == fence[1][0] - 1) - or (xi == fence[0][0] + 1 and xf == fence[1][0] + 1) - ) - ): + if fence[0][1] == fence[1][1] and y_i == y_f and y_i == fence[0][1] and\ + ((x_i == fence[0][0] - 1 and x_f == fence[1][0] - 1) or + (x_i == fence[0][0] + 1 and x_f == fence[1][0] + 1)): return False return True -def showStartScreen(): +def show_start_screen(): BASICFONT = pygame.font.SysFont("comicsansms", 3 * BASICFONTSIZE) OPTIONFONT = pygame.font.SysFont("comicsansms", 2 * BASICFONTSIZE) - titleSurf1 = BASICFONT.render("Race The Car", True, WHITE) - titleSurf2 = OPTIONFONT.render("RaceON", True, WHITE) - # titleSurf2 = BASICFONT.render('Let the race begin..', True, GREEN) - # degrees1 = 0 - # degrees2 = 0 + title_surf_1 = BASICFONT.render("Race The Car", True, WHITE) + title_surf_2 = OPTIONFONT.render("RaceON", True, WHITE) + while True: DISPLAYSURF.fill(BGCOLOR) - # rotatedSurf1 = pygame.transform.rotate(titleSurf1, degrees1) - # rotatedRect1 = rotatedSurf1.get_rect(6 - # rotatedRect1.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 4) DISPLAYSURF.blit(hmImg1, DISPLAYSURF.get_rect()) - DISPLAYSURF.blit(titleSurf1, (WINDOWWIDTH / 3.4, WINDOWHEIGHT / 20)) - DISPLAYSURF.blit(titleSurf2, (WINDOWWIDTH / 1.4, WINDOWHEIGHT / 2)) - # rotatedSurf2 = pygame.transform.rotate(titleSurf2, degrees2) - # rotatedRect2 = rotatedSurf2.get_rect() - # rotatedRect2.center = (WINDOWWIDTH / 2, WINDOWHEIGHT / 3) - # DISPLAYSURF.blit(rotatedSurf2, rotatedRect2) - drawPressKeyMsg() - if checkForKeyPress(): + DISPLAYSURF.blit(title_surf_1, (WINDOWWIDTH / 3.4, WINDOWHEIGHT / 20)) + DISPLAYSURF.blit(title_surf_2, (WINDOWWIDTH / 1.4, WINDOWHEIGHT / 2)) + draw_press_key_msg() + if check_for_key_press(): pygame.event.get() # clear event queue return pygame.display.update() FPSCLOCK.tick(FPS) - # degrees1 += 3 # rotate by 3 degrees each frame - # degrees2 += 7 # rotate by 7 degrees each frame -def drawPressKeyMsg(): - pressKeySurf = BASICFONT.render("Press a key to play.", True, DARKGRAY) - pressKeyRect = pressKeySurf.get_rect() - pressKeyRect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30) - DISPLAYSURF.blit(pressKeySurf, pressKeyRect) +def draw_press_key_msg(): + press_key_surf = BASICFONT.render("Press a key to play.", True, DARKGRAY) + press_key_rect = press_key_surf.get_rect() + press_key_rect.topleft = (WINDOWWIDTH - 200, WINDOWHEIGHT - 30) + DISPLAYSURF.blit(press_key_surf, press_key_rect) -def checkForKeyPress(): +def check_for_key_press(): if len(pygame.event.get(QUIT)) > 0: terminate() - keyUpEvents = pygame.event.get(KEYUP) - if len(keyUpEvents) == 0: + key_up_events = pygame.event.get(KEYUP) + if len(key_up_events) == 0: return None - if keyUpEvents[0].key == K_ESCAPE: + if key_up_events[0].key == K_ESCAPE: terminate() - return keyUpEvents[0].key + return key_up_events[0].key if __name__ == "__main__": diff --git a/constants.py b/constants.py index 965c446..a394e61 100644 --- a/constants.py +++ b/constants.py @@ -1,3 +1,5 @@ +import pygame + BOARDHEIGHT = 7 BOARDWIDTH = 7 TILESIZE = 92 @@ -64,10 +66,10 @@ JRIGHTUP = "jumprightup" JRIGHTDOWN = "jumprightdown" -carImg = pygame.image.load("car_11.png") -carImg1 = pygame.image.load("car_22.png") +car_img = pygame.image.load("car_11.png") +car_img_1 = pygame.image.load("car_22.png") flagImg = pygame.image.load("blue2.jpg") flagImg1 = pygame.image.load("red2.jpg") hmImg1 = pygame.image.load("hm2.jpg") -FENCELIMIT = 8 # The maximum numbr of fences a player can use \ No newline at end of file +FENCELIMIT = 8 # The maximum number of fences a player can use diff --git a/setup.py b/setup.py index 21976e4..36aa2a0 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ import os.path, cx_Freeze -### setting environment variables dynamically to void key error on local machines +# setting environment variables dynamically to void key error on local machines PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__)) os.environ["TCL_LIBRARY"] = os.path.join(PYTHON_INSTALL_DIR, "tcl", "tcl8.6") os.environ["TK_LIBRARY"] = os.path.join(PYTHON_INSTALL_DIR, "tcl", "tk8.6") diff --git a/test_Race_the_car.py b/test_Race_the_car.py index f07a499..4673ce6 100644 --- a/test_Race_the_car.py +++ b/test_Race_the_car.py @@ -1,4 +1,3 @@ -import pytest import Race_the_car BOARDWIDTH = Race_the_car.BOARDWIDTH @@ -12,14 +11,15 @@ ONE_STARTING_ROW = Race_the_car.ONE_STARTING_ROW TWO_STARTING_ROW = Race_the_car.TWO_STARTING_ROW -def test_getStartingBoard(): - board = Race_the_car.getStartingBoard(OPEN_SPACE) - # player 1 position correct +def test_get_starting_board(): + board = Race_the_car.get_starting_board(OPEN_SPACE) + + # Player 1 position correct assert board[BOARDWIDTH_CENTER][ONE_STARTING_ROW] == PLAYER_ONE board[BOARDWIDTH_CENTER][ONE_STARTING_ROW] = OPEN_SPACE - # player 2 position correct + # Player 2 position correct assert board[BOARDWIDTH_CENTER][TWO_STARTING_ROW] == PLAYER_TWO board[BOARDWIDTH_CENTER][TWO_STARTING_ROW] = OPEN_SPACE @@ -28,8 +28,8 @@ def test_getStartingBoard(): for y in range(BOARDHEIGHT): assert board[x][y] == OPEN_SPACE - # proper behavior if input is changed - board = Race_the_car.getStartingBoard(0) + # Proper behavior if input is changed + board = Race_the_car.get_starting_board(0) assert board[BOARDWIDTH_CENTER][ONE_STARTING_ROW] == PLAYER_ONE board[BOARDWIDTH_CENTER][ONE_STARTING_ROW] = 0 assert board[BOARDWIDTH_CENTER][TWO_STARTING_ROW] == PLAYER_TWO @@ -39,4 +39,3 @@ def test_getStartingBoard(): assert board[x][y] == 0 return -