[ create a new paste ] login | about

Link: http://codepad.org/EulGPBxv    [ raw code | fork ]

Python, pasted on Mar 27:
import pygame

noOfTurns = 0
playerTurn = 1
posCheckRow = 0
posCheckColumn = 0
oldPosCheckRow = 0
oldPosCheckColumn = 0
firstMoveMode = 1      #First move mode - ignores position checks
gridwidth = 5
gridheight = 7
redrawBoard = True


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 WINNING BLOCK
grid[0][0] = 3



pygame.init()		# Initialize pygame
size = [screenw, screenh+height]	# Set the height and width of the screen
screen = pygame.display.set_mode(size)

userMessage="Player 1"
textColor = GREEN

basicFont = pygame.font.SysFont(None, 48)

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) // (height + margin)
            
            posCheckRow = row      #uses the value to check new postion hasn`t increased by >1 i.econnected block 
            posCheckColumn = column

            validMove =  ( ( abs(posCheckRow - oldPosCheckRow) + abs(posCheckColumn - oldPosCheckColumn) ) == 1 )

            if (grid[row][column] > 0) or (validMove==False and firstMoveMode == 0):
                print "validMove is ", validMove
            else:
                oldPosCheckRow = posCheckRow
                oldPosCheckColumn = posCheckColumn
                firstMoveMode = 0
                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)
                    redrawBoard = True
                else:
                    grid[row][column] = 2
                    print("PLAYER 2: ", "Click ", pos, "Grid coordinates: ", row, column)
                    redrawBoard = True
                      
	    	   
        #Selects players turn
        if noOfTurns ==  4:            # counts the number of turns
            noOfTurns = 0              #reset player turns to zero
            firstMoveMode = 1
            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
        redrawBoard = True
        playerTurn = not playerTurn     
        print "Player", playerTurn

               
        if (playerTurn == 1):
            userMessage="Player 1"
            textColor = GREEN
        else:
            userMessage="Player 2"
            textColor = RED

    if (redrawBoard == True):
        redrawBoard = False
        # 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     
                if grid[row][column] == 3:
                    color = YELLOW
                pygame.draw.rect(screen,
                                 color,
                                 [(margin+width)*column+margin,
                                  (margin+height)*row+margin+height,
                                  width,
                                  height])
                                  
        text = basicFont.render(userMessage, True, textColor, (0,0,0))
        textRect = text.get_rect()
        textRect.centerx = screen.get_rect().centerx
        textRect.centery = (height / 2)
        
        screen.blit(text, textRect)
        # Go ahead and update the screen with what we've drawn.
        pygame.display.flip()
     
    # Limit to 20 frames per second
    clock.tick(20)
     
# Be IDLE friendly. If you forget this line, the program will 'hang'
# on exit.
pygame.quit()


Create a new paste based on this one


Comments: