Advertisement
MrEDog

Checkers 0.1.1

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