0% found this document useful (0 votes)
16 views

Python

The document describes a Python program that implements a game of tic-tac-toe between two players. It includes functions to: 1) Create an empty 3x3 board, check for empty spaces, randomly place marks for players, evaluate wins by rows, columns and diagonals, and determine if there is an overall winner or tie. 2) The main function runs a game loop that randomly assigns marks until there is a winner or tie, printing the board after each turn. 3) When called, the program runs a sample game and prints the winning player.

Uploaded by

taspmleal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Python

The document describes a Python program that implements a game of tic-tac-toe between two players. It includes functions to: 1) Create an empty 3x3 board, check for empty spaces, randomly place marks for players, evaluate wins by rows, columns and diagonals, and determine if there is an overall winner or tie. 2) The main function runs a game loop that randomly assigns marks until there is a winner or tie, printing the board after each turn. 3) When called, the program runs a sample game and prints the winning player.

Uploaded by

taspmleal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PING-PONG

# Import required libraryimport turtle # Create screensc = turtle.Screen()sc.title(“Pong


game”)sc.bgcolor(“white”)sc.setup(width=1000, height=600) # Left paddleleft_pad =
turtle.Turtle()left_pad.speed(0)left_pad.shape(“square”)left_pad.color(“black”)left_pad.shapes
ize(stretch_wid=6, stretch_len=2)left_pad.penup()left_pad.goto(-400, 0) # Right
paddleright_pad =
turtle.Turtle()right_pad.speed(0)right_pad.shape(“square”)right_pad.color(“black”)right_pad.s
hapesize(stretch_wid=6, stretch_len=2)right_pad.penup()right_pad.goto(400, 0) # Ball of circle
shapehit_ball =
turtle.Turtle()hit_ball.speed(40)hit_ball.shape(“circle”)hit_ball.color(“blue”)hit_ball.penup()hit
_ball.goto(0, 0)hit_ball.dx = 5hit_ball.dy = -5 # Initialize the scoreleft_player = 0right_player = 0
# Displays the scoresketch =
turtle.Turtle()sketch.speed(0)sketch.color(“blue”)sketch.penup()sketch.hideturtle()sketch.goto(
0, 260)sketch.write(“Left_player : 0 Right_player: 0″,align=”center”, font=(“Courier”, 24,
“normal”)) # Functions to move paddle verticallydef paddleaup():y = left_pad.ycor()y +=
20left_pad.sety(y) def paddleadown():y = left_pad.ycor()y -= 20left_pad.sety(y) def
paddlebup():y = right_pad.ycor()y += 20right_pad.sety(y) def paddlebdown():y =
right_pad.ycor()y -= 20right_pad.sety(y) # Keyboard
bindingssc.listen()sc.onkeypress(paddleaup, “e”)sc.onkeypress(paddleadown,
“x”)sc.onkeypress(paddlebup, “Up”)sc.onkeypress(paddlebdown, “Down”) while
True:sc.update()
hit_ball.setx(hit_ball.xcor()+hit_ball.dx)hit_ball.sety(hit_ball.ycor()+hit_ball.dy) # Checking
bordersif hit_ball.ycor() > 280:hit_ball.sety(280)hit_ball.dy *= -1 if hit_ball.ycor() < -
280:hit_ball.sety(-280)hit_ball.dy *= -1 if hit_ball.xcor() > 500:hit_ball.goto(0, 0)hit_ball.dy *= -
1left_player += 1sketch.clear()sketch.write(“Left_player : {} Right_player: {}”.format(left_player,
right_player), align=”center”,font=(“Courier”, 24, “normal”)) if hit_ball.xcor() < -
500:hit_ball.goto(0, 0)hit_ball.dy *= -1right_player += 1sketch.clear()sketch.write(“Left_player :
{} Right_player: {}”.format(left_player, right_player), align=”center”,font=(“Courier”, 24,
“normal”)) # Paddle ball collisionif (hit_ball.xcor() > 360 andhit_ball.xcor() < 370)
and(hit_ball.ycor() < right_pad.ycor()+40 andhit_ball.ycor() > right_pad.ycor()-
40):hit_ball.setx(360)hit_ball.dx*=-1if (hit_ball.xcor()<-360 andhit_ball.xcor()>-370)
and(hit_ball.ycor()<left_pad.ycor()+40 andhit_ball.ycor()>left_pad.ycor()-40):hit_ball.setx(-
360)hit_ball.dx*=-1
COBRA

