├── Numberdiscussinggame.py ├── Pokeman game.py ├── Snake.py ├── cow and bulls.py ├── emoji.py ├── ponggame.py └── stonepaperscissor.py /Numberdiscussinggame.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | print("Hi welcome to the game, This is a number guessing game.\nYou got 7 chances to guess the number. Let's start the game") 4 | 5 | number_to_guess = random.randrange(100) 6 | 7 | chances = 7 8 | 9 | guess_counter = 0 10 | 11 | while guess_counter < chances: 12 | 13 | guess_counter += 1 14 | my_guess = int(input('Please Enter your Guess : ')) 15 | 16 | if my_guess == number_to_guess: 17 | print(f'The number is {number_to_guess} and you found it right !! in the {guess_counter} attempt') 18 | break 19 | 20 | elif guess_counter >= chances and my_guess != number_to_guess: 21 | print(f'Oops sorry, The number is {number_to_guess} better luck next time') 22 | 23 | elif my_guess > number_to_guess: 24 | print('Your guess is higher ') 25 | 26 | elif my_guess < number_to_guess: 27 | print('Your guess is lesser') 28 | -------------------------------------------------------------------------------- /Pokeman game.py: -------------------------------------------------------------------------------- 1 | 2 | powers = [3, 8, 9, 7] 3 | 4 | mini, maxi = 0, 0 5 | 6 | for power in powers: 7 | if mini == 0 and maxi == 0: 8 | mini, maxi = powers[0], powers[0] 9 | print(mini, maxi) 10 | else: 11 | mini = min(mini, power) 12 | maxi = max(maxi, power) 13 | print(mini, maxi) 14 | 15 | -------------------------------------------------------------------------------- /Snake.py: -------------------------------------------------------------------------------- 1 | import turtle 2 | import random 3 | 4 | w = 500 5 | h = 500 6 | food_size = 10 7 | delay = 100 8 | 9 | offsets = { 10 | "up": (0, 20), 11 | "down": (0, -20), 12 | "left": (-20, 0), 13 | "right": (20, 0) 14 | } 15 | 16 | def reset(): 17 | global snake, snake_dir, food_position, pen 18 | snake = [[0, 0], [0, 20], [0, 40], [0, 60], [0, 80]] 19 | snake_dir = "up" 20 | food_position = get_random_food_position() 21 | food.goto(food_position) 22 | move_snake() 23 | 24 | def move_snake(): 25 | global snake_dir 26 | 27 | new_head = snake[-1].copy() 28 | new_head[0] = snake[-1][0] + offsets[snake_dir][0] 29 | new_head[1] = snake[-1][1] + offsets[snake_dir][1] 30 | 31 | 32 | if new_head in snake[:-1]: 33 | reset() 34 | else: 35 | snake.append(new_head) 36 | 37 | 38 | if not food_collision(): 39 | snake.pop(0) 40 | 41 | 42 | if snake[-1][0] > w / 2: 43 | snake[-1][0] -= w 44 | elif snake[-1][0] < - w / 2: 45 | snake[-1][0] += w 46 | elif snake[-1][1] > h / 2: 47 | snake[-1][1] -= h 48 | elif snake[-1][1] < -h / 2: 49 | snake[-1][1] += h 50 | 51 | 52 | pen.clearstamps() 53 | 54 | 55 | for segment in snake: 56 | pen.goto(segment[0], segment[1]) 57 | pen.stamp() 58 | 59 | 60 | screen.update() 61 | 62 | turtle.ontimer(move_snake, delay) 63 | 64 | def food_collision(): 65 | global food_position 66 | if get_distance(snake[-1], food_position) < 20: 67 | food_position = get_random_food_position() 68 | food.goto(food_position) 69 | return True 70 | return False 71 | 72 | def get_random_food_position(): 73 | x = random.randint(- w / 2 + food_size, w / 2 - food_size) 74 | y = random.randint(- h / 2 + food_size, h / 2 - food_size) 75 | return (x, y) 76 | 77 | def get_distance(pos1, pos2): 78 | x1, y1 = pos1 79 | x2, y2 = pos2 80 | distance = ((y2 - y1) ** 2 + (x2 - x1) ** 2) ** 0.5 81 | return distance 82 | def go_up(): 83 | global snake_dir 84 | if snake_dir != "down": 85 | snake_dir = "up" 86 | 87 | def go_right(): 88 | global snake_dir 89 | if snake_dir != "left": 90 | snake_dir = "right" 91 | 92 | def go_down(): 93 | global snake_dir 94 | if snake_dir!= "up": 95 | snake_dir = "down" 96 | 97 | def go_left(): 98 | global snake_dir 99 | if snake_dir != "right": 100 | snake_dir = "left" 101 | 102 | 103 | screen = turtle.Screen() 104 | screen.setup(w, h) 105 | screen.title("Snake") 106 | screen.bgcolor("blue") 107 | screen.setup(500, 500) 108 | screen.tracer(0) 109 | 110 | 111 | pen = turtle.Turtle("square") 112 | pen.penup() 113 | 114 | 115 | food = turtle.Turtle() 116 | food.shape("square") 117 | food.color("yellow") 118 | food.shapesize(food_size / 20) 119 | food.penup() 120 | 121 | 122 | screen.listen() 123 | screen.onkey(go_up, "Up") 124 | screen.onkey(go_right, "Right") 125 | screen.onkey(go_down, "Down") 126 | screen.onkey(go_left, "Left") 127 | 128 | 129 | reset() 130 | turtle.done() 131 | Output: 132 | -------------------------------------------------------------------------------- /cow and bulls.py: -------------------------------------------------------------------------------- 1 | 2 | import random 3 | 4 | def getDigits(num): 5 | return [int(i) for i in str(num)] 6 | 7 | 8 | 9 | def noDuplicates(num): 10 | num_li = getDigits(num) 11 | if len(num_li) == len(set(num_li)): 12 | return True 13 | else: 14 | return False 15 | 16 | 17 | 18 | def generateNum(): 19 | while True: 20 | num = random.randint(1000,9999) 21 | if noDuplicates(num): 22 | return num 23 | 24 | 25 | 26 | def numOfBullsCows(num,guess): 27 | bull_cow = [0,0] 28 | num_li = getDigits(num) 29 | guess_li = getDigits(guess) 30 | 31 | for i,j in zip(num_li,guess_li): 32 | 33 | 34 | if j in num_li: 35 | 36 | 37 | if j == i: 38 | bull_cow[0] += 1 39 | 40 | 41 | else: 42 | bull_cow[1] += 1 43 | 44 | return bull_cow 45 | 46 | 47 | 48 | num = generateNum() 49 | tries =int(input('Enter number of tries: ')) 50 | 51 | 52 | while tries > 0: 53 | guess = int(input("Enter your guess: ")) 54 | 55 | if not noDuplicates(guess): 56 | print("Number should not have repeated digits. Try again.") 57 | continue 58 | if guess < 1000 or guess > 9999: 59 | print("Enter 4 digit number only. Try again.") 60 | continue 61 | 62 | bull_cow = numOfBullsCows(num,guess) 63 | print(f"{bull_cow[0]} bulls, {bull_cow[1]} cows") 64 | tries -=1 65 | 66 | if bull_cow[0] == 4: 67 | print("You guessed right!") 68 | break 69 | else: 70 | print(f"You ran out of tries. Number was {num}") 71 | -------------------------------------------------------------------------------- /emoji.py: -------------------------------------------------------------------------------- 1 | import demoji 2 | 3 | demoji.download_codes() 4 | -------------------------------------------------------------------------------- /ponggame.py: -------------------------------------------------------------------------------- 1 | import turtle as t 2 | playerAscore=0 3 | playerBscore=0 4 | 5 | window=t.Screen() 6 | window.title("The Pong Game") 7 | window.bgcolor("green") 8 | window.setup(width=800,height=600) 9 | window.tracer(0) 10 | 11 | leftpaddle=t.Turtle() 12 | leftpaddle.speed(0) 13 | leftpaddle.shape("square") 14 | leftpaddle.color("white") 15 | leftpaddle.shapesize(stretch_wid=5,stretch_len=1) 16 | leftpaddle.penup() 17 | leftpaddle.goto(-350,0) 18 | 19 | rightpaddle=t.Turtle() 20 | rightpaddle.speed(0) 21 | rightpaddle.shape("square") 22 | rightpaddle.color("white") 23 | rightpaddle.shapesize(stretch_wid=5,stretch_len=1) 24 | rightpaddle.penup() 25 | rightpaddle.goto(-350,0) 26 | 27 | ball=t.Turtle() 28 | ball.speed(0) 29 | ball.shape("circle") 30 | ball.color("red") 31 | ball.penup() 32 | ball.goto(5,5) 33 | ballxdirection=0.2 34 | ballydirection=0.2 35 | 36 | pen=t.Turtle() 37 | pen.speed(0) 38 | pen.color("Blue") 39 | pen.penup() 40 | pen.hideturtle() 41 | pen.goto(0,260) 42 | pen.write("score",align="center",font=('Arial',24,'normal')) 43 | 44 | def leftpaddleup(): 45 | y=leftpaddle.ycor() 46 | y=y+90 47 | leftpaddle.sety(y) 48 | 49 | def leftpaddledown(): 50 | y=leftpaddle.ycor() 51 | y=y+90 52 | leftpaddle.sety(y) 53 | 54 | def rightpaddleup(): 55 | y=rightpaddle.ycor() 56 | y=y+90 57 | rightpaddle.sety(y) 58 | 59 | def rightpaddledown(): 60 | y=rightpaddle.ycor() 61 | y=y+90 62 | rightpaddle.sety(y) 63 | 64 | window.listen() 65 | window.onkeypress(leftpaddleup,'w') 66 | window.onkeypress(leftpaddledown,'s') 67 | window.onkeypress(rightpaddleup,'Up') 68 | window.onkeypress(rightpaddledown,'Down') 69 | 70 | while True: 71 | window.update() 72 | 73 | ball.setx(ball.xcor()+ballxdirection) 74 | ball.sety(ball.ycor()+ballxdirection) 75 | 76 | if ball.ycor()>290: 77 | ball.sety(290) 78 | ballydirection=ballydirection*-1 79 | if ball.ycor()<-290: 80 | ball.sety(-290) 81 | ballydirection=ballydirection*-1 82 | 83 | if ball.xcor() > 390: 84 | ball.goto(0,0) 85 | ball_dx = ball_dx * -1 86 | player_a_score = player_a_score + 1 87 | pen.clear() 88 | pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal")) 89 | os.system("afplay wallhit.wav&") 90 | 91 | 92 | 93 | if(ball.xcor()) < -390: 94 | ball.goto(0,0) 95 | ball_dx = ball_dx * -1 96 | player_b_score = player_b_score + 1 97 | pen.clear() 98 | pen.write("Player A: {} Player B: {} ".format(player_a_score,player_b_score),align="center",font=('Monaco',24,"normal")) 99 | os.system("afplay wallhit.wav&") 100 | 101 | 102 | 103 | if(ball.xcor() > 340) and (ball.xcor() < 350) and (ball.ycor() < rightpaddle.ycor() + 40 and ball.ycor() > rightpaddle.ycor() - 40): 104 | ball.setx(340) 105 | ball_dx = ball_dx * -1 106 | os.system("afplay paddle.wav&") 107 | 108 | if(ball.xcor() < -340) and (ball.xcor() > -350) and (ball.ycor() < leftpaddle.ycor() + 40 and ball.ycor() > leftpaddle.ycor() - 40): 109 | ball.setx(-340) 110 | ball_dx = ball_dx * -1 111 | os.system("afplay paddle.wav&") 112 | -------------------------------------------------------------------------------- /stonepaperscissor.py: -------------------------------------------------------------------------------- 1 | import random 2 | 3 | 4 | print('Winning rules of the game ROCK PAPER SCISSORS are:\n' 5 | + "Rock vs Paper -> Paper wins \n" 6 | + "Rock vs Scissors -> Rock wins \n" 7 | + "Paper vs Scissors -> Scissors wins \n") 8 | 9 | while True: 10 | 11 | print("Enter your choice \n 1 - Rock \n 2 - Paper \n 3 - Scissors \n") 12 | 13 | 14 | choice = int(input("Enter your choice: ")) 15 | 16 | 17 | while choice > 3 or choice < 1: 18 | choice = int(input('Enter a valid choice please : ')) 19 | 20 | if choice == 1: 21 | choice_name = 'Rock' 22 | elif choice == 2: 23 | choice_name = 'Paper' 24 | else: 25 | choice_name = 'Scissors' 26 | 27 | print('User choice is:', choice_name) 28 | print("Now it's Computer's Turn...") 29 | 30 | comp_choice = random.randint(1, 3) 31 | 32 | if comp_choice == 1: 33 | comp_choice_name = 'Rock' 34 | elif comp_choice == 2: 35 | comp_choice_name = 'Paper' 36 | else: 37 | comp_choice_name = 'Scissors' 38 | 39 | print("Computer choice is:", comp_choice_name) 40 | print(choice_name, 'vs', comp_choice_name) 41 | 42 | if choice == comp_choice: 43 | result = "DRAW" 44 | elif (choice == 1 and comp_choice == 2) or (comp_choice == 1 and choice == 2): 45 | result = 'Paper' 46 | elif (choice == 1 and comp_choice == 3) or (comp_choice == 1 and choice == 3): 47 | result = 'Rock' 48 | elif (choice == 2 and comp_choice == 3) or (comp_choice == 2 and choice == 3): 49 | result = 'Scissors' 50 | 51 | if result == "DRAW": 52 | print("<== It's a tie! ==>") 53 | elif result == choice_name: 54 | print("<== User wins! ==>") 55 | else: 56 | print("<== Computer wins! ==>") 57 | 58 | print("Do you want to play again? (Y/N)") 59 | ans = input().lower() 60 | if ans == 'n': 61 | break 62 | 63 | 64 | print("Thanks for playing!") 65 | --------------------------------------------------------------------------------