import pygame import random # Define some colors black = (0, 0, 0) # defines all colours to be used throughout the program white = (255, 255, 255) red = (255, 0, 0) light_red = (255, 100, 100) green = (0, 255, 0) light_green = (100, 255, 100) blue = (0, 0, 255) light_blue = (100, 100, 255) yellow = (255, 255, 0) light_yellow = (255, 255, 100) # This sets the WIDTH and HEIGHT of each grid location WIDTH = 50 HEIGHT = 50 MARGIN = 5 # Create a 2 dimensional array. A two dimensional # array is simply a list of lists. grid = [] for row in range(9): # Add an empty array that will hold each cell # in this row grid.append([]) for column in range(7): grid[row].append(0) # Append a cell # Set row 1, cell 5 to one. (Remember rows and # column numbers start at zero.) grid[0][0] = 20 grid[0][1] = 20 grid[0][5] = 10 grid[0][6] = 10 grid[8][0] = 11 grid[8][1] = 11 grid[8][5] = 21 grid[8][6] = 21 # Initialize pygame pygame.init() # Set the HEIGHT and WIDTH of the screen WINDOW_SIZE = [1020, 576] screen = pygame.display.set_mode(WINDOW_SIZE) # Set title of screen pygame.display.set_caption("Array Backed Grid") # Used to manage how fast the screen updates clock = pygame.time.Clock() dice_1_img = pygame.image.load("Dice_roll_1.png") dice_2_img = pygame.image.load("Dice_roll_2.png") dice_3_img = pygame.image.load("Dice_roll_3.png") dice_4_img = pygame.image.load("Dice_roll_4.png") arrow_left_img = pygame.image.load("arrow_left.png") arrow_left_green_img = pygame.image.load("arrow_left_green.png") arrow_up_img = pygame.image.load("arrow_up.png") arrow_up_green_img = pygame.image.load("arrow_up_green.png") arrow_right_img = pygame.image.load("arrow_right.png") arrow_right_green_img = pygame.image.load("arrow_right_green.png") p1_c1_rowpos = 8 p1_c1_colpos = 0 p1_c2_rowpos = 8 p1_c2_colpos = 1 p2_c1_rowpos = 8 p2_c1_colpos = 5 p2_c2_rowpos = 8 p2_c2_colpos = 6 def left(counter_row, counter_col, taken, blot, start, home, dice_value): if counter_col - dice_value >= 0: if grid[counter_row][counter_col - dice_value] == 0 or grid[counter_row][counter_col - dice_value] == blot or grid[counter_row][counter_col - dice_value] == home: grid[counter_row][counter_col - dice_value] = taken grid[counter_row][counter_col] = 0 counter_col = counter_col - dice_value dir_chosen = True dice_rolled = False else: print("illegal move") elif counter_col - dice_value < 0 and counter_col != 0: if grid[counter_row][0] == 0 or grid[counter_row][0] == blot or grid[counter_row][0] == home: grid[counter_row][0] = taken grid[counter_row][counter_col] = 0 counter_col = 0 dir_chosen = True dice_rolled = False else: print("illegal move") else: print("illegal move") ####TRYING TO TAKE counter_col FROM HERE AND CALL IT AFTER THIS FUNCTION HAS RAN TO CHANGE THE STARTING POSITION OF COUNTER def right(counter_row, counter_col, taken, blot, start, home, dice_value): if counter_col + dice_value <= 6: if grid[counter_row][counter_col + dice_value] == 0 or grid[counter_row][counter_col + dice_value] == blot or grid[counter_row][counter_col - dice_value] == home: grid[counter_row][counter_col + dice_value] = taken grid[counter_row][counter_col] = 0 counter_col = counter_col + dice_value dir_chosen = True dice_rolled = False else: print("illegal move") elif counter_col + dice_value > 6 and counter_col != 6: if grid[counter_row][6] == 0 or grid[counter_row][6] == blot or grid[counter_row][6] == home: grid[counter_row][6] = taken grid[counter_row][counter_col] = 0 counter_col = 6 dir_chosen = True dice_rolled = False else: print("illegal move") else: print("illegal move") ####TRYING TO TAKE counter_col FROM HERE AND CALL IT AFTER THIS FUNCTION HAS RAN TO CHANGE THE STARTING POSITION OF COUNTER def up(counter_row, counter_col, taken, blot, start, home, dice_value): if counter_row - dice_value >= 0: if grid[counter_row - dice_value][counter_col] == 0 or grid[counter_row - dice_value][counter_col] == blot or grid[counter_row - dice_value][counter_col] == home: grid[counter_row - dice_value][counter_col] = taken grid[counter_row][counter_col] = 0 counter_row = counter_row - dice_value dir_chosen = True dice_rolled = False else: print("illegal move") elif counter_row - dice_value < 0 and counter_row != 0: if grid[0][counter_col] == 0 or grid[0][counter_col] == blot or grid[0][counter_col] == home: grid[0][counter_col] = taken grid[counter_row][counter_col] = 0 counter_row = 0 dir_chosen = True dice_rolled = False else: print("illegal move") else: print("illegal move") ####TRYING TO TAKE counter_row FROM HERE AND CALL IT AFTER THIS FUNCTION HAS RAN TO CHANGE THE STARTING POSITION OF COUNTER # -------- Main Program Loop ----------- def game_play(): running = True dice_rolled = False dir_chosen = False while running: for event in pygame.event.get(): # User did something if event.type == pygame.QUIT: # If user clicked close running = False # Flag that we are done so we exit this loop if event.type == pygame.MOUSEBUTTONDOWN: mouse = pygame.mouse.get_pos() if (140 + 75 > mouse[0] > 140 and 213 + 75 > mouse[1] > 213) and dice_rolled == False: dice_value = random.randrange(1, 5) if dice_value == 1: screen.blit(dice_1_img, [140, 213]) if dice_value == 2: screen.blit(dice_2_img, [140, 213]) if dice_value == 3: screen.blit(dice_3_img, [140, 213]) if dice_value == 4: screen.blit(dice_4_img, [140, 213]) pygame.display.update() dice_rolled = True dir_chosen = False if (65 + 75 > mouse[0] > 65 and 438 + 75 > mouse[1] > 438) and dice_rolled == True and dir_chosen == False: print("left", dice_value) left(p1_c1_rowpos, p1_c1_colpos, 1, 2, 11, 10, dice_value) dir_chosen = True dice_rolled = False ###I WANT p1_c1_colpos TO CHANGE TO WHATEVER counter_col IS AFTER IT'S COME OUT OF THE FUNCTION ###IVE TRIED USING THE RETURN FUNCTION BUT I CANT GET IT TO WORK if (215 + 75 > mouse[0] > 215 and 438 + 75 > mouse[1] > 438) and dice_rolled == True and dir_chosen == False: print("right", dice_value) right(p1_c1_rowpos, p1_c1_colpos, 1, 2, 11, 10, dice_value) dir_chosen = True dice_rolled = False ###I WANT p1_c1_colpos TO CHANGE TO WHATEVER counter_col IS AFTER IT'S COME OUT OF THE FUNCTION ###IVE TRIED USING THE RETURN FUNCTION BUT I CANT GET IT TO WORK if (140 + 75 > mouse[0] > 140 and 363 + 75 > mouse[1] > 363) and dice_rolled == True and dir_chosen == False: print("up", dice_value) up(p1_c1_rowpos, p1_c1_colpos, 1, 2, 11, 10, dice_value) dir_chosen = True dice_rolled = False ###I WANT p1_c1_rowpos TO CHANGE TO WHATEVER counter_row IS AFTER IT'S COME OUT OF THE FUNCTION ###IVE TRIED USING THE RETURN FUNCTION BUT I CANT GET IT TO WORK screen.blit(arrow_left_img, [65, 438]) screen.blit(arrow_up_img, [140, 363]) screen.blit(arrow_right_img, [215, 438]) pygame.display.update() # Draw the grid for row in range(9): for column in range(7): reccolor = white ellcolor = white if grid[row][column] == 10: reccolor = light_red ellcolor = light_red if grid[row][column] == 20: reccolor = light_blue ellcolor = light_blue if grid[row][column] == 11: reccolor = light_red ellcolor = red if grid[row][column] == 21: reccolor = light_blue ellcolor = blue if grid[row][column] == 1: reccolor = white ellcolor = red if grid[row][column] == 2: reccolor = white ellcolor = blue pygame.draw.rect(screen, reccolor, [((MARGIN + WIDTH) * column + MARGIN)+ 317, ((MARGIN + HEIGHT) * row + MARGIN) + 52, WIDTH, HEIGHT]) pygame.draw.ellipse(screen, ellcolor, [((MARGIN + WIDTH) * column + MARGIN)+ 317, ((MARGIN + HEIGHT) * row + MARGIN) + 52, WIDTH, HEIGHT]) # Limit to 60 frames per second clock.tick(60) pygame.display.flip() game_play() pygame.quit()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question