import pygameimport timeimport random pygame.init() white = (255, 255, 255)yellow = (255,
255, 102)black = (0, 0, 0)red = (213, 50, 80)green = (0, 255, 0)blue = (50, 153, 213) dis_width =
600dis_height = 400 dis = pygame.display.set_mode((dis_width,
dis_height))pygame.display.set_caption(‘Snake Game’) clock = pygame.time.Clock()
snake_block = 10snake_speed = 15 font_style = pygame.font.SysFont(“bahnschrift”,
25)score_font = pygame.font.SysFont(“comicsansms”, 35) def Your_score(score): value =
score_font.render(“Your Score: ” + str(score), True, yellow) dis.blit(value, [0, 0]) def
our_snake(snake_block, snake_list): for x in snake_list: pygame.draw.rect(dis, black, [x[0], x[1],
snake_block, snake_block]) def message(msg, color): mesg = font_style.render(msg, True,
color) dis.blit(mesg, [dis_width / 6, dis_height / 3]) def gameLoop(): game_over = False
game_close = False x1 = dis_width / 2 y1 = dis_height / 2 x1_change = 0 y1_change = 0
snake_List = [] Length_of_snake = 1 foodx = round(random.randrange(0, dis_width –
snake_block) / 10.0) * 10.0 foody = round(random.randrange(0, dis_height – snake_block) /
10.0) * 10.0 while not game_over: while game_close == True: dis.fill(blue) message(“You Lost!
Press C-Play Again or Q-Quit”, red) Your_score(Length_of_snake – 1) pygame.display.update()
for event in pygame.event.get(): if event.type == pygame.KEYDOWN: if event.key ==
pygame.K_q: game_over = True game_close = False if event.key == pygame.K_c: gameLoop()
for event in pygame.event.get(): if event.type == pygame.QUIT: game_over = True if event.type
== pygame.KEYDOWN: if event.key == pygame.K_LEFT: x1_change = -snake_block y1_change =
0 elif event.key == pygame.K_RIGHT: x1_change = snake_block y1_change = 0 elif event.key ==
pygame.K_UP: y1_change = -snake_block x1_change = 0 elif event.key == pygame.K_DOWN:
y1_change = snake_block x1_change = 0 if x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 <
0: game_close = True x1 += x1_change y1 += y1_change dis.fill(blue) pygame.draw.rect(dis,
green, [foodx, foody, snake_block, snake_block]) snake_Head = [] snake_Head.append(x1)
snake_Head.append(y1) snake_List.append(snake_Head) if len(snake_List) > Length_of_snake:
del snake_List[0] for x in snake_List[:-1]: if x == snake_Head: game_close = True
our_snake(snake_block, snake_List) Your_score(Length_of_snake – 1) pygame.display.update()
if x1 == foodx and y1 == foody: foodx = round(random.randrange(0, dis_width – snake_block) /
10.0) * 10.0 foody = round(random.randrange(0, dis_height – snake_block) / 10.0) * 10.0
Length_of_snake += 1 clock.tick(snake_speed) pygame.quit() quit() gameLoop()
JOGO_GALO

# Tic-Tac-Toe Program using# random number in Python# importing all necessary


librariesimport numpy as npimport randomfrom time import sleep# Creates an empty
boarddef create_board():return(np.array([[0, 0, 0],[0, 0, 0],[0, 0, 0]]))# Check for empty places
on boarddef possibilities(board):l = []for i in range(len(board)):for j in range(len(board)):if
board[i][j] == 0:l.append((i, j))return(l)# Select a random place for the playerdef
random_place(board, player):selection = possibilities(board)current_loc =
random.choice(selection)board[current_loc] = playerreturn(board)# Checks whether the player
has three# of their marks in a horizontal rowdef row_win(board, player):for x in
range(len(board)):win = Truefor y in range(len(board)):if board[x, y] != player:win =
Falsecontinueif win == True:return(win)return(win)# Checks whether the player has three# of
their marks in a vertical rowdef col_win(board, player):for x in range(len(board)):win = Truefor
y in range(len(board)):if board[y][x] != player:win = Falsecontinueif win ==
True:return(win)return(win)# Checks whether the player has three# of their marks in a
diagonal rowdef diag_win(board, player):win = Truey = 0for x in range(len(board)):if board[x, x]
!= player:win = Falseif win:return winwin = Trueif win:for x in range(len(board)):y = len(board) –
1 – xif board[x, y] != player:win = Falsereturn win# Evaluates whether there is# a winner or a
tiedef evaluate(board):winner = 0for player in [1, 2]:if (row_win(board, player)
orcol_win(board,player) ordiag_win(board,player)):winner = playerif np.all(board != 0) and
winner == 0:winner = -1return winner# Main function to start the gamedef play_game():board,
winner, counter = create_board(), 0, 1print(board)sleep(2)while winner == 0:for player in [1,
2]:board = random_place(board, player)print(“Board after ” + str(counter) + ”
move”)print(board)sleep(2)counter += 1winner = evaluate(board)if winner !=
0:breakreturn(winner)# Driver Codeprint(“Winner is: ” + str(play_game()))

You might also like