Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some minor updates #16

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ $ cd Race-the-car
```sh
$ pip install cx_Freeze
```
* If you don't have cx_Freeze installed, get it by typing the following in terminal(for Linux)
* For Linux, execute the following in the terminal
```sh
$ sudo apt install cx-freeze
```
Expand Down
71 changes: 60 additions & 11 deletions Race_the_car.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
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
Expand All @@ -31,51 +33,65 @@ def main():
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (450, 10)
mainBoard = getStartingBoard()

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

while True:

msg = "Player turn-> " + str(playerChance)
mouseClicked = False
drawBoard(mainBoard, msg)
hasWon(mainBoard)
DISPLAYSURF.blit(textSurfaceObj, textRectObj)

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

elif event.type == MOUSEBUTTONUP:
mousex, mousey = event.pos
mouseClicked = True

spotx, spoty = getSpotClicked(mainBoard, mousex, mousey)

if (spotx, spoty) == (None, None):

# check for the button pressed
if FENCE_RECT.collidepoint((mousex, mousey)) and mouseClicked:
# Fence Button pressed
fenceClicked = True
moveClicked = False
mouseClicked = False

if MOVE_RECT.collidepoint((mousex, mousey)):
# MOVe button pressed
fenceClicked = False
moveClicked = True
mouseClicked = False

else:
playerx, playery = getPlayerPosition(mainBoard, playerChance)

if playerChance == 1:
onumber = 2

else:
onumber = 1
opp_playerx, opp_playery = getPlayerPosition(mainBoard, onumber)
Expand Down Expand Up @@ -139,15 +155,21 @@ def main():
)
drawFenceHighlight(spotx, spoty, HIGHLIGHTCOLOR, mousex, mousey)
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

else:
hasWon(mainBoard, onumber)

else:
drawBoard(
mainBoard,
Expand All @@ -157,6 +179,7 @@ def main():
)

# tile pressed

elif moveClicked:
drawBoard(mainBoard, "Player " + str(playerChance) + " to move the car")
pygame.draw.rect(DISPLAYSURF, BLACK, MOVE_RECT, 2)
Expand Down Expand Up @@ -185,11 +208,14 @@ def main():


def terminate():
""" Exit the game and terminate the application """
pygame.quit()
sys.exit()


def checkForQuit():
"""Check if the quit button has been pressed
If so, exit the game."""
for event in pygame.event.get(QUIT):
terminate()
for event in pygame.event.get(KEYUP):
Expand All @@ -204,8 +230,9 @@ def getLeftTopOfTile(tileX, tileY):
return (left, top)


def getStartingBoard(state=OPEN_SPACE):
# Returns the board data structure with tiles in it

def getStartingBoard(state=3):
"""Returns the board data structure with tiles in it"""

board = []
for x in range(BOARDWIDTH):
Expand All @@ -219,7 +246,8 @@ def getStartingBoard(state=OPEN_SPACE):


def hasWon(board, playerwon=-1):
# checks for winning player

"""checks for winning player"""
color1 = LIGHTBGCOLOR
color2 = BGCOLOR
if board[BOARDWIDTH - int(BOARDWIDTH / 2) - 1][0] == 1 or playerwon == 1:
Expand All @@ -245,8 +273,8 @@ def hasWon(board, playerwon=-1):


def drawTile(tileX, tileY, number, adjx=0, adjy=0):
# draw a tile at tileX ad tileY
# pixels over determined by adjx and adjy
""" 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)
Expand Down Expand Up @@ -293,32 +321,37 @@ def drawTile(tileX, tileY, number, adjx=0, adjy=0):


def getPlayerPosition(board, number):
# return the x,y co=ordinates of the blank box

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


def makeMove(board, move, number):
# just makes a move not checks if it's valid or not


"""just makes a move and checks if it's valid or not"""
playerx, playery = getPlayerPosition(board, number)

if move == UP:
board[playerx][playery], board[playerx][playery + 1] = (
board[playerx][playery + 1],
board[playerx][playery],
)

elif move == DOWN:
board[playerx][playery], board[playerx][playery - 1] = (
board[playerx][playery - 1],
board[playerx][playery],
)

