Advertisement
MrEDog

Checkers 1.2

Nov 5th, 2020
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 13.06 KB | None | 0 0
  1. """
  2.    This program is checkers. That is all.
  3. """
  4.  
  5. import pygame as py
  6. from pygame import *
  7.  
  8. py.init()
  9.  
  10. # Screen size, if you change this the board will scale with the width.
  11. width = 600
  12. height = 650
  13.  
  14. # create the screen and set the caption
  15. screen = py.display.set_mode((width, height))
  16. py.display.set_caption("Checkers")
  17.  
  18. fps = 75
  19.  
  20. clock = py.time.Clock()
  21.  
  22. white = (255,255,255)
  23. dark_gray = (100,100,100)
  24.  
  25. black = (0,0,0)
  26.  
  27. off_black = (1,1,1)
  28.  
  29. brown = (101,67,33)
  30. beige = (222, 184, 135)
  31.  
  32. redwood = (165, 91, 83)
  33.  
  34. # Configurable size of the board, and the size of the squares changes with it.
  35. board_size = 8
  36. square_size = width / board_size
  37.  
  38. # Groups to make my life easier
  39. squares = py.sprite.Group()
  40. checkers = py.sprite.Group()
  41. all_sprites = py.sprite.Group()
  42.  
  43. # variable to figure out whos turn it is
  44. turn = white
  45.  
  46. # Sets up the board logic. because of not enough planning it is [y, x]
  47. board = [[None for i in range(board_size)] for j in range(board_size)]
  48.  
  49. # Checker sprite
  50. class Checker(py.sprite.Sprite):    
  51.     def __init__(self, location, color):
  52.         super(Checker, self).__init__()
  53.        
  54.         # create a circle of the color
  55.         self.surf = py.Surface((square_size-10,square_size-10))
  56.         self.surf.fill(black)
  57.         self.surf.set_colorkey(black)
  58.        
  59.         py.draw.circle(self.surf, color, [(square_size-10)//2,(square_size-10)//2], (square_size-10)//2)
  60.        
  61.         # set starting position based on arguments.
  62.         self.rect = self.surf.get_rect(topleft=(location[0]+5,location[1]+5))
  63.        
  64.         # set important variables
  65.         self.isCrown = False
  66.         self.isSelected = False
  67.         self.coords = [int(location[0]//square_size), int(location[1]//square_size)]
  68.         self.location = [float(location[0]+5),float(location[1]+5)]
  69.         self.color = color
  70.        
  71.     # sets the position of the checker somewhere and also you can determine what becomes the center.
  72.     # I made this function to make it easier to move it.
  73.     def set_pos(self, location, reference):
  74.         if reference == 'topleft':
  75.             self.rect = self.surf.get_rect(topleft=location)
  76.         elif reference == 'center':
  77.             self.rect = self.surf.get_rect(center=location)
  78.            
  79.     def make_crown(self):
  80.         py.draw.circle(self.surf, off_black, [(square_size-10)//2, (square_size-10)//2], square_size//6)
  81.    
  82.     # returns all valid moves. for a move to be valid it must be a jump,
  83.     # be empty, and be across the diagonal.
  84.     def valid_moves(self):
  85.         valid_moves = []
  86.        
  87.         # figure out which direction it is coming from.
  88.         if self.color == white:
  89.             direction = -1
  90.         else:
  91.             direction = 1
  92.            
  93.         # variables to hold the location of the
  94.         topright = [self.coords[0]-direction, self.coords[1]+direction]
  95.         topleft = [self.coords[0]+direction, self.coords[1]+direction]
  96.        
  97.         bottomright = [self.coords[0]-direction, self.coords[1]-direction]
  98.         bottomleft = [self.coords[0]+direction, self.coords[1]-direction]
  99.            
  100.        
  101.         # forward right
  102.         if topright[0] in range(board_size) and topright[1] in range(board_size):
  103.             if board[topright[1]][topright[0]] == None:
  104.                 valid_moves.append(topright)
  105.             elif topright[1]+direction in range(board_size) and topright[0]-direction in range(8):
  106.                 if board[topright[1]][topright[0]] != None and board[topright[1]+direction][topright[0]-direction] == None and board[topright[1]][topright[0]].color != self.color:
  107.                     valid_moves.append([topright[0]-direction, topright[1]+direction])
  108.            
  109.         # forward left
  110.         if topleft[0] in range(board_size) and topleft[1] in range(board_size):
  111.             if board[topleft[1]][topleft[0]] == None:
  112.                 valid_moves.append(topleft)
  113.             elif topright[1]+direction in range(board_size) and topright[0]+direction in range(8):
  114.                 if board[topleft[1]][topleft[0]] != None and board[topleft[1]+direction][topleft[0]+direction] == None and board[topleft[1]][topleft[0]].color != self.color:
  115.                     valid_moves.append([topleft[0]+direction, topleft[1]+direction])
  116.            
  117.         if self.isCrown:
  118.             # backward right
  119.             if bottomright[0] in range(board_size) and bottomright[1] in range(board_size):
  120.                 if board[bottomright[1]][bottomright[0]] == None:
  121.                     valid_moves.append(bottomright)
  122.                 elif topright[1]-direction in range(board_size) and topright[0]+direction in range(8):
  123.                     if board[bottomright[1]][bottomright[0]] != None and board[bottomright[1]-direction][bottomright[0]-direction] == None and board[bottomright[1]][bottomright[0]].color != self.color:
  124.                         valid_moves.append([bottomright[0]-direction, bottomright[1]-direction])
  125.            
  126.             # backward left
  127.             if bottomleft[0] in range(board_size) and bottomleft[1] in range(board_size):
  128.                 if board[bottomleft[1]][bottomleft[0]] == None:
  129.                     valid_moves.append(bottomleft)
  130.                 elif topright[1]-direction in range(board_size) and topright[0]+direction in range(8):
  131.                     if board[bottomleft[1]][bottomleft[0]] != None and board[bottomleft[1]-direction][bottomleft[0]+direction] == None and board[bottomleft[1]][bottomleft[0]].color != self.color:
  132.                         valid_moves.append([bottomleft[0]+direction, bottomleft[1]-direction])
  133.            
  134.         return valid_moves
  135.    
  136.     # Determine if move is valid, and if so snap to the grid
  137.     # and tell the game logic that there is a checker there
  138.     def snap_to_grid(self):
  139.         global turn
  140.         x, y = self.rect.topleft
  141.        
  142.         coord_x = round(x / square_size)
  143.         coord_y = round(y / square_size)
  144.        
  145.         new_x = coord_x * square_size + 5
  146.         new_y = coord_y * square_size + 5
  147.        
  148.         # get all valid moves from the starting position
  149.         valid_moves = self.valid_moves()
  150.        
  151.         # check if it is valid, or if it is going back to the same spot. If either is true,
  152.         # then reset. If not, move to that spot.
  153.         if [new_x, new_y] == self.location or not [coord_x, coord_y] in valid_moves:
  154.             self.set_pos(self.location, 'topleft')
  155.         else:
  156.             # move to new position
  157.             self.set_pos((new_x, new_y), 'topleft')
  158.            
  159.             # tell game data that the checker moved from the old square
  160.             board[self.coords[1]][self.coords[0]] = None
  161.            
  162.             # the next few lines are to determine if the checker jumped or not
  163.             old_x, old_y = self.coords
  164.            
  165.             self.location = [new_x, new_y]
  166.             self.coords = [int(self.location[0]//square_size), int(self.location[1]//square_size)]
  167.            
  168.             distance = (old_x - self.coords[0])^2 + (old_y - self.coords[1])*2
  169.            
  170.             # check if checker jumped top right
  171.             if distance == -8:
  172.                 board[self.coords[1]+1][self.coords[0]-1].kill()
  173.                 board[self.coords[1]+1][self.coords[0]-1] = None
  174.             # check if checker jumped top left
  175.             elif distance == 4:
  176.                 board[self.coords[1]+1][self.coords[0]+1].kill()
  177.                 board[self.coords[1]+1][self.coords[0]+1] = None
  178.             # check if checker jumped bottom right
  179.             elif distance == 0:
  180.                 board[self.coords[1]-1][self.coords[0]-1].kill()
  181.                 board[self.coords[1]-1][self.coords[0]-1] = None
  182.             # check if checker jumped bottom left
  183.             elif distance == -4:
  184.                 board[self.coords[1]-1][self.coords[0]+1].kill()
  185.                 board[self.coords[1]-1][self.coords[0]+1] = None
  186.            
  187.  
  188.             # tell game data the checker moved to the new square
  189.             board[self.coords[1]][self.coords[0]] = self
  190.            
  191.             if self.coords[1] == 0 and self.color == white:
  192.                 self.isCrown = True
  193.                 self.make_crown()
  194.             elif self.coords[1] == 7 and self.color == dark_gray:
  195.                 self.isCrown = True
  196.                 self.make_crown()
  197.            
  198.             # set to be the next turn
  199.             if self.color == white:
  200.                 turn = dark_gray
  201.             else:
  202.                 turn = white
  203.            
  204.     # if it is selected, move it with the cursor, if not then snap to the grid
  205.     def update(self):
  206.         mouse = py.mouse.get_pos()
  207.         clicked = py.mouse.get_pressed()
  208.        
  209.         if clicked[0] and self.isSelected and self.color == turn:
  210.             self.set_pos(mouse, 'center')
  211.         elif not clicked[0] and self.isSelected:
  212.             self.isSelected = False
  213.             self.snap_to_grid()
  214.        
  215. # each square on the board, technically didnt have to be a sprite but this was easiest.
  216. class Square(py.sprite.Sprite):
  217.     def __init__(self, location, color):
  218.         super(Square, self).__init__()
  219.         self.surf = py.Surface((square_size, square_size))
  220.         self.surf.fill(color)
  221.         self.rect = self.surf.get_rect(topleft=location)
  222.        
  223. # Point used to determine what checker the user clicks.
  224. class Point(py.sprite.Sprite):
  225.     def __init__(self, location):
  226.         super(Point, self).__init__()
  227.         x = location[0]
  228.         y = location[1]
  229.         self.rect = py.Rect(x, y, 1, 1)
  230.        
  231. def text_objects(text, font):
  232.     textSurface = font.render(text, True, white)
  233.     return textSurface, textSurface.get_rect()
  234.  
  235. def turn_display(turn):
  236.     font = py.font.SysFont(None, 25)
  237.     text = font.render(turn, True, off_black)
  238.     return text
  239.  
  240. # function to quit the game
  241. def quit_game():
  242.     py.quit()
  243.     quit()
  244.    
  245. # ignore this, this is just for testing purposes
  246. def test_set_up():
  247.     screen.fill(black)
  248.    
  249.     for i in range(board_size):    
  250.         for j in range(board_size):
  251.             location = [i*square_size, j*square_size]
  252.            
  253.             if (i+j) % 2 == 0:
  254.                 color = brown
  255.             else:
  256.                 color = beige
  257.                
  258.             new_square = Square(location, color)
  259.             squares.add(new_square)
  260.            
  261.     new_checker1 = Checker([4*square_size, 6*square_size], dark_gray)
  262.     checkers.add(new_checker1)
  263.    
  264.     new_checker2 = Checker([3*square_size, 1*square_size], white)
  265.     checkers.add(new_checker2)
  266.    
  267.     # add the checkers to the game logic
  268.     for checker in checkers:
  269.         board[checker.coords[1]][checker.coords[0]] = checker
  270.    
  271. # Function to set up the board and the starting position of the checkers.
  272. def set_up():
  273.     screen.fill(redwood)
  274.    
  275.     for i in range(board_size):    
  276.         for j in range(board_size):
  277.             location = [i*square_size, j*square_size]
  278.            
  279.             if (i+j) % 2 == 0:
  280.                 color = brown
  281.                
  282.                 if j > board_size-4:
  283.                     new_checker = Checker(location, white)
  284.                     checkers.add(new_checker)
  285.                 elif j < 3:
  286.                     new_checker = Checker(location, dark_gray)
  287.                     checkers.add(new_checker)
  288.             else:
  289.                 color = beige
  290.    
  291.             new_square = Square(location, color)
  292.             squares.add(new_square)
  293.    
  294.     # add the checkers to the game logic
  295.     for checker in checkers:
  296.         board[checker.coords[1]][checker.coords[0]] = checker
  297.  
  298. # the main loop
  299. def game_loop():
  300.     set_up()
  301.     #test_set_up()
  302.    
  303.     while True:
  304.         # whenever an event happens it runs through this
  305.         for event in py.event.get():
  306.             # when the player hits the x button or alt-f4
  307.             if event.type == QUIT:
  308.                 quit_game()
  309.                 # when the player clicks, check if it clicked a checker.
  310.             elif event.type == MOUSEBUTTONDOWN:
  311.                 mouse = py.mouse.get_pos()                
  312.                 collision = py.sprite.spritecollideany(Point(mouse), checkers)
  313.            
  314.                 if collision:
  315.                     collision.isSelected = True
  316.        
  317.         screen.fill(redwood)            
  318.    
  319.         # update position of the checkers
  320.         checkers.update()
  321.        
  322.         # add all the squares to the screen first
  323.         for entity in squares:
  324.             screen.blit(entity.surf, entity.rect)
  325.            
  326.         # then add all the checkers so that they render in front of the squares
  327.         for entity in checkers:
  328.             screen.blit(entity.surf, entity.rect)
  329.        
  330.        
  331.         # Display what turn it is.
  332.         if turn == white:            
  333.             turn_text = turn_display("Turn: White")
  334.         else:
  335.             turn_text = turn_display("Turn: Gray")
  336.  
  337.         screen.blit(turn_text,(15,width+17))
  338.                    
  339.         # update the display and limit the while loop to run at the speed
  340.         # of the set fps.
  341.         py.display.update()
  342.         clock.tick(fps)
  343.        
  344. game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement