From 5049cc31fc6f9f813c85bb8343fc183065fae4fa Mon Sep 17 00:00:00 2001 From: Riya Loya Date: Thu, 13 Oct 2022 01:17:58 +0530 Subject: [PATCH] Few Game Added --- Snake.py | 161 +++++++++++++++++++++++++++++++++++++++++++++++++ Tic-Tac-Toe.py | 143 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 304 insertions(+) create mode 100644 Snake.py create mode 100644 Tic-Tac-Toe.py diff --git a/Snake.py b/Snake.py new file mode 100644 index 0000000..5161adf --- /dev/null +++ b/Snake.py @@ -0,0 +1,161 @@ +# import required modules +import turtle +import time +import random + +delay = 0.1 +score = 0 +high_score = 0 + + + +# Creating a window screen +wn = turtle.Screen() +wn.title("Snake Game") +wn.bgcolor("blue") +# the width and height can be put as user's choice +wn.setup(width=600, height=600) +wn.tracer(0) + +# head of the snake +head = turtle.Turtle() +head.shape("square") +head.color("white") +head.penup() +head.goto(0, 0) +head.direction = "Stop" + +# food in the game +food = turtle.Turtle() +colors = random.choice(['red', 'green', 'black']) +shapes = random.choice(['square', 'triangle', 'circle']) +food.speed(0) +food.shape(shapes) +food.color(colors) +food.penup() +food.goto(0, 100) + +pen = turtle.Turtle() +pen.speed(0) +pen.shape("square") +pen.color("white") +pen.penup() +pen.hideturtle() +pen.goto(0, 250) +pen.write("Score : 0 High Score : 0", align="center", + font=("candara", 24, "bold")) + + + +# assigning key directions +def goup(): + if head.direction != "down": + head.direction = "up" + + +def godown(): + if head.direction != "up": + head.direction = "down" + + +def goleft(): + if head.direction != "right": + head.direction = "left" + + +def goright(): + if head.direction != "left": + head.direction = "right" + + +def move(): + if head.direction == "up": + y = head.ycor() + head.sety(y+20) + if head.direction == "down": + y = head.ycor() + head.sety(y-20) + if head.direction == "left": + x = head.xcor() + head.setx(x-20) + if head.direction == "right": + x = head.xcor() + head.setx(x+20) + + + +wn.listen() +wn.onkeypress(goup, "w") +wn.onkeypress(godown, "s") +wn.onkeypress(goleft, "a") +wn.onkeypress(goright, "d") + +segments = [] + + + +# Main Gameplay +while True: + wn.update() + if head.xcor() > 290 or head.xcor() < -290 or head.ycor() > 290 or head.ycor() < -290: + time.sleep(1) + head.goto(0, 0) + head.direction = "Stop" + colors = random.choice(['red', 'blue', 'green']) + shapes = random.choice(['square', 'circle']) + for segment in segments: + segment.goto(1000, 1000) + segments.clear() + score = 0 + delay = 0.1 + pen.clear() + pen.write("Score : {} High Score : {} ".format( + score, high_score), align="center", font=("candara", 24, "bold")) + if head.distance(food) < 20: + x = random.randint(-270, 270) + y = random.randint(-270, 270) + food.goto(x, y) + + # Adding segment + new_segment = turtle.Turtle() + new_segment.speed(0) + new_segment.shape("square") + new_segment.color("orange") # tail colour + new_segment.penup() + segments.append(new_segment) + delay -= 0.001 + score += 10 + if score > high_score: + high_score = score + pen.clear() + pen.write("Score : {} High Score : {} ".format( + score, high_score), align="center", font=("candara", 24, "bold")) + # Checking for head collisions with body segments + for index in range(len(segments)-1, 0, -1): + x = segments[index-1].xcor() + y = segments[index-1].ycor() + segments[index].goto(x, y) + if len(segments) > 0: + x = head.xcor() + y = head.ycor() + segments[0].goto(x, y) + move() + for segment in segments: + if segment.distance(head) < 20: + time.sleep(1) + head.goto(0, 0) + head.direction = "stop" + colors = random.choice(['red', 'blue', 'green']) + shapes = random.choice(['square', 'circle']) + for segment in segments: + segment.goto(1000, 1000) + segment.clear() + + score = 0 + delay = 0.1 + pen.clear() + pen.write("Score : {} High Score : {} ".format( + score, high_score), align="center", font=("candara", 24, "bold")) + time.sleep(delay) + +wn.mainloop() diff --git a/Tic-Tac-Toe.py b/Tic-Tac-Toe.py new file mode 100644 index 0000000..399ec76 --- /dev/null +++ b/Tic-Tac-Toe.py @@ -0,0 +1,143 @@ +board = [ + ["-", "-", "-"], + ["-", "-", "-"], + ["-", "-", "-"], +] + +initBoard = [ + ["0,0", "0,1", "0,2"], + ["1,0", "1,1", "1,2"], + ["2,0", "2,1", "2,2"], +] + +winner = "-" + + +def isBoardFull(): + numDash = 9 + for i in board: + for j in i: + if j != "-": + numDash = numDash - 1 + return numDash + + +def printBoard(): + for i in board: + print(i, end="\n") + print("\n") + + +def checkWin(): + # Check horizontal win + winner = "-" + + for i in board: + winner = i[0] + for j in i: + if j != winner: + winner = "-" + break + if winner != "-": + return winner + + # Check vertical win + winner = "-" + + for i in range(3): + winner = board[0][i] + for j in range(0, 3): + if winner != board[j][i]: + winner = "-" + break + if winner != "-": + return winner + + # Check diagonal win + winner = board[0][0] + + for i in range(3): + for j in range(3): + if winner != board[i][i]: + winner = "-" + break + if winner != "-": + return winner + + winner = board[0][2] + + for i in range(3): + for j in range(3): + if i + j == 2: + if winner != board[i][j]: + winner = "-" + break + if winner != "-": + return winner + + +def startGame(): + + print(f"šŸ‘šŸ‘\tWelcome to the Tic-Tac-Toe Game\tšŸ‘šŸ‘\n\n") + + for i in initBoard: + print(i) + + print("\n") + + player1Name = str(input("Enter your name : (player 1 - x) : ")) + player2Name = str(input("Enter your name : (player 2 - o) : ")) + + print("\n") + + for i in range(0, 9): + player1Place = str(input(f"Enter your position : ({player1Name}) : ")) + player1Place = player1Place.split(",") + + if board[int(player1Place[0])][int(player1Place[1])] == "-": + board[int(player1Place[0])][int(player1Place[1])] = "x" + else: + print("Invalid Position!!!") + + printBoard() + + winner = checkWin() + if winner == "x": + print(f"Player : {player1Name} with symbol 'x' is the winner!!!") + printBoard() + break + elif winner == "o": + printBoard() + print(f"Player : {player2Name} with symbol 'o' is the winner!!!") + break + elif isBoardFull() == 0: + print("Game Tied!!!\n") + break + + player2Place = str(input(f"Enter your position : ({player2Name}) : ")) + print("\n") + player2Place = player2Place.split(",") + + if board[int(player2Place[0])][int(player2Place[1])] == "-": + board[int(player2Place[0])][int(player2Place[1])] = "o" + else: + print("Invalid Position!!!") + + printBoard() + + winner = checkWin() + if winner == "x": + print(f"Player : {player1Name} with symbol 'x' is the winner!!!\n") + printBoard() + break + elif winner == "o": + print(f"Player : {player2Name} with symbol 'o' is the winner!!!\n") + printBoard() + break + elif isBoardFull() == 0: + print("Game Tied!!!\n") + break + + +if __name__ == "__main__": + startGame()