108 lines
2.6 KiB
Python
108 lines
2.6 KiB
Python
import pygame
|
|
|
|
BLOCKSIZE = 200
|
|
width = 600
|
|
height = 600
|
|
SIZE = (width, height)
|
|
BLACK = (0, 0, 0)
|
|
WHITE = (255, 255, 255)
|
|
RED = (255, 0, 0)
|
|
|
|
class Block:
|
|
def __init__(self, grid_pos, visibility):
|
|
self.grid_pos = grid_pos
|
|
self.visibility = visibility
|
|
def draw(self,screen):
|
|
block_shape = (self.grid_pos[0], self.grid_pos[1], BLOCKSIZE, BLOCKSIZE)
|
|
pygame.draw.rect(screen, BLACK, block_shape)
|
|
pygame.draw.rect(screen, RED, block_shape, 1)
|
|
|
|
class Circle:
|
|
def __init__(self, grid_pos, visibility):
|
|
self.grid_pos = grid_pos
|
|
self.visibility = visibility
|
|
def set_visible(self):
|
|
self.visibility = True
|
|
def draw(self,screen):
|
|
radius = BLOCKSIZE // 2
|
|
center = (self.grid_pos[0]-radius, self.grid_pos[1]-radius)
|
|
pygame.draw.circle(screen, WHITE, center, radius)
|
|
pygame.draw.circle(screen, BLACK, center, radius-10)
|
|
|
|
class Cross:
|
|
def __init__(self, grid_pos, visibility):
|
|
self.grid_pos = grid_pos
|
|
self.visibility = visibility
|
|
def set_visible(self):
|
|
self.visibility = True
|
|
def draw(self,screen):
|
|
pass
|
|
|
|
|
|
def main():
|
|
screen = init_game()
|
|
create_grid(screen)
|
|
game_loop(screen)
|
|
exit_game()
|
|
|
|
def init_game():
|
|
global circles
|
|
global blocks
|
|
global crosses
|
|
crosses ={}
|
|
circles = {}
|
|
blocks = {}
|
|
pygame.init()
|
|
screen = pygame.display.set_mode(SIZE)
|
|
return screen
|
|
|
|
def game_loop(screen):
|
|
while True:
|
|
if not event_handling():
|
|
break
|
|
if not update_game():
|
|
break
|
|
event = event_handling()
|
|
update_game()
|
|
draw_game(screen)
|
|
|
|
def event_handling():
|
|
for event in pygame.event.get():
|
|
if event.type == pygame.QUIT:
|
|
return False
|
|
if event.type == pygame.MOUSEBUTTONDOWN:
|
|
pos = pygame.mouse.get_pos()
|
|
x = pos[0] // BLOCKSIZE * BLOCKSIZE
|
|
y = pos[1] // BLOCKSIZE * BLOCKSIZE
|
|
circles[(x, y)].set_visible()
|
|
return True
|
|
|
|
|
|
def update_game():
|
|
return True
|
|
|
|
def draw_game(screen):
|
|
screen.fill(BLACK)
|
|
|
|
for x,y in blocks:
|
|
if blocks[(x,y)].visibility:
|
|
blocks[(x,y)].draw(screen)
|
|
if circles[(x,y)].visibility:
|
|
circles[(x,y)].draw(screen)
|
|
|
|
pygame.display.flip()
|
|
|
|
def create_grid(screen):
|
|
y = 0
|
|
for _ in range(3):
|
|
x = 0
|
|
for _ in range(3):
|
|
blocks[(x,y)]= (Block ((x, y), True))
|
|
circles[(x, y)] = (Circle((x, y), False))
|
|
x += BLOCKSIZE
|
|
y += BLOCKSIZE
|
|
|
|
def exit_game():
|
|
pygame.quit()
|
|
|
|
main() |