elif move == RIGHT:
board[playerx][playery], board[playerx - 1][playery] = (
board[playerx - 1][playery],
board[playerx][playery],
)

elif move == LEFT:
board[playerx][playery], board[playerx + 1][playery] = (
board[playerx + 1][playery],
Expand All @@ -330,11 +363,13 @@ def makeMove(board, move, number):
board[playerx][playery + 2],
board[playerx][playery],
)

elif move == JUPLEFT:
board[playerx][playery], board[playerx + 1][playery + 1] = (
board[playerx + 1][playery + 1],
board[playerx][playery],
)

elif move == JUPRIGHT:
board[playerx][playery], board[playerx - 1][playery + 1] = (
board[playerx - 1][playery + 1],
Expand All @@ -346,11 +381,13 @@ def makeMove(board, move, number):
board[playerx][playery - 2],
board[playerx][playery],
)

elif move == JDOWNLEFT:
board[playerx][playery], board[playerx + 1][playery - 1] = (
board[playerx + 1][playery - 1],
board[playerx][playery],
)

elif move == JDOWNRIGHT:
board[playerx][playery], board[playerx - 1][playery - 1] = (
board[playerx - 1][playery - 1],
Expand All @@ -367,6 +404,7 @@ def makeMove(board, move, number):
board[playerx - 1][playery + 1],
board[playerx][playery],
)

elif move == JRIGHTDOWN:
board[playerx][playery], board[playerx - 1][playery - 1] = (
board[playerx - 1][playery - 1],
Expand All @@ -378,11 +416,13 @@ def makeMove(board, move, number):
board[playerx + 2][playery],
board[playerx][playery],
)

elif move == JLEFTUP:
board[playerx][playery], board[playerx + 1][playery + 1] = (
board[playerx + 1][playery + 1],
board[playerx][playery],
)

elif move == JLEFTDOWN:
board[playerx][playery], board[playerx + 1][playery - 1] = (
board[playerx + 1][playery - 1],
Expand Down Expand Up @@ -500,7 +540,8 @@ def drawFenceHighlight(tileX, tileY, highLightColor, mousex, mousey, fence_thick


def makeText(text, color, bgcolor, top, left):
# create the surface and rect object for some text
"""create the surface and rect object for some text"""

textSurf = BASICFONT.render(text, True, color, bgcolor)
textRect = textSurf.get_rect()
textRect.topleft = (top, left)
Expand Down Expand Up @@ -704,7 +745,7 @@ def fencePutting(tileX, tileY, playerChance, mousex, mousey):


def drawHighlightTile(tileX, tileY, highLightColor, tile_thickness=4):
# highlight the box where car may move based on mouse hover
"""highlight the box where car may move based on mouse hover"""
left, top = getLeftTopOfTile(tileX, tileY)
pygame.draw.rect(
DISPLAYSURF,
Expand Down Expand Up @@ -794,22 +835,27 @@ def validateMove(board, tileX, tileY, playerChance, direction, playerx=-1, playe
return validateMove(
board, playerx, playery - 1, playerChance, DOWN
) and validateMove(board, tileX, tileY, onumber, RIGHT)

if direction == JRIGHTUP:
return validateMove(
board, playerx - 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, UP)

if direction == JRIGHTDOWN:
return validateMove(
board, playerx - 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, DOWN)

if direction == JLEFTUP:
return validateMove(
board, playerx + 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, UP)

if direction == JLEFTDOWN:
return validateMove(
board, playerx + 1, playery, playerChance, RIGHT
) and validateMove(board, tileX, tileY, onumber, DOWN)

return True


Expand Down Expand Up @@ -913,7 +959,8 @@ def validateFence(board, playerChance, startx, starty):


def fenceLine(xi, yi, xf, yf):
# function to check if fence has been put on the valid space or not
"""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
Expand Down Expand Up @@ -981,6 +1028,8 @@ def drawPressKeyMsg():


def checkForKeyPress():
"""Listens to the keys pressed by the player and returns that key event"""

if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
Expand Down