Created
April 11, 2018 08:38
-
-
Save hallazzang/07935d44bfcd7bbb6fbabb194d5e0df9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import pygame | |
from pygame.locals import * | |
SCREEN_WIDTH, SCREEN_HEIGHT = 640, 480 | |
pygame.init() | |
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) | |
clock = pygame.time.Clock() | |
matrix = [[0]*8 for _ in range(8)] | |
while True: | |
left_pressed, right_pressed = False, False | |
for event in pygame.event.get(): | |
if event.type == QUIT: | |
sys.exit(0) | |
elif event.type == KEYDOWN: | |
if event.key == K_ESCAPE: | |
sys.exit(0) | |
elif event.key == K_s: | |
print('{%s}' % ','.join('0b' + ''.join(map(str, row)) for row in matrix)) | |
if event.type in (MOUSEMOTION, MOUSEBUTTONDOWN): | |
if event.type == MOUSEMOTION: | |
left_pressed, _, right_pressed = map(bool, event.buttons) | |
elif event.type == MOUSEBUTTONDOWN: | |
left_pressed = event.button == 1 | |
right_pressed = event.button == 3 | |
row = event.pos[1] // 50 | |
col = event.pos[0] // 50 | |
if row >= 0 and row < 8 and col >= 0 and col < 8: | |
if left_pressed: | |
matrix[row][col] = 1 | |
elif right_pressed: | |
matrix[row][col] = 0 | |
screen.fill((0, 0, 0)) | |
for row_idx, row in enumerate(matrix): | |
for col_idx, cell in enumerate(row): | |
if cell == 1: | |
color = (255, 0, 0) | |
else: | |
color = (100, 0, 0) | |
pygame.draw.rect(screen, color, (col_idx*50, row_idx*50, 50, 50)) | |
pygame.display.update() | |
clock.tick(60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment