Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- """
- This program is checkers. That is all.
- """
- import pygame as py
- import random
- from pygame import *
- py.init()
- # Screen size, if you change this the board will scale with the width.
- width = 600
- height = 650
- # create the screen and set the caption
- screen = py.display.set_mode((width, height))
- py.display.set_caption("Checkers")
- fps = 75
- clock = py.time.Clock()
- white = (255,255,255)
- dark_gray = (100,100,100)
- black = (0,0,0)
- brown = (101,67,33)
- beige = (222, 184, 135)
- redwood = (165, 91, 83)
- # Configurable size of the board, and the size of the squares changes with it.
- board_size = 8
- square_size = width / board_size
- # Groups to make my life easier
- squares = py.sprite.Group()
- checkers = py.sprite.Group()
- all_sprites = py.sprite.Group()
- # variable to figure out whos turn it is
- turn = white
- # Sets up the board logic. because of not enough planning it is [y, x]
- board = [[None for i in range(board_size)] for j in range(board_size)]
- # Checker sprite
- class Checker(py.sprite.Sprite):
- def __init__(self, location, color):
- super(Checker, self).__init__()
- # create a circle of the color
- self.surf = py.Surface((square_size-10,square_size-10))
- self.surf.fill(black)
- self.surf.set_colorkey(black)
- py.draw.circle(self.surf, color, [(square_size-10)//2,(square_size-10)//2], (square_size-10)//2)
- # set starting position based on arguments.
- self.rect = self.surf.get_rect(topleft=(location[0]+5,location[1]+5))
- # set important variables
- self.isCrown = False
- self.isSelected = False
- self.coords = [int(location[0]//square_size), int(location[1]//square_size)]
- self.location = [float(location[0]+5),float(location[1]+5)]
- self.color = color
- # sets the position of the checker somewhere and also you can determine what becomes the center.
- # I made this function to make it easier to move it.
- def set_pos(self, location, reference):
- if reference == 'topleft':
- self.rect = self.surf.get_rect(topleft=location)
- elif reference == 'center':
- self.rect = self.surf.get_rect(center=location)
- # returns all valid moves. for a move to be valid it must be a jump,
- # be empty, and be across the diagonal.
- def valid_moves(self):
- valid_moves = []
- # figure out which direction it is coming from.
- if self.color == white:
- direction = -1
- else:
- direction = 1
- # variables to hold the location of the
- topright = [self.coords[0]-direction, self.coords[1]+direction]
- topleft = [self.coords[0]+direction, self.coords[1]+direction]
- # forward right
- if topright[0] < 7 and topright[1] > 0:
- if board[topright[1]][topright[0]] == None:
- valid_moves.append(topright)
- 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:
- valid_moves.append([topright[0]-direction, topright[1]+direction])
- # forward left
- if topleft[0] > 0 and topleft[1] > 0:
- if board[topleft[1]][topleft[0]] == None:
- valid_moves.append(topleft)
- 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:
- valid_moves.append([topleft[0]+direction, topleft[1]+direction])
- if self.isCrown:
- print("is a crown")
- """
- TO DO
- """
- return valid_moves
- # Determine if move is valid, and if so snap to the grid
- # and tell the game logic that there is a checker there
- def snap_to_grid(self):
- global turn
- x, y = self.rect.topleft
- coord_x = round(x / square_size)
- coord_y = round(y / square_size)
- new_x = coord_x * square_size + 5
- new_y = coord_y * square_size + 5
- # get all valid moves from the starting position
- valid_moves = self.valid_moves()
- # check if it is valid, or if it is going back to the same spot. If either is true,
- # then reset. If not, move to that spot.
- if [new_x, new_y] == self.location or not [coord_x, coord_y] in valid_moves:
- self.set_pos(self.location, 'topleft')
- else:
- # move to new position
- self.set_pos((new_x, new_y), 'topleft')
- # tell game data that the checker moved from the old square
- board[self.coords[1]][self.coords[0]] = None
- # the next few lines are to determine if the checker jumped or not
- old_x, old_y = self.coords
- self.location = [new_x, new_y]
- self.coords = [int(self.location[0]//square_size), int(self.location[1]//square_size)]
- distance = (old_x - self.coords[0])^2 + (old_y - self.coords[1])*2
- # check if checker jumped top right
- if distance == -8:
- board[self.coords[1]+1][self.coords[0]-1].kill()
- # check if checker jumped top left
- elif distance == 4:
- board[self.coords[1]+1][self.coords[0]+1].kill()
- # check if checker jumped bottom right
- elif distance == 0:
- board[self.coords[1]-1][self.coords[0]-1].kill()
- # check if checker jumped bottom left
- elif distance == -4:
- board[self.coords[1]-1][self.coords[0]+1].kill()
- # tell game data the checker moved to the new square
- board[self.coords[1]][self.coords[0]] = self
- # set to be the next turn
- if self.color == white:
- turn = dark_gray
- else:
- turn = white
- # if it is selected, move it with the cursor, if not then snap to the grid
- def update(self):
- mouse = py.mouse.get_pos()
- clicked = py.mouse.get_pressed()
- if clicked[0] and self.isSelected and self.color == turn:
- self.set_pos(mouse, 'center')
- elif not clicked[0] and self.isSelected:
- self.isSelected = False
- self.snap_to_grid()
- # each square on the board, technically didnt have to be a sprite but this was easiest.
- class Square(py.sprite.Sprite):
- def __init__(self, location, color):
- super(Square, self).__init__()
- self.surf = py.Surface((square_size, square_size))
- self.surf.fill(color)
- self.rect = self.surf.get_rect(topleft=location)
- self.location = location
- self.color = color
- # Point used to determine what checker the user clicks.
- class Point(py.sprite.Sprite):
- def __init__(self, location):
- super(Point, self).__init__()
- x = location[0]
- y = location[1]
- self.rect = py.Rect(x, y, 1, 1)
- # function to quit the game
- def quit_game():
- py.quit()
- quit()
- # Function to set up the board and the starting position of the checkers.
- def set_up():
- screen.fill(black)
- for i in range(board_size):
- for j in range(board_size):
- location = [i*square_size, j*square_size]
- if (i+j) % 2 == 0:
- color = brown
- if j > board_size-4:
- new_checker = Checker(location, white)
- checkers.add(new_checker)
- elif j < 3:
- new_checker = Checker(location, dark_gray)
- checkers.add(new_checker)
- else:
- color = beige
- new_square = Square(location, color)
- squares.add(new_square)
- # add the checkers to the game logic
- for checker in checkers:
- board[checker.coords[1]][checker.coords[0]] = checker
- # the main loop
- def game_loop():
- set_up()
- while True:
- # whenever an event happens it runs through this
- for event in py.event.get():
- # when the player hits the x button or alt-f4
- if event.type == QUIT:
- quit_game()
- # when the player clicks, check if it clicked a checker.
- elif event.type == MOUSEBUTTONDOWN:
- mouse = py.mouse.get_pos()
- collision = py.sprite.spritecollideany(Point(mouse), checkers)
- if collision:
- collision.isSelected = True
- screen.fill(redwood)
- # update position of the checkers
- checkers.update()
- # add all the squares to the screen first
- for entity in squares:
- screen.blit(entity.surf, entity.rect)
- # then add all the checkers so that they render in front of the squares
- for entity in checkers:
- screen.blit(entity.surf, entity.rect)
- # update the display and limit the while loop to run at the speed of the
- # set fps.
- py.display.update()
- clock.tick(fps)
- game_loop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement