import pygame
noOfTurns = 0
playerTurn = 0
gridwidth = 5
gridheight = 7
print "Please select grid width (5-18)"
input = raw_input('>')
gridwidth = int(input)
print "Please select grid height (5-18)"
input = raw_input('>')
gridheight = int(input)
BLACK = ( 0, 0, 0)
WHITE = ( 255, 255, 255)
GREEN = ( 0, 255, 0)
RED = ( 255, 0, 0)
PURPLE = ( 255, 0, 255)
YELLOW = ( 255, 255, 0)
width = 30 # This sets the width and height of each grid location
height = 30
margin = 5 # This sets the margin between each cell
#Calculate the size of the screen needed
screenw = (width*gridwidth) + ((gridwidth)*margin)
screenh = (height*gridheight) + ((gridheight)*margin)
grid = [] # Create a 2 dimensional array
for row in range(gridheight): # Add an empty array that will hold each cell in this row
grid.append([])
for column in range(gridwidth):
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
#grid[2][5] = 1
pygame.init() # Initialize pygame
size = [screenw, screenh] # Set the height and width of the screen
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Mouldy Chocolate") # Set title of screen
done = False #Loop until the user clicks the close button.
clock = pygame.time.Clock() # Used to manage how fast the screen updates
# -------- Main Program Loop -----------
while done == False:
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:
pos = pygame.mouse.get_pos() # User clicks the mouse. Get the position
column = pos[0] // (width + margin) # Change the x/y screen coordinates to grid coordinates
row = pos[1] // (height + margin)
noOfTurns = noOfTurns + 1 #counts the number of turns for each player
if playerTurn == 1: # Set that location to zero
grid[row][column] = 1
print("PLAYER 1: ", "Click ", pos, "Grid coordinates: ", row, column)
else:
grid[row][column] = 2
print("PLAYER 2: ", "Click ", pos, "Grid coordinates: ", row, column)
#Selects players turn
if noOfTurns == 4: # counts the number of turns
noOfTurns = 0 #reset player turns to zero
playerTurn = not playerTurn
print "Player", playerTurn #visual of which players turn
if (event.type == pygame.KEYDOWN) and (event.key == pygame.K_RETURN) and (noOfTurns >= 1):
noOfTurns = 0 #reset player turns to zero
playerTurn = not playerTurn
print "Player", playerTurn
# Set the screen background
screen.fill(BLACK)
# Draw the grid
for row in range(gridheight):
for column in range(gridwidth):
color = WHITE
if grid[row][column] == 1:
color = GREEN
if grid[row][column] == 2:
color = RED
pygame.draw.rect(screen,
color,
[(margin+width)*column+margin,
(margin+height)*row+margin,
width,
height])
# Limit to 20 frames per second
clock.tick(20)
# 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()