Python Pygame Tic tac toe PVP [closed]
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
1
down vote
favorite
I am trying to make a player vs player Tic Tac Toe game using pygame.
So far I got a simple grid setup that shows me the coordinates and changes the box color to green when it's clicked.
How do I make 'x' and 'y' depending on which players turn it is, instead of a green box all the time.
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# ------- VARIABLES ------------
WIDTH = 160
HEIGHT = 160
MARGIN = 5
# ------- FUNCTIONS ---------------
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid =
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append()
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [500, 500]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen
pygame.display.set_caption("Array Backed Grid")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Which players turn is it?
grid[row][column] = 0
grid[row][column] = 1
grid[row][column] = 2
print("Click ", pos, "Grid coordinates: ", row, column)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(10):
for column in range(10):
color = WHITE
# Empty
if grid[row][column] == 0:
color = WHITE
# Player Y
if grid[row][column] == 1:
color = GREEN
#Player X
if grid[row][column] == 2:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
tic-tac-toe pygame
closed as off-topic by 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t Jun 7 at 6:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t
add a comment |Â
up vote
1
down vote
favorite
I am trying to make a player vs player Tic Tac Toe game using pygame.
So far I got a simple grid setup that shows me the coordinates and changes the box color to green when it's clicked.
How do I make 'x' and 'y' depending on which players turn it is, instead of a green box all the time.
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# ------- VARIABLES ------------
WIDTH = 160
HEIGHT = 160
MARGIN = 5
# ------- FUNCTIONS ---------------
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid =
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append()
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [500, 500]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen
pygame.display.set_caption("Array Backed Grid")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Which players turn is it?
grid[row][column] = 0
grid[row][column] = 1
grid[row][column] = 2
print("Click ", pos, "Grid coordinates: ", row, column)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(10):
for column in range(10):
color = WHITE
# Empty
if grid[row][column] == 0:
color = WHITE
# Player Y
if grid[row][column] == 1:
color = GREEN
#Player X
if grid[row][column] == 2:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
tic-tac-toe pygame
closed as off-topic by 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t Jun 7 at 6:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am trying to make a player vs player Tic Tac Toe game using pygame.
So far I got a simple grid setup that shows me the coordinates and changes the box color to green when it's clicked.
How do I make 'x' and 'y' depending on which players turn it is, instead of a green box all the time.
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# ------- VARIABLES ------------
WIDTH = 160
HEIGHT = 160
MARGIN = 5
# ------- FUNCTIONS ---------------
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid =
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append()
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [500, 500]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen
pygame.display.set_caption("Array Backed Grid")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Which players turn is it?
grid[row][column] = 0
grid[row][column] = 1
grid[row][column] = 2
print("Click ", pos, "Grid coordinates: ", row, column)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(10):
for column in range(10):
color = WHITE
# Empty
if grid[row][column] == 0:
color = WHITE
# Player Y
if grid[row][column] == 1:
color = GREEN
#Player X
if grid[row][column] == 2:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
tic-tac-toe pygame
I am trying to make a player vs player Tic Tac Toe game using pygame.
So far I got a simple grid setup that shows me the coordinates and changes the box color to green when it's clicked.
How do I make 'x' and 'y' depending on which players turn it is, instead of a green box all the time.
import pygame
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
# ------- VARIABLES ------------
WIDTH = 160
HEIGHT = 160
MARGIN = 5
# ------- FUNCTIONS ---------------
# Create a 2 dimensional array. A two dimensional
# array is simply a list of lists.
grid =
for row in range(10):
# Add an empty array that will hold each cell
# in this row
grid.append()
for column in range(10):
grid[row].append(0) # Append a cell
# Set row 1, cell 5 to one. (Remember rows and
# column numbers start at zero.)
grid[1][5] = 1
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
WINDOW_SIZE = [500, 500]
screen = pygame.display.set_mode(WINDOW_SIZE)
# Set title of screen
pygame.display.set_caption("Array Backed Grid")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# -------- Main Program Loop -----------
while not done:
for event in pygame.event.get(): # User did something
if event.type == pygame.QUIT: # If user clicked close
done = True # Flag that we are done so we exit this loop
elif event.type == pygame.MOUSEBUTTONDOWN:
# User clicks the mouse. Get the position
pos = pygame.mouse.get_pos()
# Change the x/y screen coordinates to grid coordinates
column = pos[0] // (WIDTH + MARGIN)
row = pos[1] // (HEIGHT + MARGIN)
# Which players turn is it?
grid[row][column] = 0
grid[row][column] = 1
grid[row][column] = 2
print("Click ", pos, "Grid coordinates: ", row, column)
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(10):
for column in range(10):
color = WHITE
# Empty
if grid[row][column] == 0:
color = WHITE
# Player Y
if grid[row][column] == 1:
color = GREEN
#Player X
if grid[row][column] == 2:
color = RED
pygame.draw.rect(screen,
color,
[(MARGIN + WIDTH) * column + MARGIN,
(MARGIN + HEIGHT) * row + MARGIN,
WIDTH,
HEIGHT])
# Limit to 60 frames per second
clock.tick(60)
# Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()
tic-tac-toe pygame
asked Jun 6 at 14:51
Smith Syed
91
91
closed as off-topic by 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t Jun 7 at 6:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t
closed as off-topic by 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t Jun 7 at 6:30
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Code not implemented or not working as intended: Code Review is a community where programmers peer-review your working code to address issues such as security, maintainability, performance, and scalability. We require that the code be working correctly, to the best of the author's knowledge, before proceeding with a review." â 200_success, Sam Onela, Stephen Rauch, Billal BEGUERADJ, t3chb0t
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes