import pygame import numpy as np # --- constants --- (UPPER_CASE names) SCREEN_WIDTH = 1050 SCREEN_HEIGHT = 400 SPACING = 83.333 BUCKETS = [[]]*24 #BLACK = ( 0, 0, 0) #242 209 107 SAND = (242, 209, 107) GREEN_FILT = (0,102,0) WHITE = (255, 255, 255) RED = (255, 0, 0) SALMON = (250,128,114) BLACK = (0,0,0) BROWN = (160,82,45) LIGHT_GREY = (220,220,220) num_pieces = 15 FPS = 30 cen = SPACING/2 - 11 t = 5*SPACING - cen-22 m = 7*SPACING+50 - cen-22 STARTING_IDX_P1 = [[cen,0], [cen, 30], [cen, 60], [cen, 90], [cen,120], [SCREEN_WIDTH-cen-22, 0], [SCREEN_WIDTH-cen-22, 30], [t, 378],[t,348],[t,318],[m, 378], [m,348],[m,318],[m,288],[m,258]] STARTING_IDX_P2 = [[cen, 378], [cen, 348], [cen, 318], [cen, 288], [cen, 258], [SCREEN_WIDTH-cen-22, 378], [SCREEN_WIDTH-cen-22, 348], [t, 0], [t, 30], [t, 60], [m, 0], [m,30],[m,60],[m,90],[m,120]] # --- classses --- (CamelCase names) # empty # --- functions --- (lower_case names) # empty # --- main --- # - init - pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) #screen_rect = screen.get_rect() pygame.display.set_caption("Backgammon") # - objects - all_rects = {-1 : [], 1 : []} x, y = [0, 0] for p in [-1,1]: if p == -1: for idx in STARTING_IDX_P1: all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)] if p == 1: for idx in STARTING_IDX_P2: all_rects[p] += [pygame.rect.Rect(idx[0],idx[1], 22, 22)] # for i in range(num_pieces): # x = x+20 # all_rects[p] += [pygame.rect.Rect(x,y, 22, 22)] # x = 100 # y += 100 all_drag = {-1 : [], 1 : []} all_drag[-1] += [False]*num_pieces all_drag[1] += [False]*num_pieces all_off = {-1 : [], 1 : []} all_off[-1] += [[0,0]]*num_pieces all_off[1] += [[0,0]]*num_pieces is_true = False # - mainloop - clock = pygame.time.Clock() buckets = [5,0,0,0,3,0,5,0,0,0,0,2,5,0,0,0,3,0,5,0,0,0,0,2] running = True player = 1 def find_pin(pos): x,y = pos x -= 50 if x > 550 else 0 if y < 175: pin = (13 + int(x / SPACING)) idx = int(x / SPACING) elif y > 225: pin = (12 - int(x / SPACING)) idx = 12+ int(x / SPACING) return pin, idx # Find the y position based on the chosen pin def calc_pos(buckets, chosen): amount = buckets[chosen] # My math is incredibly off if chosen > 11: print(amount) y = 378 - (30 * amount) print(y) chosen -= 11 x = (SPACING*(chosen-1))+(SPACING/2) x += 50 if x > 500 else 0 else: chosen += 1 y = 30 * amount x = (SPACING*(chosen-1))+(SPACING/2) x += 50 if x > 500 else 0 return x,y while running: # - events - for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.MOUSEBUTTONDOWN: if event.button == 1: rectangles = all_rects[player] offsets = all_off[player] meh = [rect.collidepoint(event.pos) for rect in rectangles] if any(meh): is_true = np.where(meh)[0][0] if any(meh): print("GETTING CALLED") rectangles_drag[is_true] = True mouse_x, mouse_y = event.pos pin, idx = find_pin(event.pos) buckets[idx] -= 1 offsets[is_true][0] = rectangles[is_true].x - mouse_x offsets[is_true][1] = rectangles[is_true].y - mouse_y elif event.type == pygame.MOUSEBUTTONUP: if event.button == 1: if is_true is not None: pin, idx = find_pin(event.pos) print(pin,idx) print(calc_pos(buckets, idx)) x, y = calc_pos(buckets,idx) buckets[idx] += 1 rectangles_drag = all_drag[player] rectangles_drag[is_true] = False rectangles[is_true].x = x rectangles[is_true].y = y elif event.type == pygame.MOUSEMOTION: rectangles_drag = all_drag[player] rectangles = all_rects[player] offsets = all_off[player] if any(rectangles_drag): is_true = np.where(rectangles_drag)[0][0] if any(rectangles_drag): mouse_x, mouse_y = event.pos rectangles[is_true].x = mouse_x + offsets[is_true][0] rectangles[is_true].y = mouse_y + offsets[is_true][1] # - updates (without draws) - # empty # - draws (without updates) - screen.fill(GREEN_FILT) # pygame.draw.polygon(screen, (RED), [[0, 0], [50,0],[25,100]], 2) color = LIGHT_GREY x = 0 y = 150 # for _ in range(2): for i in range(12): if x < 500 and x+SPACING > 500: x = 550 color = SALMON if color == LIGHT_GREY else LIGHT_GREY pygame.draw.polygon(screen, color, [[x, 0], [x+SPACING, 0], [(2*x+SPACING)/2, y]]) x += SPACING # y += 50 x = 0 y = 250 # for _ in range(2): color = SALMON if color == LIGHT_GREY else LIGHT_GREY for i in range(12): if x < 500 and x+SPACING > 500: x = 550 color = SALMON if color == LIGHT_GREY else LIGHT_GREY pygame.draw.polygon(screen, color, [[x, 400], [x+SPACING, 400], [(2*x+SPACING)/2, y]]) x += SPACING pygame.draw.rect(screen, BROWN, pygame.rect.Rect((500, 0, 50, 400))) for p in [-1,1]: for rect in all_rects[p]: pygame.draw.rect(screen, RED if p == -1 else BLACK, rect) pygame.display.flip() # - constant game speed / FPS - clock.tick(FPS) # - end - pygame.quit()