You are on page 1of 3

# Checkers

# Draw a 3 x 3 checkerboard with alternating light


(beige) and dark color (blue)
# Draw the cell number on each Tile
# The cell number should be computed using the
row_index and col_index of the board

import pygame
def main():
pygame.init()
pygame.display.set_mode((500, 400))
pygame.display.set_caption('Checkerboard')
w_surface = pygame.display.get_surface()
game = Game(w_surface)
game.play()
pygame.quit()

class Game:
def __init__(self, ​|__Q1__|​):
# === objects that are part of every game that we will discuss
self.surface = game_surface
self.bg_color = pygame.Color('black')
self.FPS = 60
self.game_Clock = pygame.time.Clock()
self.close_clicked = False
self.continue_game = True
# === game specific objects
self.board = []
self.board_size = 3
​|__Q2__|
def create_board(self):
​|__Q3__|
width = self.surface.get_width()//self.board_size
height = self.surface.get_height()//self.board_size
for row_index in range(0, self.board_size):
row = []
for col_index in range(0,self.board_size):
x = ​|__Q4__|
y = ​|__Q5__|
cell = ​|__Q6__|
​|__Q7__|
row.append(tile)
​|__Q8__|
def play(self):
while ​|__Q9__|​:
self.handle_events()
self.draw()
if ​|__Q10__|
self.update()
self.decide_continue()
self.game_Clock.tick(self.FPS)
def handle_events(self):
events = pygame.event.get()
for ​|__Q11_|​ in events:
if item.type == pygame.QUIT:
​|__Q12__|
def draw(self):
self.surface.fill(self.bg_color) # clear the display surface first
for ​|__Q13__|​ in self.board:
for tile in row:
tile.draw()
pygame.display.update()
# update and decide_continue methods have pass statements
class Tile:
# Shared Attributes or Class Attributes
surface = None
even_color = pygame.Color('beige')
odd_color = pygame.Color('blue')
text_color = pygame.Color('black')
font_size = 40
@classmethod
def set_surface(cls,game_surface):
cls.surface = game_surface
# Instance Methods
def __init__(self,x,y,width,height,cell):
self.rect = pygame.Rect(x,y,width,height)
self.content = cell
def draw(self):
if ​|__Q14__|​ :
pygame.draw.rect(Tile.surface,Tile.even_color,self.rect)
else:
pygame.draw.rect(Tile.surface,Tile.odd_color,self.rect)
self.draw_content()
def draw_content(self):
font = pygame.font.SysFont('',Tile.font_size)
text_surface = font.render(str(self.content),True,Tile.text_color)
t_x = self.rect.x + (self.rect.width - text_surface.get_width())//2
t_y = self.rect.y + (self.rect.height - text_surface.get_height())//2
​|__Q15__|​.blit(text_surface, ​|__Q16__|​)
main()

You might also like